Home
Make PDF pages smaller by cutting off white margins, and remove headers, footers or other content printed in the margins, from the command line. Toggle Page Boxes in SumatraPDF shows the boxes that set a page’s size.
Available in pre-release 3.7
Crop every page by 36 points (half an inch) on each side: sumatrapdf-tool run crop.js in.pdf out.pdf 36 36 36 36. At a glance:
  • See the page boxes: Ctrl + K, Toggle Page Boxes (CmdTogglePageBoxes) in SumatraPDF, or sumatrapdf-tool pages in.pdf.
  • Crop the page (hide the margins): the crop.js script below, run with sumatrapdf-tool run.
  • Remove content in the margins: sumatrapdf-tool trim -b mediabox -m 40 -o out.pdf in.pdf
sumatrapdf-tool is installed with SumatraPDF and added to PATH. SumatraPDF.exe takes the same arguments.

How page boxes work #

  • MediaBox is the whole sheet.
  • CropBox is the part of the sheet that viewers, SumatraPDF included, show and print. Without a CropBox the whole MediaBox is shown.
  • BleedBox, TrimBox and ArtBox are for print production; many PDFs don’t have them.
Cropping sets the CropBox: the margins are hidden, but what’s in them stays in the file. trim does the opposite: it deletes content outside a box but keeps the page size.

See the page boxes #

In SumatraPDF, open the PDF and press Ctrl + K, then Toggle Page Boxes in Command Palette. Each box the page has is outlined and labeled with its name. Run it again to hide them. In pre-release builds it is also Debug → Show page boxes. The command is only offered for PDF files.
From the command line:
sumatrapdf-tool pages in.pdf 1
<page pagenum="1">
<MediaBox l="0" b="0" r="595" t="842" />
<CropBox l="36" b="36" r="559" t="806" />
<Rotate v="0" />
</page>
Values are in points (172 inch), measured from the bottom-left corner. Leave out the page number to list every page. pages lists only the boxes set on the page itself; a MediaBox set once for the whole document isn’t shown. See sumatrapdf-tool pages.

Crop the margins #

Save this as crop.js:
// sumatrapdf-tool run crop.js in.pdf out.pdf top right bottom left
var doc = Document.openDocument(scriptArgs[0]);
var top = Number(scriptArgs[2]), right = Number(scriptArgs[3]);
var bottom = Number(scriptArgs[4]), left = Number(scriptArgs[5]);
for (var i = 0; i < doc.countPages(); i++) {
	var page = doc.loadPage(i);
	var r = page.getBounds("MediaBox"); // [x0, y0, x1, y1], y0 is the top
	page.setPageBox("CropBox", [r[0] + left, r[1] + top, r[2] - right, r[3] - bottom]);
}
doc.save(scriptArgs[1]);
Then run it with the margins to cut, in points, in the order top, right, bottom, left:
sumatrapdf-tool run crop.js in.pdf out.pdf 36 36 36 36
  • 72 points = 1 inch, 28.35 points = 1 cm.
  • Every page is cropped by the same amounts.
  • Top and left mean as the page is shown, also on rotated pages.
  • Margins are measured from the sheet (MediaBox), so cropping an already cropped file replaces the old crop instead of adding to it. To undo a crop, run it with 0 0 0 0.
Open out.pdf in SumatraPDF, or check the new CropBox with sumatrapdf-tool pages out.pdf 1. More on scripts: sumatrapdf-tool run.

Remove content in the margins #

trim deletes text, images and drawings that lie completely outside a box. To remove everything within 40 points of the page edges, e.g. running headers, footers and page numbers:
sumatrapdf-tool trim -b mediabox -m 40 -o out.pdf in.pdf
  • -b is the box to start from: mediabox, cropbox, bleedbox, trimbox or artbox.
  • -m moves the box inward (negative values move it outward): one value for all sides, 20,30 for top and bottom, left and right, or 10,20,30,40 for top, right, bottom, left.
  • -e does the reverse: removes the content inside the box and keeps the margins.
  • -o is required; without it trim stops with argument error: no output to write to.
  • Anything that is partly inside the box is kept whole.
  • The page size doesn’t change. To also make the page smaller, run crop.js on the result.
Note: pass -b. Without it trim uses the CropBox, and on a page without one it prints No box found for page 0 and changes nothing. -f falls back to the MediaBox, but then ignores -m.
If trim -b mediabox also prints No box found for page ..., the pages share one MediaBox set for the whole document (sumatrapdf-tool pages then lists no MediaBox). Give every page its own CropBox first, then trim to it:
sumatrapdf-tool run crop.js in.pdf tmp.pdf 0 0 0 0
sumatrapdf-tool trim -b cropbox -m 40 -o out.pdf tmp.pdf
More in sumatrapdf-tool trim.

Tips #

  • Use Toggle Page Boxes or pages first to see how big the margins are.
  • Use trim when the content must really be gone; cropping only hides it. To remove sensitive content, see Redact a PDF.
  • Write to a new file and compare it with the original in SumatraPDF before replacing it.

See also #