Skip to content
Lamina PDF
0.9.0
GitHub
Open the web app

Crop pages

You’ll crop half an inch from each side of every page. Use it to trim the white border of a scan, to cut printer’s marks off a proof, or to make slides fit a phone’s screen.

The margins are measured on the page as a reader sees it. A page turned on its side loses the same strip from its visible top as an upright page does, so one set of margins suits a document of mixed sizes and rotations. The program prints each page whose crop hides content:

packages/sdk/examples/crop-pages.ts
// Crop every page by half an inch on each side, whatever its size or rotation.
//
// Margins are measured on the page as it shows, so a page turned on its side loses the same
// strip from its visible top as an upright one. Cropping only hides content: the edit lists the
// pages whose hidden content stays in the file, where text extraction still finds it.
import { Document } from "@laminapdf/sdk";
/** The document with 36 points cropped from each side of every page. Needs `init()`. */
export function cropPages(bytes: Uint8Array): Uint8Array<ArrayBuffer> {
using doc = Document.open(bytes);
const pages = doc.pages.map((page) => page.id);
const edited = doc.cropPages(pages, { top: 36, right: 36, bottom: 36, left: 36 });
for (const page of edited.croppedContentRemains ?? []) {
console.warn(`page ${page} hides content that is still in the file`);
}
return doc.save();
}
Page 1 of crop_pages.pdf
Open crop_pages.pdf (4 pages, 3 KB)

The input has four pages, one at each rotation, and the second is drawn at twice the usual scale. In the result, each page shows 72 points less across and 72 less down, and its “Page n” label sits the same distance from its visible corner.

  • cropPages (CropPages in Rust) sets each page’s crop box: the part of the page a reader shows and prints. It takes top, right, bottom and left in points, and a side you leave out is 0.
  • Cropping hides content but keeps it. Text outside the crop box stays in the file, and copying or extracting text still finds it. The edit’s croppedContentRemains lists the pages where that is true.
  • Signed forms: cropping a page that shows a signed field breaks the signature, so saving refuses the edit unless you allow the save to break it.
  • Undo: one undo() puts every page’s crop box back.
  • Set a box exactly: setPageBoxes (SetPageBoxes in Rust) writes any of a page’s five boxes in the page’s own coordinates: media (the paper), crop, bleed, trim and art. null removes a box, so the page uses its default, which is the media box for the crop box and the crop box for the others. Each box must lie inside the media box.
  • Crop only some pages: pass their IDs. To give pages different margins, crop each group in its own call inside one transact, and one undo reverts them all.
  • Private content: a crop isn’t a redaction. Anyone with another tool can read what it hides, so don’t crop a page to remove something private.

Next: organise pages, to delete, move and rotate them too.