我会自由地承认这可能是this的副本,但是那里没有答案,我想我可以添加更多信息 .
使用iText 5.5.0
我需要的是:斑马条纹表格,单元格之间没有顶部/底部边框,但表格本身有一个底部边框,或者表格被分割成多个页面 . 我在测试运行中使用"lorem ipsum"和其他任意数据进行了一个小小的剪辑 . 我部分切断了显示"Page 1 of 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个 Headers 行:第1页上有25行符合 Headers ,最后5行在第2页上 . 调试器告诉我 table.getLastCompletedRowIndex()
的计算结果为32,而不是预期的27第1页末尾 .
实际上,保存到我的文件的最终结果在第2页的最后一行有一个底部边框,但在第1页没有 . 我们在添加PdfPTableEventForwarder之前都没有边框 .
1 回答
如果您有一个包含10行的表并且您拆分了一行,则总共有11行 . 这解释了你对行数的困惑 .
我不明白为什么在只需要一个事件时使用
PdfPTableEventForwarder
. 当您有一系列PdfPTable
事件时,将使用PdfPTableEventForwarder
.更改表或单元格事件中的单元格是 not correct . 这将 never 工作 . 触发事件时,单元格已经被渲染 . 如果要绘制底部边框,请使用
PdfPTableEvent
实现的tableLayout()
方法中传递给您的坐标,按照lineTo()
,moveTo()
和stroke()
命令的顺序绘制底部边框 .一个与您需要的示例不同的示例,但在这里可以找到类似的方式:PressPreviews.java . 不需要拆分之前或之后,您只需要基本的
PdfPTableEvent
接口和一个看起来像这样的tableLayout()
方法 .关于
y
值,我可能会弄错,但我希望你能得到一般的想法 .