使用Ghostscript 9.01裁剪PDF [英] Cropping a PDF using Ghostscript 9.01

查看:120
本文介绍了使用Ghostscript 9.01裁剪PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是程序员,但想学习如何使用Ghostscript裁剪PDF.

I am not a programmer, but would like to learn how to crop a PDF using Ghostscript.

我已经在计算机上安装了Ghostscript 9.01.

I have installed Ghostscript 9.01 in my machine.

请逐步指导我(从调用Ghostscript开始)以裁剪具有特定坐标的PDF.

Please guide me step by step process (starting from invoking Ghostscript) to crop a PDF with the specific coordinates.

我什至不熟悉Ghostscript.

I am even new to Ghostscript.

推荐答案

首先,请注意,PDF的度量单位与PostScript相同:称为 point [pt].

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

假设页面大小为A4.然后,媒体尺寸为:

Assuming you have a page size of A4. Then the media dimensions are:

595 points width  == 210 millimeters
842 points height == 297 millimeters

假设您要裁剪:

   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

然后您的Ghostscript命令行是这样的(在Windows上):

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

或在Linux上:

gs                               \
  -o cropped.pdf                 \
  -sDEVICE=pdfwrite              \
  -c "[/CropBox [24 72 559 794]" \
  -c " /PAGES pdfmark"           \
  -f uncropped-input.pdf

但是,这可能不适用于所有类型的PDF [1] .在这种情况下,您还可以尝试以下命令:

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

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


[^] :更具体地说:它不适用于已将其/CropBox定义为特定值的PDF.解决此问题的肮脏办法是,在运行上述GS命令之前,使用文本编辑器将所有需要的页面的字符串/CropBox更改为/cROPBoX(或类似的大小写更改).大小写更改可以有效地撤消" cropbox设置(无需更改任何使现有xref表无效的PDF对象偏移量),因此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.

这篇关于使用Ghostscript 9.01裁剪PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆