iText - 避免第一行在页面拆分时剪切表格

时间:2013-05-14 16:12:48

标签: java itext

我正在使用包含许多表的itext 5。每个表都有许多行具有不同的单元格高度。当表格第一行不适合页面的其余部分时,iText会拆分表格,最后一行页面显示标题行,下一页面的第一行显示第二行标题行。

我希望只有当表的第一行不适合页面的其余部分时,才将其放在新页面上。该表应该能够在第一行以外的任何行上拆分。 我用setSplitRows,setSplitLate& keepRowsTogether,但没用。

public class ProposalItextSplitRow {

public ProposalItextSplitRow() {
}

public static void main(String[] args) {
    try {
        String methodName = Thread.currentThread().getStackTrace()[2].getMethodName();
        Document document = new Document();
        document.setPageSize(PageSize.LETTER);
        document.setMargins(16, 14, 14, 14);
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("C:/Test.pdf"));
        document.open();
        document.setPageSize(PageSize.LETTER);
        document.setMargins(16, 14, 42, 38);

        for (int m = 1; m < 20; m++) {

            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);
            add(table, "Header Row normal " + m, BaseColor.LIGHT_GRAY);

            add(table, "Text Row 1 ", BaseColor.WHITE);
            add(table, "Text Row 2 ", BaseColor.WHITE);
            add(table, "Text Row 3 ", BaseColor.WHITE);

            addPadding(table);

            document.add(table);
        }

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

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

}

0 个答案:

没有答案