Skip to main content
Skip table of contents

How to prevent splitting a table?

How to ensure the entire table goes on the same page.

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

Please take a look at the Splitting example:

JAVA
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++) {
    doc.add(p);
}
doc.add(table);

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 determining you don’t want to split the table:

JAVA
table.setKeepTogether(true);

Now you get this result:

The table doesn

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 How to prevent splitting a table? 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.