Question

I have just spent a bit of time figuring out how to insert fillable fields into a PdfPTable using iText. Through reading the book and googling, I knew that fields should be added via direct content, and as I needed to add fields to specific cells in a table I was building, I should to use a cell event to do this.

In order to improve my understanding, I was hoping someone could confirm that the reason we do it in this way is because the PdfPCellEvent gives us a Rectangle which provides us the exact position of the table cell?

Apologies if this seems very obvious - my Java knowledge has been built up by tackling specific requirements rather than learning it in a structured way so is probably missing some very basic concepts.

Was it helpful?

Solution

PdfPTable/PdfPCell draw lines and text to the content stream of a PDF.

Fields aren't part of the content stream. A field is an abstract concept that can be visualized on a page by a widget annotation. Annotations aren't part of the content stream. They are stored in PDF dictionaries that are referred to from the page dictionary (as opposed to being present in a page's content stream). These dictionaries contain the coordinates of the annotation.

That's why you should use cell events to add a field to a table. When adding the table to a page, iText creates a content stream. iText doesn't know the coordinates of every line and text snippet up until the rendering is done. That's when the table and cell events are triggered. You can use the coordinates passed to the events to create annotations.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top