How to split a row over multiple pages?

Category: 
Tags: tablessplit tablesplit rowiText 7

I am using PdfPTable to create a table in PDF. I have a single row in the table. In my row, the last column has data which has a height that needs more space than remaining height of the page. So the row is getting started from the next page while the table headers are on the previous page and there is large blank space below the header on the first page. Can anybody suggest how I can split the row over multiple pages?

Posted on StackOverflow on Feb 27, 2014 by Manish

In iText 7, table rows are split by default. Suppose that you have such a String:

String txt = “Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas porttitor congue massa. Fusce posuere, magna sed pulvinar ultricies, purus lectus malesuada libero, sit amet commodo magna eros quis urna. Nunc viverra imperdiet enim. Fusce est. Vivamus a tellus. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Proin pharetra nonummy pede. Mauris et orci. Aenean nec lorem. In porttitor. Donec laoreet nonummy augue.”

Now if you use this code:

PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
pdfDoc.setDefaultPageSize(PageSize.A6);
Document doc = new Document(pdfDoc);
Table table = new Table(2);
table.addHeaderCell(new Cell().add(new Paragraph("Test")));
table.addHeaderCell(new Cell().add(new Paragraph("Header")));
table.addCell(new Cell().add("Test"));
Cell cell = new Cell().add(new Paragraph(txt));
table.addCell(cell);
doc.add(table);
doc.close();

The beginning of the content will appear at the first page and it will be continued at the second page:

First page
First page
Second page
Second page

See also HeaderFooter2 example that faces with the same problem.

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