java iText - 将内容添加到现有页面的底部

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13465657/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 12:51:34  来源:igfitidea点击:

iText - add content to the bottom of an existing page

javaitext

提问by janenz00

I want to add a piece of text to every page of a PDF file. This answer in SOworks fine. But, the text is added to the top of the page. I would like to add my text to the bottom of each page. How do I do this?

我想向 PDF 文件的每一页添加一段文本。SO中的这个答案工作正常。但是,文本被添加到页面顶部。我想将我的文字添加到每个页面的底部。我该怎么做呢?

Here is the relevant part of the code.

这是代码的相关部分。

    while (iteratorPDFReader.hasNext()) {
        PdfReader pdfReader = iteratorPDFReader.next();

        // Create a new page in the target for each source page.
        while (pageOfCurrentReaderPDF < pdfReader.getNumberOfPages()) {
            document.newPage();
            pageOfCurrentReaderPDF++;
            currentPageNumber++;
            page = writer.getImportedPage(pdfReader, pageOfCurrentReaderPDF);
            cb.addTemplate(page, 0, 0);

            document.add(new Paragraph("My Text here"));  //As per the SO answer

        }
        pageOfCurrentReaderPDF = 0;
    }

The code is part of a function which accepts a folder, reads the PDF files in it and merges them into one single file. So, I would like to add the text in the above loop itself, instead of iterating the file once again.

该代码是接受文件夹、读取其中的 PDF 文件并将它们合并为一个文件的函数的一部分。所以,我想在上面的循环中添加文本,而不是再次迭代文件。

回答by Bruno Lowagie

If you want to automatically add content to every page, you need a page event. This is explained in chapter 5 of my book" iText in Action - Second Edition". If you don't own a copy of the book, you can consult the examples here. You can also find solutions by looking for the keyword Header / Footer.

如果要自动向每个页面添加内容,则需要一个页面事件。这在我的书“ iText in Action - Second Edition”的第 5 章中进行了解释。如果您没有本书的副本,您可以在此处查阅示例。您还可以通过查找关键字Header / Footer来找到解决方案。

The example you're referring to doesn't look correct at first sight. Sure, you can use "two passes", one to create the content, and another to add headers or footers, but the suggested solution is different from the recommended solution: http://itextpdf.com/examples/iia.php?id=118

您所指的示例乍一看并不正确。当然,您可以使用“两遍”,一个创建内容,另一个添加页眉或页脚,但建议的解决方案与推荐的解决方案不同:http: //itextpdf.com/examples/iia.php?id =118

You are copying the mistake in your question: why on earth would you import the document you've just created into a new document, thus throwing away all possible interactivity you've added to that document? It just doesn't make sense. It's unbelievable that this answer received that many up-votes. I'm the original developer of iText and I'm not at all happy with that answer!

您正在复制问题中的错误:到底为什么要将刚刚创建的文档导入到新文档中,从而丢弃您添加到该文档中的所有可能的交互性?它只是没有意义。令人难以置信的是,这个答案得到了如此多的赞成票。我是 iText 的原始开发人员,我对这个答案一点也不满意!

In your case, there may be no need to create the document in memory first and to add the footer afterwards. Just take a look at http://itextpdf.com/examples/iia.php?id=104

在您的情况下,可能无需先在内存中创建文档,然后再添加页脚。看看http://itextpdf.com/examples/iia.php?id=104

You need to create a PdfPageEventimplementation (for instance using PdfPageEventHelper) and you need to implement the onEndPage()method.

您需要创建一个PdfPageEvent实现(例如 using PdfPageEventHelper)并且您需要实现该onEndPage()方法。

Documented caveats:

记录的警告:

  • Do not use onStartPage()to add content,
  • Do not add anything to the Documentobject passed to the page event,
  • Unless you specified a different page size, the lower-left corner has the coordinate x = 0; y = 0. You need to take that into account when adding the footer. The y-value for the footer is lower than the y-value for the header.
  • 不要onStartPage()用来添加内容,
  • 不要向Document传递给页面事件的对象添加任何内容,
  • 除非您指定了不同的页面大小,否则左下角的坐标为x = 0; y = 0。添加页脚时需要考虑到这一点。页脚的 y 值低于页眉的 y 值。

For more info: consult my book.

欲了解更多信息:请参阅我的书。

回答by mkl

Have a look at chapter 6of iText in Action, 2nd edition, especially at subsection 6.4.1: Concatenating and splitting PDF documents.

查看iText in Action,第 2 版的第 6 章,尤其是第 6.4.1 小节:连接和拆分 PDF 文档。

Listing 6.22, ConcatenateStamp.java, shows you how you should create a PDF from copies of pages (in your case: all pages) of multiple other PDFs; the sample additionally adds a new "Page X of Y" footer; this demonstrates how you can add content at given positions on the pages while merging the source files.

代码清单 6.22 ConcatenateStamp.java向您展示了如何从多个其他 PDF 的页面(在您的情况下:所有页面)的副本创建 PDF;该示例还添加了一个新的“Page X of Y”页脚;这演示了如何在合并源文件时在页面上的给定位置添加内容。

回答by hd1

Perhaps thismay be of assistance here... I suspect you want to do something like the following:

也许可能对这里有帮助......我怀疑你想做如下事情:

cb.addTemplate(page, 0, 0);

document.add(new Paragraph("My Text here"));  
document.setFooter(new HeaderFooter("Footnote goes here"));
}
pageOfCurrentReaderPDF = 0;