PDF - 删除白边 [英] PDF - Remove White Margins

查看:855
本文介绍了PDF - 删除白边的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道一种从PDF文件中删除白边距的方法。就像Adobe Acrobat X Pro一样。我知道它不适用于每个PDF文件。

I would like to know a way to remove white margins from a PDF file. Just like Adobe Acrobat X Pro does. I understand it will not work with every PDF file.

我猜这样做的方法是获取文本边距,然后裁剪出边距。

I would guess that the way to do it, is by getting the text margins, then cropping out of that margins.

首选PyPdf。

iText根据此代码查找文本边距:

iText finds text margins based on this code:

public void addMarginRectangle(String src, String dest)
    throws IOException, DocumentException {
    PdfReader reader = new PdfReader(src);
    PdfReaderContentParser parser = new PdfReaderContentParser(reader);
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT));
    TextMarginFinder finder;
    for (int i = 1; i <= reader.getNumberOfPages(); i++) {
        finder = parser.processContent(i, new TextMarginFinder());
        PdfContentByte cb = stamper.getOverContent(i);
        cb.rectangle(finder.getLlx(), finder.getLly(),
            finder.getWidth(), finder.getHeight());
        cb.stroke();
    }
    stamper.close();
}


推荐答案

我不太熟悉使用PyPDF,但我知道Ghostscript将能够为你做到这一点。以下是类似问题的其他答案的链接:

I'm not too familiar with PyPDF, but I know Ghostscript will be able to do this for you. Here are links to some other answers on similar questions:


  1. 每页将PDF 2面转换为每页1面 (SuperUser.com)

  2. 自由软件将pdf的页面分割到中间? (SuperUser.com)

  3. 使用Ghostscript 9.01裁剪PDF (StackOverflow.com)

  1. Convert PDF 2 sides per page to 1 side per page (SuperUser.com)
  2. Freeware to split a pdf's pages down the middle? (SuperUser.com)
  3. Cropping a PDF using Ghostscript 9.01 (StackOverflow.com)

第三个答案可能是你说'我明白它不适用于每个PDF文件'的原因。它使用 pdfmark 命令尝试将 / CropBox 设置为PDF页面对象。

The third answer is probably what made you say 'I understand it will not work with every PDF file'. It uses the pdfmark command to try and set the /CropBox into the PDF page objects.

前三个答案的方法很可能在第三个答案失败时成功。此方法使用<< / PageOffset [NNN MMM]>>的PostScript命令片段。 setpagedevice 将PDF页面移动并放置在由 -gNNNNxMMMM 参数(定义设备宽度和高度,以像素为单位)定义的(较小)媒体大小上)。

The method of the first two answers will most likely succeed where the third one fails. This method uses a PostScript command snippet of <</PageOffset [NNN MMM]>> setpagedevice to shift and place the PDF pages on a (smaller) media size defined by the -gNNNNxMMMM parameter (which defines device width and height in pixels).

如果您理解前两个答案背后的概念,您将能够轻松地调整其中使用的方法来裁剪PDF的所有4个边缘上的边距页面:

If you understand the concept behind the first two answers, you'll easily be able to adapt the method used there to crop margins on all 4 edges of a PDF page:

一个示例命令,用于在4个边缘的每个边缘上裁剪一个字母大小的PDF(8.5x11in == 612x792pt)半英寸(== 36pt)适用于Windows):

An example command to crop a letter sized PDF (8.5x11in == 612x792pt) by half an inch (==36pt) on each of the 4 edges (command is for Windows):

gswin32c.exe ^
    -o cropped.pdf ^
    -sDEVICE=pdfwrite ^
    -g5400x7200 ^
    -c "<</PageOffset [-36 -36]>> setpagedevice" ^
    -f input.pdf

结果页面大小为7.5x10in(== 540x720pt)。要在Linux或Mac上执行相同操作,请使用:

The resulting page size will be 7.5x10in (== 540x720pt). To do the same on Linux or Mac, use:

gs \
    -o cropped.pdf \
    -sDEVICE=pdfwrite \
    -g5400x7200 \
    -c "<</PageOffset [-36 -36]>> setpagedevice" \
    -f input.pdf






更新:如何使用Ghostscript确定'边距'

评论要求自动确定白边距。你也可以使用Ghostscript。它的 bbox 设备可以确定每个页面上(虚拟)墨水所覆盖的区域(因此,间接地确定画布每个边缘的空白)。

A comment asked for 'automatic' determination of the white margins. You can use Ghostscript's too for this. Its bbox device can determine the area covered by the (virtual) ink on each page (and hence, indirectly the whitespace for each edge of the canvas).

这是命令:

gs \
  -q -dBATCH -dNOPAUSE \
  -sDEVICE=bbox \
   input.pdf 

输出(示例):

 %%BoundingBox: 57 29 562 764
 %%HiResBoundingBox: 57.265030 29.347046 560.245045 763.649977
 %%BoundingBox: 57 28 562 667
 %%HiResBoundingBox: 57.265030 28.347046 560.245045 666.295011

bbox 设备在内存中呈现每个PDF页面(不将任何输出写入磁盘),然后将BoundingBox和HiResBoundingBox信息打印到 stderr的。您可以像这样修改此命令以使结果更容易解析:

The bbox device renders each PDF page in memory (without writing any output to disk) and then prints the BoundingBox and HiResBoundingBox info to stderr. You may modify this command like that to make the results more easy to parse:

gs \
    -q -dBATCH -dNOPAUSE \
    -sDEVICE=bbox \
     input.pdf \
     2>&1 \  
  | grep -v HiResBoundingBox

输出(示例):

 %%BoundingBox: 57 29 562 764
 %%BoundingBox: 57 28 561 667

这会告诉你......

This would tell you...


  • ... ...左下角 Page 1 的内容矩形位于坐标 [57 29] ,右上角位于 [562 741]

  • ... Page 2 内容矩形的左下角位于坐标 [57 28] 右上角是 [561 667]

  • ...that the lower left corner of the content rectangle of Page 1 is at coordinates [57 29] with the upper right corner is at [562 741]
  • ...that the lower left corner of the content rectangle of Page 2 is at coordinates [57 28] with the upper right corner is at [561 667]

这意味着:


  • Page 1 在左边缘使用57pt的空格( 72pt == 1in == 25,4mm )。

  • Page 1 使用29pt的空格底边。

  • 第2页在左边缘使用57pt的空格。

  • Page 2 在底部边缘使用28pt的空格。

  • Page 1 uses a whitespace of 57pt on the left edge (72pt == 1in == 25,4mm).
  • Page 1 uses a whitespace of 29pt on the bottom edge.
  • Page 2 uses a whitespace of 57pt on the left edge.
  • Page 2 uses a whitespace of 28pt on the bottom edge.

从这个简单的例子可以看出,空格并不完全每页都一样。根据您的需要(您可能希望多页PDF的每一页都具有相同的大小,不是吗?),您必须计算出文档所有页面中每条边的最小边距。

As you can see from this simple example already, the whitespace is not exactly the same for each page. Depending on your needs (you likely want the same size for each page of a multi-page PDF, no?), you have to work out what are the minimum margins for each edge across all pages of the document.

现在右边和上边缘的空白怎么样?要计算,您需要知道每个页面的原始页面大小。确定这一点的最简单方法是: pdfinfo 实用程序。 5页PDF的示例命令:

Now what about the right and top edge whitespace? To calculate that, you need to know the original page size for each page. The most simple way to determine this: the pdfinfo utility. Example command for a 5 page PDF:

pdfinfo \
  -f 1 \
  -l 5 \
   input.pdf \
| grep "Page "

输出(示例):

Page    1 size: 612 x 792 pts (letter)
Page    2 size: 612 x 792 pts (letter)
Page    3 size: 595 x 842 pts (A4)
Page    4 size: 842 x 1191 pts (A3)
Page    5 size: 612 x 792 pts (letter)

这将帮助您确定所需的画布大小以及每个新PDF页面的顶部和右侧边缘所需的(最大)白色边距。

This will help you determine the required canvas size and the required (maximum) white margins of the top and right edges of each of your new PDF pages.

当然,这些计算都可以编写脚本。

These calculations can all be scripted too, of course.

但是如果你的PDF全部是uniq页面大小,或者它们是1页文档,这一切都更容易完成...

But if your PDFs are all of a uniq page size, or if they are 1-page documents, it all is much easier to get done...

这篇关于PDF - 删除白边的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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