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

Subtotal 如何计算itext 7中表格列的总计和小计?,subtotal,itext7,Subtotal,Itext7,在itext 5中,可以使用单元格事件计算表列的小计。在itext 7中如何实现这一点 itext 5示例可以在以下URL中找到:目前在iText7中没有单元格事件功能的直接替代方案。 然而,使用丰富的渲染机制可以实现所需的输出,该机制允许的不仅仅是事件 我将提供实现页面上表小计的可能方法之一。总数很简单,所以我把它排除在答案的范围之外 对于初学者,我们需要一个负责计算的类: private static class SubtotalHandler { private int subto

在itext 5中,可以使用单元格事件计算表列的小计。在itext 7中如何实现这一点


itext 5示例可以在以下URL中找到:

目前在
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 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);
    }
}
输出如下所示:

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