为特定的表格布局裁剪了单元格数据。 iText 2.1.7

时间:2019-03-07 06:07:41

标签: java itext tablelayout

对于具有较大数据的特定单元格的特定表格布局,则无法正确导出到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());
}

此处,单元格12中的数据(行跨度为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的空白区域中得到了扩展。

0 个答案:

没有答案