【问题标题】:iText PdfPTableEventForwarder not getting called when expected,iText PdfPTableEventForwarder 在预期时没有被调用,
【发布时间】:2014-08-28 13:51:15
【问题描述】:

我承认这可能与this 重复,但那里没有答案,我想我可以添加更多信息。

使用 iText 5.5.0

我需要什么:一个斑马条纹表格,其中没有单元格之间的顶部/底部边框,但表格本身有一个底部边框,或者当表格被拆分为多个时页。我在使用“lorem ipsum”和其他任意数据的测试运行中有一个小片段。我部分切断了“第 1 页,共 2 页”的页脚,但这张表确实 在第 2 页上继续添加其他行。我希望此表看起来或多或少,但在页面的最后一行添加了底部边框。

我正在尝试通过匿名内部类实现 PdfPTableEventForwarder。我有一个看起来像这样的方法:

public PdfPTable createStandardTable(int columnCount, int headerRows) {
    PdfPTableEventForwarder tableEvent = new PdfPTableEventForwarder()
    {
        // begin another anonymous inner class extends PdfPTableEventForwarder
        @Override
        public void splitTable(PdfPTable table) {
            PdfPRow lastRow = table.getRow(table.getLastCompletedRowIndex());
            for (PdfPCell cell : lastRow.getCells()) {
                cell.setBorder(Rectangle.LEFT + Rectangle.RIGHT + Rectangle.BOTTOM);
            }
        }
        // end anonymous inner class extends PdfPTableEventForwarder
    };

    PdfPTable table = new PdfPTable(columnCount);
    table.setSpacingBefore(TABLE_SPACING);
    table.setSpacingAfter(TABLE_SPACING);
    table.setWidthPercentage(TABLE_WIDTH_PERCENT);
    table.setHeaderRows(headerRows);
    table.setTableEvent(tableEvent);
    return table;
}

在其他地方我像这样创建我的表:

// Details of code to create document and headers not shown
PdfPTable table = createStandardTable(12, 2);
// Details of code to build table not shown, but includes cell.setBorder(Rectangle.LEFT + Rectangle.RIGHT)
document.add(table);

我在调试器中运行了这个,在splitTable 的第一行有一个断点,发现该事件只被调用一次。我希望它会调用两次:第一次是在第 1 页结束和第 2 页开始时,第二次是在表格完成时。此外,我在这个表中有 30 行加上 2 个标题行:25 行适合第 1 页的标题,最后 5 行在第 2 页。调试器告诉我table.getLastCompletedRowIndex() 计算结果为 32,而不是第 1 页末尾预期的 27。

确实,保存到我的文件的最终结果在第 2 页的最后一行有一个底部边框,但在第 1 页没有一个。在添加 PdfPTableEventForwarder 之前,两者都没有边框。

【问题讨论】:

    标签: java pdf-generation itext


    【解决方案1】:
    • 当您有一个包含 10 行的表并拆分一行时,您的总行数为 11 行。这解释了您对行数的困惑。
    • 我不明白为什么你只需要一个事件时使用PdfPTableEventForwarder。当您有一系列PdfPTable 事件时使用PdfPTableEventForwarder
    • 更改表格或单元格事件中的单元格不正确。这将永远不会起作用。触发事件时,单元格已被渲染。如果要绘制底部边框,请使用PdfPTableEvent 实现的tableLayout() 方法中传递给您的坐标,按照lineTo()moveTo()stroke() 命令的顺序绘制底部边框。李>

    可以在此处找到与您需要的示例不同但方式相似的示例:PressPreviews.java。不需要拆分之前或之后,您只需要基本的PdfPTableEvent 接口和一个看起来像这样的tableLayout() 方法。

    public void tableLayout(PdfPTable table, float[][] width, float[] height,
            int headerRows, int rowStart, PdfContentByte[] canvas) {
        float widths[] = width[0];
        float x1 = widths[0];
        float x2 = widths[widths.length - 1];
        float y = height[height.length - 1];
        PdfContentByte cb = canvas[PdfPTable.LINECANVAS];
        cb.moveTo(x1, y);
        cb.lineTo(x2, y);
        cb.stroke();
    }
    

    关于y 的值,我可能会弄错,但我希望您能大致了解。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-23
      • 1970-01-01
      • 1970-01-01
      • 2013-11-10
      • 1970-01-01
      • 2019-08-24
      • 2021-03-06
      • 2020-02-10
      相关资源
      最近更新 更多