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

时间:2017-02-16 18:09:13

标签: subtotal itext7

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

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

1 个答案:

答案 0 :(得分:2)

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

当涉及到页脚单元格的渲染时,相应的单元格会向小计处理程序询问数量并打印它,然后重置小计处理程序:

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