boxable icon indicating copy to clipboard operation
boxable copied to clipboard

Need to start new Table on next Page if not enough space left on current Page

Open gstrickland opened this issue 8 years ago • 3 comments

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?

gstrickland avatar Mar 03 '17 12:03 gstrickland

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();
        }            
    }

gstrickland avatar Mar 03 '17 12:03 gstrickland

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 avatar Mar 04 '17 06:03 Frulenzo

@Frulenzo - can you provide sample code to page.break in table

poojabagade avatar Oct 05 '20 23:10 poojabagade