首页 > 解决方案 > 针对特定表格布局裁剪单元格数据。iText 2.1.7

问题描述

对于具有较大数据的特定单元格的特定表格布局,它无法正确导出为 PDF,该特定单元格中的数据被裁剪。

private static void tableLayout1() {
    Document doc = new Document();

    try {
        PdfWriter.getInstance(doc, new FileOutputStream(
                ".\\TableLayout_1.pdf"));
        doc.open();

        // Table
        PdfPTable table = new PdfPTable(3);
        table.setWidthPercentage(100f);
        table.setSplitLate(false);

        //row 1
        PdfPCell cell00 = new PdfPCell(new Paragraph("Cell00"));
        cell00.setBackgroundColor(new Color(200, 0, 0));
        cell00.setRowspan(3);
        PdfPCell cell01 = new PdfPCell(new Paragraph("Cell01"));
        cell01.setBackgroundColor(new Color(0, 200, 0));
        cell01.setColspan(2);

        //row 2
        PdfPCell cell11 = new PdfPCell(new Paragraph("Cell11"));
        cell11.setBackgroundColor(new Color(100, 100, 0));
        PdfPCell cell12 = new PdfPCell( getLongCellData() );
        cell12.setBackgroundColor(new Color(0, 200, 200));
        cell12.setRowspan(2);

        //row 3
        PdfPCell cell21 = new PdfPCell(new Paragraph("Cell21"));
        cell21.setBackgroundColor(new Color(200, 0, 200));

        table.addCell(cell00);
        table.addCell(cell01);
        table.addCell(cell11);
        table.addCell(cell12);
        table.addCell(cell21);

        doc.add(table);


    } catch (FileNotFoundException | DocumentException e) {
        e.printStackTrace();
    }
    doc.close();

}

private static Paragraph getLongCellData() {
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < 100; i++) {
        sb.append("Cell12_" + i);
        sb.append("\n");
    }

    return new Paragraph(sb.toString());
}

此处来自 cell12 的数据(其行跨度为 2 并且具有更大的数据)在页面末尾被裁剪,并且不会在下一页继续。尝试将此表分为两个不同的表,如下所示,但问题仍然存在。

private static void tableLayout2() {
    Document doc = new Document();

    try {
        PdfWriter.getInstance(doc, new FileOutputStream(
                ".\\TableLayout_2.pdf"));
        doc.open();

        // Table
        PdfPTable table = new PdfPTable(2);
        table.setWidthPercentage(100f);
        table.setSplitLate(false);
        table.setWidths(new int[]{1, 2});


        //Inner table for column 1
        PdfPTable column1Table = new PdfPTable(1);
        PdfPCell cell00 = new PdfPCell(new Paragraph("Cell00"));
        cell00.setBackgroundColor(new Color(200, 0, 0));
        //cell00.setRowspan(3);
        column1Table.addCell(cell00);


        //Inner table for column 2 
        PdfPTable column2Table = new PdfPTable(2);

        PdfPCell cell01 = new PdfPCell(new Paragraph("Cell01"));
        cell01.setBackgroundColor(new Color(0, 200, 0));
        cell01.setColspan(2);

        PdfPCell cell11 = new PdfPCell(new Paragraph("Cell11"));
        cell11.setBackgroundColor(new Color(100, 100, 0));
        PdfPCell cell12 = new PdfPCell( getLongCellData() );
        cell12.setBackgroundColor(new Color(0, 200, 200));
        cell12.setRowspan(2);

        PdfPCell cell21 = new PdfPCell(new Paragraph("Cell21"));
        cell21.setBackgroundColor(new Color(200, 0, 200));

        column2Table.addCell(cell01);
        column2Table.addCell(cell11);
        column2Table.addCell(cell12);
        column2Table.addCell(cell21);

        table.addCell(column1Table);
        table.addCell(column2Table);

        doc.add(table);


    } catch (FileNotFoundException | DocumentException e) {
        e.printStackTrace();
    }
    doc.close();

}

这个问题在 iText5 上得到了部分解决,但是,由于某些问题,我们不太可能升级库,所以有什么解决方案吗?

使用 iText5,数据不会被裁剪,但单元格的高度不太合适。对于 TableLayout_2_withIText5.pdf,cell21 应该在 page3 而不是 page4 上呈现。对于 TableLayout_1_withIText5.pdf,cell21 在 Page1 的边距区域中得到扩展。

标签: javaitexttablelayout

解决方案


推荐阅读