Skip to content

fix(engines): correct annotation positions for PDFs with non-zero MediaBox#627

Open
marco-streng wants to merge 1 commit into
embedpdf:mainfrom
marco-streng:fix/annotation-position-non-zero-mediabox
Open

fix(engines): correct annotation positions for PDFs with non-zero MediaBox#627
marco-streng wants to merge 1 commit into
embedpdf:mainfrom
marco-streng:fix/annotation-position-non-zero-mediabox

Conversation

@marco-streng
Copy link
Copy Markdown

Summary

Closes: #625

Fix incorrect annotation positions for PDF files with a non-zero MediaBox origin (e.g., CAD/technical drawing exports from AutoCAD or Vectorworks). In such PDFs the MediaBox lower-left corner is not at (0, 0) — a typical example is [-1685, -1192, 1685, 1192]. Because the previous coordinate conversion assumed the origin was always (0, 0), annotation icons (e.g., text/comment stamps) were rendered at the raw PDF coordinate in CSS space, placing them far outside the visible page area. Native PDF viewers display these annotations at the correct position.

The fix reads each page's actual bounding-box origin at document open time via FPDF_GetPageBoundingBox, stores it in PdfPageObject.origin, and applies it in both the PDF→CSS and CSS→PDF coordinate conversions.

Changes

pdf.ts

  • Added optional origin?: Position field to PdfPageObject to carry the MediaBox/CropBox lower-left corner in PDF user space.

engine.ts

  • openDocumentBuffer: For each page, calls FPDF_LoadPage + FPDF_GetPageBoundingBox to read the bounding-box origin (left, bottom) and stores it in page.origin. Falls back to (0, 0) when the call fails or the PDF has no explicit origin.
  • convertPagePointToDevicePoint: Subtracts page.origin before converting PDF user-space coordinates to CSS device-space coordinates (device_x = PDF.x − ox, device_y = DH − (PDF.y − oy)).
  • convertDevicePointToPagePoint: Adds page.origin back when converting CSS device-space coordinates to PDF user space (inverse of the above), ensuring newly placed or moved annotations are saved at the correct position in the file.
  • convertPageRectToDeviceRect benefits automatically through delegation to convertPagePointToDevicePoint — no direct change required.

@vercel
Copy link
Copy Markdown

vercel Bot commented May 13, 2026

@marco-streng is attempting to deploy a commit to the OpenBook Team on Vercel.

A member of the Team first needs to authorize it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Annotations rendered outside page for PDFs with non-zero MediaBox origin

1 participant