iText - 在所有元素的顶部插入叠加文本

时间:2022-02-20 21:11:07

Using the iText PDF library, is it possible to freely (like "position: absolute" in CSS) insert overlay text or other elements on page, so that it gets rendered on top of all existing elements?

使用iText PDF库,是否可以*地(如CSS中的“position:absolute”)在页面上插入叠加文本或其他元素,以便在所有现有元素之上进行渲染?

Thanks for all the help and tips in advance.

感谢所有的帮助和提示。

2 个解决方案

#1


Check out PDFStamper. It might be exactly what you're looking for.

查看PDFStamper。它可能正是您正在寻找的。

Here's the tutorial. http://itextdocs.lowagie.com/tutorial/general/copystamp/#pdfstamper

这是教程。 http://itextdocs.lowagie.com/tutorial/general/copystamp/#pdfstamper

#2


Yes, absolutely. Everything has to be absolutely positioned in PDF, so doing text flow, alignment, and so forth takes more work. iText does all that for you if you ask, but it's still more work.

是的,一点没错。一切都必须绝对定位于PDF中,因此进行文本流,对齐等需要更多的工作。如果你问的话,iText会为你做所有这些,但它还有更多工作要做。

As Suvesh pointed out, PdfStamper works great for making changes to an existing PDF. It's quite simple to add absolutely positioned text.

正如Suvesh指出的那样,PdfStamper非常适合对现有PDF进行更改。添加绝对定位的文本非常简单。

It's also possible to position text absolutely in a PDF you're building from scratch in iText.

也可以将文本绝对放在您正在iText中从头开始构建的PDF中。

In either case, you need to get a PdfContentByte. In PdfStamper, you call getOverContent(pageNum) or getUnderContent(pageNum). With a PdfWriter, you just call getOverContent() or getUnderContent().

在任何一种情况下,您都需要获得PdfContentByte。在PdfStamper中,您调用getOverContent(pageNum)或getUnderContent(pageNum)。使用PdfWriter,您只需调用getOverContent()或getUnderContent()。

With PdfWriter, you only need to get the over or under PdfContentByte once. Every time it starts a new page, it'll suck all the content out and reset the object, ready for the next page.

使用PdfWriter,您只需要获得一次或多于PdfContentByte。每次启动一个新页面时,它都会将所有内容都删除并重置该对象,为下一页做好准备。

OTOH, it doesn't start building pages until you call doc.close(), so you need to use a PdfPageEvent (I recommend deriving from PdfPageEventHelper) to write on the pages as they're generated... not a problem you'll have with a PdfStamper.

OTOH,它在你调用doc.close()之前不会开始构建页面,所以你需要使用PdfPageEvent(我建议从PdfPageEventHelper派生)在页面生成时写入...不是问题你'我将使用PdfStamper。

#1


Check out PDFStamper. It might be exactly what you're looking for.

查看PDFStamper。它可能正是您正在寻找的。

Here's the tutorial. http://itextdocs.lowagie.com/tutorial/general/copystamp/#pdfstamper

这是教程。 http://itextdocs.lowagie.com/tutorial/general/copystamp/#pdfstamper

#2


Yes, absolutely. Everything has to be absolutely positioned in PDF, so doing text flow, alignment, and so forth takes more work. iText does all that for you if you ask, but it's still more work.

是的,一点没错。一切都必须绝对定位于PDF中,因此进行文本流,对齐等需要更多的工作。如果你问的话,iText会为你做所有这些,但它还有更多工作要做。

As Suvesh pointed out, PdfStamper works great for making changes to an existing PDF. It's quite simple to add absolutely positioned text.

正如Suvesh指出的那样,PdfStamper非常适合对现有PDF进行更改。添加绝对定位的文本非常简单。

It's also possible to position text absolutely in a PDF you're building from scratch in iText.

也可以将文本绝对放在您正在iText中从头开始构建的PDF中。

In either case, you need to get a PdfContentByte. In PdfStamper, you call getOverContent(pageNum) or getUnderContent(pageNum). With a PdfWriter, you just call getOverContent() or getUnderContent().

在任何一种情况下,您都需要获得PdfContentByte。在PdfStamper中,您调用getOverContent(pageNum)或getUnderContent(pageNum)。使用PdfWriter,您只需调用getOverContent()或getUnderContent()。

With PdfWriter, you only need to get the over or under PdfContentByte once. Every time it starts a new page, it'll suck all the content out and reset the object, ready for the next page.

使用PdfWriter,您只需要获得一次或多于PdfContentByte。每次启动一个新页面时,它都会将所有内容都删除并重置该对象,为下一页做好准备。

OTOH, it doesn't start building pages until you call doc.close(), so you need to use a PdfPageEvent (I recommend deriving from PdfPageEventHelper) to write on the pages as they're generated... not a problem you'll have with a PdfStamper.

OTOH,它在你调用doc.close()之前不会开始构建页面,所以你需要使用PdfPageEvent(我建议从PdfPageEventHelper派生)在页面生成时写入...不是问题你'我将使用PdfStamper。