2017-02-16 14 views

答えて

2

iText7における細胞イベント機能のない直接の代替は現在ありません。 Howerverでは、イベントだけでなく数多くのレンダリングメカニズムを使用して、望ましい出力を達成することができます。

私は、ページの小計を実装する方法のうちの1つを提供します。合計は簡単なので、私は答えの範囲外にしておきます。

まず第一に、我々は計算を担当するクラスが必要になりますが:

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

+0

ありがとう、Alexey。それは問題を解決した。 –

関連する問題