Need to start new Table on next Page if not enough space left on current Page
I have the same issue described in #45, in which I don't want to draw a table on the current page, if there is not enough room for it on that page. I tried the code posted there, but it didn't compile for me, because the pageBreak() method in Table is private. I came up with something that works, but it seems complicated. Please let me know if this is the right way to do this. I am running 1.5-RC.
private void drawTable(TableBuilder builder, List<String[]> rowLines, boolean newPageIfWontFit) throws IOException { Table table = builder.buildTable(rowLines, page, startY); if (table.getCurrentPage() != page) { page = table.getCurrentPage(); } float bottomMargin = 70; float remainingSize = startY - bottomMargin; float tableSize = table.getHeaderAndDataHeight(); if (remainingSize < tableSize && newPageIfWontFit) { page = new PDPage(); doc.addPage(page); startY = page.getMediaBox().getHeight() - (2 * margin); table = builder.buildTable(rowLines, page, startY); if (table.getCurrentPage() != page) { page = table.getCurrentPage(); } } startY = table.draw(); startY-= leading; if (table.getCurrentPage() != page) { page = table.getCurrentPage(); } }
So, I build the table once just to see how big it is, and if it fits on the current page, it gets drawn. If it doesn't fit on the current page, that table is discarded, a new page is added, and the table is built again on the new page.
Is this the right approach? Would having my own PageProvider help here? Or will a method be exposed to let me tell the draw() method to start on a new page if it won't fit?
Pasting the code did not work, trying again.
private void drawTable(TableBuilder builder, List<String[]> rowLines, boolean newPageIfWontFit) throws IOException {
Table table = builder.buildTable(rowLines, page, startY);
if (table.getCurrentPage() != page) {}
page = table.getCurrentPage();
}
float bottomMargin = 70;
float remainingSize = startY - bottomMargin;
float tableSize = table.getHeaderAndDataHeight();
if (remainingSize < tableSize && newPageIfWontFit) {
page = new PDPage();
doc.addPage(page);
startY = page.getMediaBox().getHeight() - (2 * margin);
table = builder.buildTable(rowLines, page, startY);
if (table.getCurrentPage() != page) {
page = table.getCurrentPage();
}
}
startY = table.draw();
startY-= leading;
if (table.getCurrentPage() != page) {
page = table.getCurrentPage();
}
}
Hi @gstrickland , I think that is valid solution you got there due ''Table''s pagebreak() being private now. This should be maybe another feature for enabling manual page break for table.
@Frulenzo - can you provide sample code to page.break in table