【发布时间】: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