【问题标题】:Cropping a PDF using Ghostscript 9.01使用 Ghostscript 9.01 裁剪 PDF
【发布时间】:2011-09-05 05:22:31
【问题描述】:

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

我已经在我的机器上安装了 Ghostscript 9.01。

请指导我一步一步的过程(从调用 Ghostscript 开始)裁剪具有特定坐标的 PDF。

我什至是 Ghostscript 的新手。

【问题讨论】:

    标签: pdf crop ghostscript


    【解决方案1】:

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

    72 points == 1 inch == 25.4 millimeters
    

    假设您的页面大小为 A4。那么媒体维度是:

    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 上):

    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]。在这些情况下,您也应该尝试以下命令:

    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 更改为(或类似的大小写更改)。大小写更改有效地“解除”了裁剪框设置(不更改任何使现有 xref 表无效的 PDF 对象偏移),因此 PDF 渲染器不再考虑它。

    【讨论】:

    • 如果您不知道原始 pdf 的尺寸是多少,您可以试试gs -sDEVICE=bbox -f uncropped-input.pdf
    • @jolly swagman:不,对不起,不,不,不! bbox 设备确实表示原始 PDF 的“尺寸”。它确实表明每个页面上的(虚拟)框将包含打印或查看页面上的所有标记。用于获取每个页面的尺寸('MediaBox')的一个命令是:pdfinfo -box some.pdf
    • @pipitas 对,它只是让你感觉页面的大小,但以后我会使用pdfinfo,因为这似乎更有用。
    • 另外,Google 可以帮助您完成转化google.co.uk/search?q=10cm+in+points
    • “但是,这可能不适用于所有类型的 PDF。在这些情况下,您应该或者尝试 [...]” 好吧,我都尝试过 Ghostscript 9.10,他们都不为我工作。另一方面,podofobox 可以工作,而且速度也快得多。另见How to crop PDF margins using pdftk and /MediaBox
    猜你喜欢
    • 2011-08-13
    • 1970-01-01
    • 1970-01-01
    • 2015-08-05
    • 2019-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多