2017-02-16 129 views

回答

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

何時來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); 
    } 
} 

輸出看起來是這樣的:

part of the output

+0

謝謝Alexey。它解決了這個問題。 –