iText - avoid last row not to cut tables on page split to next page

前端 未结 3 2145
暖寄归人
暖寄归人 2020-12-10 05:45

I am working on itext 5 using java. I have pages with mutiple tables with dynamic rows. In some instances, the table last row is splitted into next page with the folowing he

相关标签:
3条回答
  • 2020-12-10 05:53

    I had to execute the example to understand your question. You confused me by talking about a header that isn't a header (the rows with "Header Row normal" aren't header rows!) and your reference to setExtendLastRow() didn't help either (mentioning that method doesn't make sense to me; it's very confusing).

    This being said, the solution to your problem is a no-brainer. I've rewritten the main class:

    public static void main(String[] args) {
        try {
            Document document = new Document();
            document.setPageSize(PageSize.LETTER);
            document.setMargins(16, 14, 14, 14);
            PdfWriter writer = PdfWriter.getInstance(document,
                    new FileOutputStream("SplitLastRow.pdf"));
            document.open();
            document.setPageSize(PageSize.LETTER);
            document.setMargins(16, 14, 42, 38);
    
            for (int m = 1; m < 20; m++) {
    
                int row = 0;
                PdfPTable table = new PdfPTable(1);
                table.setSpacingAfter(0);
                table.setSpacingBefore(0);
                table.setTotalWidth(document.right() - document.left());
                table.setLockedWidth(true);
    
                table.setHeaderRows(1);
                table.setSkipFirstHeader(true);
                add(table, "Header Row continued " + m, BaseColor.LIGHT_GRAY, row++);
                add(table, "Header Row normal " + m, BaseColor.LIGHT_GRAY, row++);
    
                add(table, "Text Row 1 ", BaseColor.WHITE, row++);
                add(table, "Text Row 2 ", BaseColor.WHITE, row++);
                add(table, "Text Row 3 ", BaseColor.WHITE, row++);
    
                addPadding(table);
                if (writer.getVerticalPosition(true) - table.getRowHeight(0) - table.getRowHeight(1) < document.bottom()) {
                    document.newPage();
                }
                document.add(table);
            }
    
            document.close();
        } catch (Exception de) {
            de.printStackTrace();
        }
    }
    

    Make sure you define a total width instead of a width percentage, and lock the width. As documented (and as common sense tells you), a PdfPTable object doesn't know its actual width if you define a width percentage. It goes without saying that you can't calculate the height of a table that doesn't know it's actual width.

    Then use getVerticalPosition() method to get the current position of the cursor, and check if the first two rows fit on the page. If they don't go to a new page before adding the table. If you want to check if the complete table fits, use the getTotalHeight() method instead of the getRowHeight() method.

    0 讨论(0)
  • 2020-12-10 06:00

    You can do

    table.setSplitRows(false);
    

    But I believe that when there is a row that wont fit it just wont be shown. It's worth a shot though

    0 讨论(0)
  • 2020-12-10 06:09

    you can table.setKeepRowsTogather(true);

    table.setHeaderRows(1) as well alongwith it

    setKeepRowsTogather() checks if it can keep all the rows in page but splits the rows in case the table spans multiple pages. In that case setHeaderRows(1) will put the header rows again in the next page.

    0 讨论(0)
提交回复
热议问题