Ask Anvil

Answers to questions about automating PDFs, e-signatures, Webforms, and other paperwork problems.
E-signatures
Categories

How do I place a signature box on a rotated PDF page so it lands in the right spot?

const fs = require("fs");
const { PDFDocument } = require("pdf-lib");

// Convert a point measured on the page as displayed (origin top-left, y down,
// in points) into PDF user space (origin bottom-left of the CropBox).
function displayedToUserSpace(page, x, y) {
  const { x: ox, y: oy, width: w, height: h } = page.getCropBox();
  const rotation = ((page.getRotation().angle % 360) + 360) % 360;
  switch (rotation) {
    case 0:
      return { x: ox + x, y: oy + h - y };
    case 90:
      return { x: ox + y, y: oy + x };
    case 180:
      return { x: ox + w - x, y: oy + y };
    case 270:
      return { x: ox + w - y, y: oy + h - x };
    default:
      throw new Error("Unsupported /Rotate value: " + rotation);
  }
}

// A 100 x 40 box whose top-left corner sits at (72, 100) on the displayed page.
async function signatureBox(path) {
  const doc = await PDFDocument.load(fs.readFileSync(path));
  const page = doc.getPage(0);
  const a = displayedToUserSpace(page, 72, 100);
  const b = displayedToUserSpace(page, 172, 140);
  return {
    x: Math.min(a.x, b.x),
    y: Math.min(a.y, b.y),
    width: Math.abs(a.x - b.x),
    height: Math.abs(a.y - b.y),
  };
}

module.exports = { displayedToUserSpace, signatureBox };

Run npm install pdf-lib, then call signatureBox("input.pdf"). On a 612 by 792 point page with no rotation it returns x 72, y 652, width 100, height 40. On the same page with /Rotate set to 90, it returns x 100, y 72, width 40, height 100. Same box on screen, different rectangle in the file.

Why it happens

PDF stores positions in user space, where the origin is typically the lower left corner and y grows upward. The page's /Rotate entry is the number of degrees the page is turned clockwise when displayed or printed, and it must be a multiple of 90. So a coordinate you read off a rendered page (origin top left, y growing down) only matches user space on an unrotated page. On a rotated page the axes swap or flip, and the width and height of your box swap with them.

How the code works

The function reads the CropBox and the rotation with getCropBox and getRotation, then maps the displayed point to user space with one formula per rotation value. The signatureBox helper converts two opposite corners and takes the minimum and maximum, which gives a normal rectangle with a lower left origin that you can pass to whatever stamps or places the field.

Caveats

Displayed coordinates are measured from the CropBox, not the MediaBox. A CropBox defines the region the page is clipped to when displayed or printed, and its default is the MediaBox, so the two only differ on files that set a CropBox. The code already accounts for this by adding the CropBox origin.

This converts position only. Because /Rotate turns the whole page when it is displayed, anything you draw into the box in user space turns with the page, so text or an image drawn at the converted spot needs its own orientation handling. Check the result by rendering the page, not by reading the numbers.

Back to All Questions

The fastest way to build software for documents

Anvil Document SDK is a comprehensive toolbox for product teams launching document flows where PDF filling, signing, and complex conditional scenarios are necessary.
Explore Anvil
Anvil Webforms
Marketing Mode