I am working with itext 5 which has many tables. Each table has many rows with different cell heights. When a table first row doesn't fit on the rest of the page, iText splits the table and the last row of page is displayed with header row and the first row of the next page is displayed with second header row.
I want that only if first row of a table doesn't fit on the rest of the page, then place it on a new page. The table should be able to split on any row other than first row. I have tried this with setSplitRows, setSplitLate & keepRowsTogether, but no use.
public class ProposalItextSplitRow {
public ProposalItextSplitRow() {
}
public static void main(String[] args) {
try {
String methodName = Thread.currentThread().getStackTrace()[2].getMethodName();
Document document = new Document();
document.setPageSize(PageSize.LETTER);
document.setMargins(16, 14, 14, 14);
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("C:/Test.pdf"));
document.open();
document.setPageSize(PageSize.LETTER);
document.setMargins(16, 14, 42, 38);
for (int m = 1; m < 20; m++) {
PdfPTable table = new PdfPTable(1);
table.setSpacingAfter(0);
table.setSpacingBefore(0);
table.setWidthPercentage(100);
table.setHeaderRows(1);
table.setSkipFirstHeader(true);
add(table, "Header Row continued " + m, BaseColor.LIGHT_GRAY);
add(table, "Header Row normal " + m, BaseColor.LIGHT_GRAY);
add(table, "Text Row 1 ", BaseColor.WHITE);
add(table, "Text Row 2 ", BaseColor.WHITE);
add(table, "Text Row 3 ", BaseColor.WHITE);
addPadding(table);
document.add(table);
}
document.close();
} catch (Exception de) {
de.printStackTrace();
}
}
private static void add(PdfPTable table, String text, BaseColor color) {
PdfPCell pdfCellHeader = new PdfPCell();
pdfCellHeader.setBackgroundColor(color);
pdfCellHeader.addElement(new Paragraph(new Phrase(text)));
table.addCell(pdfCellHeader);
}
private static void addPadding(PdfPTable table) {
PdfPCell cell = new PdfPCell();
cell.setFixedHeight(4f);
cell.setBorder(Rectangle.NO_BORDER);
cell.setColspan(table.getNumberOfColumns());
table.addCell(cell);
}
}