We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Table overflows with multi pages in aspose pdf java

HI,

I have a table that overflows with multiple pages, how to handle the table headers automatically when we placing that component?

Basically I want to know, do we have any page events or handlers to do this kind of task?

Thanks,
Balakumar

@BalakumarSeethapathy

Thanks for contacting support.

In order to repeat Header/First Row of the table after page break, you need to use Table.setRepeatingRowsCount method, which specifies how many rows should be repeated. Please check following code snippet to achieve what you require:

Document doc = new Document();
// Add page to PDF document
doc.getPages().add();
// Instantiate a table object
Table table = new Table();
// Add the table in paragraphs collection of the desired section
doc.getPages().get_Item(1).getParagraphs().add(table);
// Set with column widths of the table
table.setColumnWidths("50 50 50");
// Set default cell border using BorderInfo object
table.setDefaultCellBorder(new BorderInfo(BorderSide.All, 0.1F));
// Set table border using another customized BorderInfo object
table.setBorder(new BorderInfo(BorderSide.All, 1F));

// Create MarginInfo object and set its left, bottom, right and top margins
MarginInfo margin = new MarginInfo();
margin.setLeft(5f);
margin.setRight(5f);
margin.setTop(5f);
margin.setBottom(5f);
// Set the default cell padding to the MarginInfo object
table.setDefaultCellPadding(margin);
// Set repeating rows count
table.setRepeatingRowsCount(1);
// Create rows in the table and then cells in the rows
Row row1 = table.getRows().add();
row1.setBackgroundColor(Color.getGreen());
row1.getCells().add("col1");
row1.getCells().add("col2");
row1.getCells().add("col3");
		
for (int i = 0; i < 100; i++){
Row row = table.getRows().add();
row.getCells().add("item " + i);
row.getCells().add("item " + i);
row.getCells().add("item " + i);
}
		
// Save the PDF document
doc.save(dataDir + "TableWithRepeatingHeader.pdf");

TableWithRepeatingHeader.pdf (9.6 KB)

For your reference, an output from the above code is also attached. In case of any further assistance, please feel free to let us know.

Thanks for your help!

Regards,
Balakumar