How to prevent splitting a table?

Tags: tablessplit tableiText 7

I'm developing an app that generates PDF using iText. I have a table that has 3 rows and when this table is near the end of a page, it sometimes puts one row on one page and two rows on the next page. Is there a way to force this table to start on the next page so I can have the full table on the next page?

Posted on StackOverflow on Oct 22, 2015 by user3392572

This was the original answer (the better solution follows):

Please take a look at the Splitting example:

Paragraph p = new Paragraph("Test");
Table table = new Table(2);
for (int i = 1; i < 6; i++) {
    table.addCell("key " + i);
    table.addCell("value " + i);
for (int i = 0; i < 40; i++) {

We have a table with 5 rows, and in this case, we're adding some paragraphs so that the table is added at the end of a page.

By default, iText will try not to split rows, but if the full table doesn't fit, it will forward the rows that don't fit to the next page:

The table splits
The table splits

You want to avoid this behavior: you don't want the table to split.

Knowing that iText will try to keep full rows intact, you can work around this problem by determine you don' want to split the table:


Now you get this result:

The table doesn't split
The table doesn't split

There was sufficient space on the previous page to render a couple of rows, but iText will forward the complete table to the next page. This does not work when the table is too large to fit on the new page and still needs to be split, e.g. when adding 50 instead of 5 rows in the example above.

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