How to write a table header if part of a table is forwarded to the next page?

How to put a table header row on subsequent pages.

23rd November 2015
admin-marketing

I am using PdfWriter to create a PDF document. I am adding a PdfPTable to the PDF document. This table has header row and then actual data rows. If the table is big, then part of it gets carried forward to new page. I want this page to have table header row as well. However, I want this header row only when the table data goes on new page.

Posted on StackOverflow on Sep 15, 2015 by Amit Ware

This is how you create a table with a header row:

// table with 2 columns:
PdfPTable table = new PdfPTable(2);
// header row:
table.addCell("Key");
table.addCell("Value");
table.setHeaderRows(1);
// many data rows:
for (int i = 1; i 

In this case, the table needs more than one page. As you used setHeaderRows() with 1 as parameter, the first row will be repeated:

Repeating header

Repeating header

If you don't want the header to be present on the first page, you have to add a single line: table.setSkipFirstHeader(true);

// table with 2 columns:
PdfPTable table = new PdfPTable(2);
// header row:
table.addCell("Key");
table.addCell("Value");
table.setHeaderRows(1);
table.setSkipFirstHeader(true);
// many data rows:
for (int i = 1; i 

Now the table looks like this:

Repeating header

Repeating header



Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now