Cropping a PDF using Ghostscript 9.01

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-26 03:07:04

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/6183479/cropping-a-pdf-using-ghostscript-9-01

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!