iText - crop out a part of pdf file

不想你离开。 提交于 2019-12-06 11:11:02

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.

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.

Update

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.

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

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.

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

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

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