1
在itext 5中,可以使用单元格事件来计算表格列的小计。这怎么能在itext 7中完成?如何计算itext 7中表格列的总数和小计?
把itext 5的例子可以在以下网址找到:http://developers.itextpdf.com/examples/tables/using-cell-events-add-special-content#2887-subtotal.java
在itext 5中,可以使用单元格事件来计算表格列的小计。这怎么能在itext 7中完成?如何计算itext 7中表格列的总数和小计?
把itext 5的例子可以在以下网址找到:http://developers.itextpdf.com/examples/tables/using-cell-events-add-special-content#2887-subtotal.java
目前的细胞事件的功能在iText7
没有直接替代品。使用丰富的渲染机制可以实现期望的输出,该机制允许比事件更多地实现期望的输出。
我只会提供一种可能的方法来实现页面上的小表的小计。总是很容易,因此我将它放在答案的范围之外。
对于初学者来说,我们需要负责计算类:
private static class SubtotalHandler {
private int subtotalSum;
public void reset() {
subtotalSum = 0;
}
public void add(int val) {
subtotalSum += val;
}
public int get() {
return subtotalSum;
}
}
生成表中的代码本身看起来像这样:
同价位细胞Table table = new Table(UnitValue.createPercentArray(new float[] {1, 1})).setWidth(400);
table.setHorizontalAlignment(HorizontalAlignment.CENTER);
// Create our calculating class instance
SubtotalHandler handler = new SubtotalHandler();
for (int i = 0; i < 200; i++) {
table.addCell(new Cell().add(String.valueOf(i + 1)));
int price = 10;
Cell priceCell = new Cell().add(String.valueOf(price));
// Note that we set a custom renderer that will interact with our handler
priceCell.setNextRenderer(new SubtotalHandlerAwareCellRenderer(priceCell, handler, price));
table.addCell(priceCell);
}
table.addFooterCell(new Cell().add("Subtotal"));
Cell subtotalCell = new Cell();
// We create a custom renderer for the subtotal value itself as well because the text of the cell will depend on the state of the subtotal handler
subtotalCell.setNextRenderer(new SubtotalCellRenderer(subtotalCell, handler));
table.addFooterCell(subtotalCell);
渲染器只是告诉已添加部分金额的小计处理程序:
private static class SubtotalHandlerAwareCellRenderer extends CellRenderer {
private SubtotalHandler subtotalHandler;
private int price;
public SubtotalHandlerAwareCellRenderer(Cell modelElement, SubtotalHandler subtotalHandler, int price) {
super(modelElement);
this.subtotalHandler = subtotalHandler;
this.price = price;
}
@Override
public void draw(DrawContext drawContext) {
super.draw(drawContext);
subtotalHandler.add(price);
}
@Override
public IRenderer getNextRenderer() {
return new SubtotalHandlerAwareCellRenderer((Cell) modelElement, subtotalHandler, price);
}
}
何时来s到页脚单元的渲染,对应的单元格询问量,并把它打印小计处理,事后重置大部处理程序:
private static class SubtotalCellRenderer extends CellRenderer {
private SubtotalHandler subtotalHandler;
public SubtotalCellRenderer(Cell modelElement, SubtotalHandler subtotalHandler) {
super(modelElement);
this.subtotalHandler = subtotalHandler;
}
@Override
public void draw(DrawContext drawContext) {
super.draw(drawContext);
new Canvas(drawContext.getCanvas(), drawContext.getDocument(), occupiedArea.getBBox()).
showTextAligned(String.valueOf(subtotalHandler.get()), occupiedArea.getBBox().getX() + 3,
occupiedArea.getBBox().getY() + 3, TextAlignment.LEFT);
subtotalHandler.reset();
}
@Override
public IRenderer getNextRenderer() {
return new SubtotalCellRenderer((Cell) modelElement, subtotalHandler);
}
}
输出看起来是这样的:
谢谢Alexey。它解决了这个问题。 –