{"record":{"id":"c59e93b551e06cd5","repo":"siyuan-note/siyuan","slug":"pdf-rectangle-annotation-has-invalid-coordinates","errorCode":null,"errorMessage":"PDF rectangle annotation has invalid coordinates","messagePattern":"PDF rectangle annotation has invalid coordinates","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"app/src/asset/anno.ts","lineNumber":1114,"sourceCode":"    }, Constants.TIMEOUT_DBLCLICK);\n};\n\nasync function getRectImgData(pdfObj: any, pageNumber: number, position: number[]) {\n    const pageView = pdfObj.pdfViewer.getPageView(pageNumber - 1);\n    if (!pageView) {\n        throw new Error(`PDF page view ${pageNumber} is unavailable`);\n    }\n\n    const pdfPage = await pdfObj.pdfDocument.getPage(pageNumber);\n    const totalRotation = ((pageView.rotation + pageView.pdfPageRotate) % 360 + 360) % 360;\n    const targetViewport = pdfPage.getViewport({\n        scale: PDF_RECT_CAPTURE_SCALE,\n        rotation: totalRotation,\n    });\n    const targetRect = targetViewport.convertToViewportRectangle(position);\n    const captureScale = getLimitedCaptureScale(targetRect);\n    if (captureScale <= 0) {\n        throw new Error(\"PDF rectangle annotation has invalid coordinates\");\n    }\n\n    const viewport = pdfPage.getViewport({scale: captureScale, rotation: totalRotation});\n    const captureBounds = getCaptureCanvasBounds(viewport.convertToViewportRectangle(position));\n    const captureViewport = pdfPage.getViewport({\n        scale: captureScale,\n        rotation: totalRotation,\n        offsetX: -captureBounds.left,\n        offsetY: -captureBounds.top,\n    });\n    const captureCanvas = document.createElement(\"canvas\");\n    captureCanvas.width = captureBounds.width;\n    captureCanvas.height = captureBounds.height;\n\n    const captureCtx = captureCanvas.getContext(\"2d\");\n    if (!captureCtx) {\n        throw new Error(\"Unable to create a canvas context for the PDF rectangle annotation\");\n    }","sourceCodeStart":1096,"sourceCodeEnd":1132,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/app/src/asset/anno.ts#L1096-L1132","documentation":"When capturing a PDF rectangle annotation, the code converts the annotation's position to viewport coordinates and computes a capture scale via getLimitedCaptureScale. If that scale is <= 0, the rectangle has degenerate dimensions and \"PDF rectangle annotation has invalid coordinates\" is thrown rather than producing a zero-size canvas.","triggerScenarios":"The stored annotation rectangle has zero or negative width/height (degenerate drag), or `position` is malformed/stale relative to the current page viewport/rotation.","commonSituations":"Annotations created by older app versions with a different coordinate convention; clicking instead of dragging to create a rectangle; position data corrupted by manual .sy editing or sync conflicts.","solutions":["Delete and redraw the problematic rectangle annotation","Validate that the annotation drag produced a non-degenerate rectangle before saving","Check for version skew between the annotation data and the current pdf.js viewport code; re-render the document","Verify the page rotation handling by reopening the asset"],"exampleFix":"// before\nif (captureScale <= 0) {\n    throw new Error(\"PDF rectangle annotation has invalid coordinates\");\n}\n// after: clamp defensively where the rect is created\nconst [x1, y1, x2, y2] = targetRect;\nif (Math.abs(x2 - x1) < 1 || Math.abs(y2 - y1) < 1) {\n    return null; // ignore degenerate rectangles instead of throwing\n}","handlingStrategy":"validation","validationCode":"const [x1, y1, x2, y2] = position;\nconst valid = Math.abs(x2 - x1) >= 1 && Math.abs(y2 - y1) >= 1;","typeGuard":"const isFiniteRect = (position: number[]): boolean => Array.isArray(position) && position.length === 4 && position.every(Number.isFinite);","tryCatchPattern":"try {\n    await captureRectangle(pdfObj, pageNumber, position);\n} catch (e) {\n    showMessage(e.message); // prompt the user to redraw the rectangle\n}","preventionTips":["Ignore click-without-drag gestures that produce degenerate rectangles","Validate stored annotation coordinates when loading annotations","Re-derive coordinates after viewport/rotation changes instead of reusing stale data"],"tags":["pdf","annotation","coordinates"],"backgroundTag":"invalid-argument-value","analyzedSha":"9f775e8a12daef8255556097396f9b2739078892","analyzedAt":"2026-09-19T03:17:15.984Z","contentChangedAt":"2026-09-19T03:17:15.984Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}