iText PDFDocument page size inaccurate

后端 未结 1 1252
有刺的猬
有刺的猬 2021-01-17 06:24

I am trying to add a header to existing pdf documents in Java with iText. I can add the header at a fixed place on the document, but all the documents are different page siz

相关标签:
1条回答
  • 2021-01-17 07:07

    Take a look at the StampHeader1 example. I adapted your code, introducing ColumnText.showTextAligned() and using a Phrase for the sake of simplicity (maybe you can change that part of your code too):

    public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
        PdfReader reader = new PdfReader(src);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
        Phrase header = new Phrase("Copy", new Font(FontFamily.HELVETICA, 14));
        for (int i = 1; i <= reader.getNumberOfPages(); i++) {
            float x = reader.getPageSize(i).getWidth() / 2;
            float y = reader.getPageSize(i).getTop(20);
            ColumnText.showTextAligned(
                stamper.getOverContent(i), Element.ALIGN_CENTER,
                header, x, y, 0);
        }
        stamper.close();
        reader.close();
    }
    

    As you have found out, this code assumes that no rotation was defined.

    Now take a look at the StampHeader2 example. I'm using your "Wrong" file and I've added one extra line:

    stamper.setRotateContents(false);
    

    By telling the stamper not to rotate the content I'm adding, I'm adding the content using the coordinates as if the page isn't rotated. Please take a look at the result: stamped_header2.pdf. We added "Copy" at the top of the page, but as the page is rotated, we see the word appear on the side. The word is rotated because the page is rotated.

    Maybe that's what you want, maybe it isn't. If it isn't, please take a look at StampHeader3 in which I calculate x and y differently, based on the rotation of the page:

    if (reader.getPageRotation(i) % 180 == 0) {
        x = reader.getPageSize(i).getWidth() / 2;
        y = reader.getPageSize(i).getTop(20);
    }
    else {
        x = reader.getPageSize(i).getHeight() / 2;
        y = reader.getPageSize(i).getRight(20);
    }
    

    Now the word "Copy" appears on what is perceived as the "top of the page" (but in reality, it could be the side of the page): stamped_header3.pdf

    0 讨论(0)
提交回复
热议问题