I am not a programmer, but would like to learn how to crop a PDF using Ghostscript.
I have installed Ghostscript 9.01 in my machine.
Please guide me step by step process (starting from invoking Ghostscript) to crop a PDF with the specific coordinates.
I am even new to Ghostscript.
First, take note that the measurement unit for PDF is the same as for PostScript: it's called a point [pt].
72 points == 1 inch == 25.4 millimeters
Assuming you have a page size of A4. Then the media dimensions are:
595 points width == 210 millimeters
842 points height == 297 millimeters
Assuming you want to crop off:
left edge: 24 points == 1/3 inch ~= 8.5 millimeters
right edge: 36 points == 1/2 inch ~= 12.7 millimeters
top edge: 48 points == 2/3 inch ~= 17.0 millimeters
bottom edge: 72 points == 1 inch ~= 25.4 millimeters
Then your Ghostscript commandline is this (on Windows):
gswin32c.exe ^
-o cropped.pdf ^
-sDEVICE=pdfwrite ^
-c "[/CropBox [24 72 559 794]" ^
-c " /PAGES pdfmark" ^
-f uncropped-input.pdf
Or on Linux:
gs \
-o cropped.pdf \
-sDEVICE=pdfwrite \
-c "[/CropBox [24 72 559 794]" \
-c " /PAGES pdfmark" \
-f uncropped-input.pdf
However, this may not work reliably for all types of PDFs [1]. In those cases you should alternatively try these commands:
gswin32c.exe ^
-o cropped.pdf ^
-sDEVICE=pdfwrite ^
-dDEVICEWIDTHPOINTS=595 ^
-dDEVICEHEIGHTPOINTS=842 ^
-dFIXEDMEDIA ^
-c "24 72 translate" ^
-c " 0 0 535 722 rectclip" ^
-f uncropped-input.pdf
or
gs \
-o cropped.pdf \
-sDEVICE=pdfwrite \
-dDEVICEWIDTHPOINTS=595 \
-dDEVICEHEIGHTPOINTS=842 \
-dFIXEDMEDIA \
-c "24 72 translate" \
-c " 0 0 535 722 rectclip" \
-f uncropped-input.pdf
[^] : To be more specific: it will not work for PDFs which come along with their own /CropBox
already defined to specific values. A dirty hack around that is to change the string /CropBox
for all pages where it is desired to /cROPBoX
(or similar case-changing) with a text editor prior to running the above GS command. The case-change effectively "disarms" the cropbox setting (without changing any PDF object offsets invalidating the existing xref
table) so it is no longer considered by PDF renderers.