iText - 裁剪出pdf文件的一部分 [英] iText - crop out a part of pdf file

查看:602
本文介绍了iText - 裁剪出pdf文件的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小问题,我正在尝试找一个解决方案。
长话短说我必须从带有itext的pdf中删除每个页面的顶部。我设法用 CROPBOX 来做到这一点,但问题是这会通过删除顶部来缩小页面。

I have a small problem and I'm trying for some time to find out a solution. Long story short I have to remove the top part of each page from a pdf with itext. I managed to do this with CROPBOX, but the problem is that this will make the pages smaller by removing the top part.

有人可以帮我实现这个,所以页面大小保持不变。我的想法是用白色矩形覆盖首页,但经过多次尝试后我没有设法做到这一点。

Can someone help me to implement this so the page size remains the same. My idea would be to override the top page with a white rectangle, but after many tries I didn't manage to do this.

这是我用来裁剪页面的当前代码。

This is the current code I'm using to crop the page.

PdfRectangle rect = new PdfRectangle(55, 0, 1000, 1000);
PdfDictionary pageDict;
for (int curentPage = 2; curentPage <= pdfReader.getNumberOfPages(); curentPage++) {
    pageDict = pdfReader.getPageN(curentPage);
    pageDict.put(PdfName.CROPBOX, rect);
}


推荐答案

在您的代码示例中,您是裁剪页面。这会降低页面的可见大小。

In your code sample, you are cropping the pages. This reduces the visible size of the page.

根据您的说明,您不希望裁剪。相反,你想要剪切

Based on your description, you don't want cropping. Instead you want clipping.

我写了一个例子,通过引入200个用户的边距来剪辑PDF的所有页面的内容单位(这是一个很大的余地)。该示例名为 ClipPdf ,您可以在此处看到剪切页面: hero_clipped.pdf (iText超级英雄在裁剪过程中失去了手臂,脚和部分头部。)

I've written an example that clips the content of all pages of a PDF by introducing a margin of 200 user units (that's quite a margin). The example is called ClipPdf and you can see a clipped page here: hero_clipped.pdf (the iText superhero has lost arms, feet and part of his head in the clipping process.)

public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
    PdfReader reader = new PdfReader(src);
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
    int n = reader.getNumberOfPages();
    PdfDictionary page;
    PdfArray media;
    for (int p = 1; p <= n; p++) {
        page = reader.getPageN(p);
        media = page.getAsArray(PdfName.CROPBOX);
        if (media == null) {
            media = page.getAsArray(PdfName.MEDIABOX);
        }
        float llx = media.getAsNumber(0).floatValue() + 200;
        float lly = media.getAsNumber(1).floatValue() + 200;
        float w = media.getAsNumber(2).floatValue() - media.getAsNumber(0).floatValue() - 400;
        float h = media.getAsNumber(3).floatValue() - media.getAsNumber(1).floatValue() - 400;
        String command = String.format(
                "\nq %.2f %.2f %.2f %.2f re W n\nq\n",
                llx, lly, w, h);
        stamper.getUnderContent(p).setLiteral(command);
        stamper.getOverContent(p).setLiteral("\nQ\nQ\n");
    }
    stamper.close();
    reader.close();
}

显然,您需要在使用之前研究此代码。理解了这段代码后,您就会知道此代码仅适用于未旋转的页面。如果你能很好地理解代码,你可以毫不费力地调整旋转页面的例子。

Obviously, you need to study this code before using it. Once you understand this code, you'll know that this code will only work for pages that aren't rotated. If you understand the code well, you should have no problem adapting the example for rotated pages.

更新

re 运算符构造一个矩形。它需要四个参数(操作符之前的值)来定义一个矩形:左下角的x坐标,左下角的y坐标,宽度和高度。

The re operator constructs a rectangle. It takes four parameters (the values preceding the operator) that define a rectangle: the x coordinate of the lower-left corner, the y coordinate of the lower-left corner, the width and the height.

W 运算符设置剪切路径。我们刚画了一个矩形;此矩形将用于剪辑后面的内容。

The W operator sets the clipping path. We have just drawn a rectangle; this rectangle will be used to clip the content that follows.

n 运算符将启动一个新路径。它丢弃了我们迄今为止构建的路径。在这种情况下,它可以防止我们绘制的矩形(以及我们用作剪切路径)实际绘制。

The n operator starts a new path. It discards the paths we've constructed so far. In this case, it prevents that the rectangle we have drawn (and that we use as clipping path) is actually drawn.

q Q 运算符保存并恢复图形状态堆栈,但这很明显。

The q and Q operators save and restore the graphics state stack, but that's rather obvious.

全部ISO-32000-1(如果你谷歌可在线获得)和 PDF的ABC

All of this is explained in ISO-32000-1 (available online if you Google well) and in the book The ABC of PDF.

这篇关于iText - 裁剪出pdf文件的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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