iText PDFDocument页面大小不准确 [英] iText PDFDocument page size inaccurate

查看:894
本文介绍了iText PDFDocument页面大小不准确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用iText为Java中的现有pdf文档添加标题。我可以在文档的固定位置添加标题,但所有文档都是不同的页面大小,因此它并不总是位于页面的顶部。我已经尝试获取页面大小,以便我可以计算标题的位置,但似乎页面大小实际上不是我想要的。在某些文档中,调用 reader.getPageSize(i).getTop(20)会将文本放在页面顶部的正确位置,但是,在某些不同的文档上它将它放在页面的一半。大多数页面都被扫描为Xerox复印机,如果这有所不同的话。这是我正在使用的代码:

  PdfReader reader = new PdfReader(readFilePath); 
PdfStamper stamper = new PdfStamper(reader,new FileOutputStream(writeFilePath));
BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA,BaseFont.CP1252,BaseFont.NOT_EMBEDDED);
for(int i = 1; i< = reader.getNumberOfPages(); i ++){
PdfContentByte cb = stamper.getOverContent(i);

cb.beginText();
cb.setFontAndSize(bf,14);
float x = reader.getPageSize(i).getWidth()/ 2;
float y = reader.getPageSize(i).getTop(20);
cb.showTextAligned(PdfContentByte.ALIGN_CENTER,Copy,x,y,0);
cb.endText();
}

stamper.close();

PDF工作正常



PDF工作不正确

解决方案

查看 StampHeader1 示例。为了简单起见,我修改了你的代码,引入了 ColumnText.showTextAligned()并使用短语(也许你可以也改变你的代码部分):

  public void manipulatePdf(String src,String dest)抛出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();
}

如您所知,此代码假定未定义轮换。<现在看看 StampHeader2 示例。我正在使用你的错误文件,我又增加了一行:

  stamper.setRotateContents(false); 

告诉压模不要旋转我正在添加的内容,我正在添加内容坐标就像页面没有旋转一样。请查看结果: stamped_header2.pdf 。我们在页面顶部添加了复制,但随着页面的旋转,我们看到单词出现在侧面。旋转这个词是因为页面被旋转了。



也许这就是你想要的,也许不是。如果不是,请查看 StampHeader3 根据页面的旋转,我以不同的方式计算 x y

  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);
}

现在,复制一词出现在被认为是顶部页面(但实际上,它可能是页面的一面): stamped_header3.pdf


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 sizes, so it is not always at the top of the page. I have tried getting the page size so that I could calculate the position of the header, but it seems as if the page size is not actually what I want. On some documents, calling reader.getPageSize(i).getTop(20) will place the text in the right place at the top of the page, however, on some different documents it will place it half way down the page. Most of the pages have been scanned be a Xerox copier, if that makes a difference. Here is the code I am using:

PdfReader reader = new PdfReader(readFilePath);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(writeFilePath));
BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
for (int i = 1; i <= reader.getNumberOfPages(); i++) {
    PdfContentByte cb = stamper.getOverContent(i);

    cb.beginText();
    cb.setFontAndSize(bf, 14);
    float x = reader.getPageSize(i).getWidth() / 2;
    float y = reader.getPageSize(i).getTop(20);
    cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "Copy", x, y, 0);
    cb.endText();
}

stamper.close();

PDF that works correctly

PDF that works incorrectly

解决方案

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

这篇关于iText PDFDocument页面大小不准确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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