pdfptable header 当不同的 table 中的数据在 itext 中增加时重复

pdfptable header repeat when data in a different table increases in itext

在我的项目中,我需要在 itext.These 中创建一个包含两列的 pdfptable 两列包含两个不同的内部 table(第 1 列包含一个内部 table 并且第 2 列包含一个内部 table)。现在这两个内部 table 都设置了 header 行。我的问题是,当第 2 列内部 table 中的数据增加时,我需要重复第 1 列内部 table 的 header。 我已经创建了所有需要的 table。但是找不到什么好的解决办法 我知道这是一个奇怪的要求,但请尽可能提供帮助。

提前致谢。

实现此目的的唯一方法是使用 table 事件。有关示例,请参阅 NestedTables3 and nested_tables3.pdf

这是实现此目的所需的代码:

class MyPdfPTableEvent implements PdfPTableEvent {

    public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) {
    }

    public void tableLayout(PdfPTable table, float[][] widths, float[] heights, int headerRows, int rowStart, PdfContentByte[] canvases) {
        ColumnText ct = new ColumnText(canvases[PdfPTable.TEXTCANVAS]);
        for (int i = 1; i < widths[1].length; i++) {
            Rectangle position = new Rectangle(widths[1][i - 1], heights[1], widths[1][i], heights[2]);
            ct.setSimpleColumn(position);
            ct.addText(new Phrase("This inner table header will always be repeated"));
            try {
                ct.go();
            } catch (DocumentException ex) {
            }
        }
    }
}

您必须向您的 table:

声明此事件
PdfPTable table = new PdfPTable(2);
table.setTableEvent(new MyPdfPTableEvent());

当然:为此,您需要添加一个空单元格,以便为您在 table 事件中添加的文本提供足够的 space。