Large table in table cell invoke page break

前端 未结 1 528
后悔当初
后悔当初 2020-12-22 11:45

I have single PdfPTable with single column. One page fit 50 rows. If I add some text data to table (for an example, 300 rows), report work fine. When I add a PdfPTable into

相关标签:
1条回答
  • 2020-12-22 12:06

    Please take a look at the NestedTables2 example. In this example, we tell iText that it's OK to split cells as soon as a row doesn't fit on a page:

    enter image description here

    This is not the default: by default, iText will try to keep a row intact by forwarding it to the next page. Only if the row doesn't fit the page, the row will be split.

    Changing the default involves adding a single line to your code. That line is called: table.setSplitLate(false);

    This is the full method that created the PDF shown in the screen shot:

    public void createPdf(String dest) throws IOException, DocumentException {
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        PdfPTable table = new PdfPTable(2);
        table.setSplitLate(false);
        table.setWidths(new int[]{1, 15});
        for (int i = 1; i <= 20; i++) {
            table.addCell(String.valueOf(i));
            table.addCell("It is not smart to use iText 2.1.7!");
        }
        PdfPTable innertable = new PdfPTable(2);
        innertable.setWidths(new int[]{1, 15});
        for (int i = 0; i < 90; i++) {
            innertable.addCell(String.valueOf(i + 1));
            innertable.addCell("Upgrade if you're a professional developer!");
        }
        table.addCell("21");
        table.addCell(innertable);
        for (int i = 22; i <= 40; i++) {
            table.addCell(String.valueOf(i));
            table.addCell("It is not smart to use iText 2.1.7!");
        }
        document.add(table);
        document.close();
    }
    

    Note that you should not use iText 2.1.7. If you do, you could have a problem with your employer, customer, investor. Read more about this in the legal section of the free ebook The Best iText Questions on StackOverflow

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