I need to build a table to which I'm adding all the content dynamically by iterating over a list of records.
Every record corresponds with a row in a
PdfPTable and finally I add this
PdfPTable to the
When the table is split, I need to add a header on the next page that explains that this is the next page of
a table that started on the previous page. Unfortunately, the
writer nor the
document give me a correct page number
if my content moves on to the next page until the full table content is added to the document.
As a result, I don't know when the content is moved on to next page during iteration process.
PdfWriter is only showing me the page number before entering the loop.
I need to get the updated page number whenever the table's content goes on to next page.
I don't need this header to be on all pages. I need it only when the table is split and rows are carried over to the next page.
I have written an example that produces something like this:
As you can see, there is no header on the first page, there is only a header on the second and third page that says "Table XYZ (Continued)". I even added a footer that says "Continue on next page". As you can see, this footer only appears at the moment the table is split. It isn't present on the last page. This wasn't asked in the question, and it is easy to remove from my code.
You can find the complete code sample here: SimpleTable5. Note that in iText 7 we use
Table instead of
Cell instead of
These are the relevant parts:
Table table = new Table(5); Cell cell = new Cell(1, 5).add("Table XYZ (Continued)"); table.addHeaderCell(cell); cell = new Cell(1, 5).add("Continue on next page"); table.addFooterCell(cell); table.setSkipFirstHeader(true); table.setSkipLastFooter(true); for (int i = 0; i
If you only want a header:
Table table = new Table(5); Cell cell = new Cell(1, 5).add("Table XYZ (Continued)"); table.addHeaderCell(cell); table.setSkipFirstHeader(true); for (int i = 0; i
The difference with your code, is that I add the following line to avoid that the header appears on the first page:
Click this link if you want to see how to answer this question in iText 5.