【问题标题】:pdfptable header repeat when data in a different table increases in itext当不同表中的数据在 itext 中增加时,pdfptable 标题重复
【发布时间】:2015-06-29 05:36:57
【问题描述】:

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

提前致谢。

【问题讨论】:

    标签: java pdf-generation itext


    【解决方案1】:

    实现此目的的唯一方法是使用表格事件。示例见NestedTables3nested_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) {
                }
            }
        }
    }
    

    你必须在你的桌子上声明这个事件:

    PdfPTable table = new PdfPTable(2);
    table.setTableEvent(new MyPdfPTableEvent());
    

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

    【讨论】:

    • 谢谢! Bruno Lowagie 先生,这个例子似乎解决了我的问题。如果我解决了我的问题,会通知你。
    • 谢谢! Bruno Lowagie 先生,您的示例解决了我的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多