gpt4 book ai didi

subtotal - 如何计算 itext 7 中表格列的总计和小计?

转载 作者:行者123 更新时间:2023-12-02 00:48:01 26 4
gpt4 key购买 nike

在 itext 5 中,可以使用单元格事件来计算表格列的小计。如何在 itext 7 中完成此操作?

可以在这个 URL 中找到 itext 5 示例:http://developers.itextpdf.com/examples/tables/using-cell-events-add-special-content#2887-subtotal.java

最佳答案

iText7 目前没有直接替代单元格事件的功能。然而,可以使用丰富的呈现机制来实现所需的输出,它允许的不仅仅是事件。

我将仅提供一种在页面上实现表格小计的可能方法。 Total 很简单,因此我将其排除在答案范围之外。

对于初学者,我们需要一个负责计算的类:

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);
}
}

当涉及到页脚单元格的呈现时,相应的单元格向小计处理程序询问金额并打印它,然后重置小计处理程序:

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);
}
}

输出看起来像这样:

part of the output

关于subtotal - 如何计算 itext 7 中表格列的总计和小计?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42281598/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com