DEV Community

KomoPDF
KomoPDF

Posted on

Why PDF annotations move when you rotate or zoom a page

A PDF viewer can put a signature in the wrong place even when the pointer coordinates look correct. The page might be rotated, zoomed, or displayed at a different size from its canvas bitmap. An overlay that happens to line up at 100% zoom can drift as soon as the user changes the view.

The fix starts with keeping two coordinate systems separate. Store an annotation in PDF page coordinates. Convert it to viewport coordinates when displaying it, and convert a pointer position back when the user edits it. PDF.js exposes both conversions on its page viewport.

Reproduce the offset

Use a page with a visible corner mark. Place an annotation near that mark, zoom in, then rotate the page by 90 degrees. Reopen the saved file in another viewer. If the annotation moves away from the mark, inspect how the placement was converted before blaming the export.

PDF page coordinates traditionally have their origin at the lower left and increase upward. A browser element's coordinates start at the upper left and increase downward. A PDF.js viewport accounts for that difference as well as the selected scale and rotation. Manually flipping Y with pageHeight - y can work for a simple unrotated page, but it does not handle all of those transformations.

PDF.js provides convertToViewportPoint(x, y) and convertToPdfPoint(x, y) on the viewport returned by page.getViewport(...). See the PDF.js viewport implementation and its rendering example.

Map a pointer through the viewport

The following example assumes that pageElement is the untransformed HTML element whose content box exactly matches the displayed PDF viewport. It has no border or padding, and the entire page is visible inside it. viewport is the same viewport used to render the page. The scale factors account for a page element displayed at a different CSS size from the viewport dimensions.

function pointerToPdf(event, pageElement, viewport) {
  const rect = pageElement.getBoundingClientRect();
  const viewportX = (event.clientX - rect.left) * viewport.width / rect.width;
  const viewportY = (event.clientY - rect.top) * viewport.height / rect.height;
  return viewport.convertToPdfPoint(viewportX, viewportY);
}

function pdfToCss(pdfX, pdfY, pageElement, viewport) {
  const [x, y] = viewport.convertToViewportPoint(pdfX, pdfY);
  const rect = pageElement.getBoundingClientRect();
  return {
    left: x * rect.width / viewport.width,
    top: y * rect.height / viewport.height,
  };
}
Enter fullscreen mode Exit fullscreen mode

Position an overlay relative to the same page element, using the returned left and top as CSS pixels. Save the PDF point, not the CSS coordinates. When zoom or rotation changes, create the viewport for the new view and run pdfToCss again. If the page is inside a scrolling container, clientX and getBoundingClientRect() are already in the same browser viewport coordinate space; do not add the scroll offset a second time.

A canvas can have a larger backing bitmap than its CSS size on a high-density display. That changes rendering resolution, not the coordinates returned by getBoundingClientRect(). Do not multiply the pointer by devicePixelRatio before this conversion. PDF.js's example separates the output scale used for canvas rendering from the viewport used to describe the page.

Points are easier than rectangles

A signature or text box has width and height as well as an anchor. Under rotation, the top-left screen corner is not necessarily the lower-left PDF corner. Convert all four corners through the viewport, then calculate the PDF-space bounding box from their minimum and maximum X and Y values. Keep the intended orientation separately if your export code needs it; a bounding box alone cannot describe every rotated object.

Test each supported page rotation, including the page's own rotation combined with any rotation the user applies. Build the viewport using the rotation actually displayed. A page with a nonzero crop box or a nondefault origin is another useful fixture: its visible corner is a safer test than assuming the page starts at (0, 0).

Check the saved file

Run a round-trip check for representative points: convert PDF to viewport and back, then compare within a small floating-point tolerance. That catches mismatched viewports but cannot prove that an exporter writes an annotation correctly. Save a copy, close the viewer, and inspect the mark in a second PDF reader at multiple zoom levels.

Also test near each edge, after scrolling, and on a high-density screen. If the overlay aligns in the browser but the exported mark moves, log the stored PDF coordinates and inspect the export library's placement conventions. Some APIs accept coordinates in a transformed page space or apply their own rotation. Keep an untouched original while testing, especially for signed documents: writing an annotation can change signature validation.

For a related rendering issue, see why PDF previews go blank on high-density screens. That article covers canvas memory; this coordinate mapping uses CSS dimensions regardless of the canvas bitmap's resolution.

Top comments (0)