繁体   English   中英

iText-避免最后一行不剪切页面上的表并拆分到下一页

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

我正在使用Java开发itext 5。 我的页面带有带有动态行的多个表。 在某些情况下,表格的最后一行与以下标题一起拆分为下一页。 我正在使用setHeaderRows()setSkipFirstHeader()来管理下一页的继续。 最后一行具有足够的空间以适合较早的页面。 我想将最后一行放在同一页而不是下一页中。

例如,在第1页上,最后一行被拆分为下一页的第一行。 取而代之的是,我想将页面1中的该行填满,因此多保留一页空白。

我尝试使用setExtendLastRow() ,但是它不起作用。 有谁知道如何解决此问题。 我正在附上一个有效的示例代码。

public class ProposalItextSplitLastRow {
 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("C:/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.setWidthPercentage(100);

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

            document.add(table);
        }

        document.close();
    } catch (Exception de) {
        de.printStackTrace();
    }
}

private static void add(PdfPTable table, String text, BaseColor color, int row) {
    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(2f);
    cell.setBorder(Rectangle.NO_BORDER);
    cell.setColspan(table.getNumberOfColumns());
    table.addCell(cell);
}
}

您可以table.setKeepRowsTogather(true);

table.setHeaderRows(1)以及它

setKeepRowsTogather()检查它是否可以将所有行保留在页面中,但如果表跨越多个页面,则将行拆分。 在这种情况下, setHeaderRows(1)会将标题行再次放在下一页中。

我必须执行示例以了解您的问题。 您通过谈论不是标题的标题而使我感到困惑(带有“ Header Row normal”的行不是标题行!),并且您对setExtendLastRow()引用也无济于事(提及该方法没有用对我来说很有意义;这非常令人困惑)。

话虽如此,您的问题的解决方案是轻而易举的。 我已经重写了主要课程:

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

确保定义总宽度而不是宽度百分比 ,然后锁定宽度。 如文档所述( 常识告诉您),如果您定义宽度百分比,则PdfPTable对象不知道其实际宽度。 不用说,您无法计算不知道其实际宽度的桌子的高度。

然后使用getVerticalPosition()方法获取光标的当前位置,并检查前两行是否适合页面。 如果他们没有在添加表格之前转到新页面。 如果要检查完整的表是否适合,请使用getTotalHeight()方法而不是getRowHeight()方法。

你可以做

table.setSplitRows(false);

但是我相信,当有行不适合时,就不会显示出来。 虽然值得一试

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM