Skip to main content
Skip table of contents

How to add "continue on next page" / "continued from next page" to a table?

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 Document.

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.

Posted on StackOverflow on Nov 28, 2012 by degr

I have written an example that produces something like this:

Page 1

Page 1

Page 2

Page 2

Page 3

Page 3

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 PdfPTable and Cell instead of PdfPCell.

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 < 350; i++) {
    table.addCell(String.valueOf(i + 1));
}
doc.add(table);

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 < 350; i++) {
    table.addCell(String.valueOf(i + 1));
}
doc.add(table);

The difference with your code, is that I add the following line to avoid that the header appears on the first page:

table.setSkipFirstHeader(true);

Click this link if you want to see how to answer this question in iText 5.

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.