2017-02-16 8 views

Répondre

2

Il n'y a actuellement aucune alternative directe de la fonctionnalité de l'événement cellulaire dans iText7. Howerver, la sortie désirée peut être atteint en utilisant un mécanisme de rendu riche qui permet beaucoup plus que de simples événements.

Je ne fournirai qu'une des façons possibles d'implémenter un sous-total de la table sur la page. Total est facile et donc je le laisse hors de la portée de la réponse.

Pour les entrées, nous aurons besoin d'une classe responsable des calculs:

private static class SubtotalHandler { 
    private int subtotalSum; 

    public void reset() { 
     subtotalSum = 0; 
    } 

    public void add(int val) { 
     subtotalSum += val; 
    } 

    public int get() { 
     return subtotalSum; 
    } 
} 

Le code pour générer la table elle-même ressemblera à ceci:

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

Renderers de cellules avec juste prix dire le gestionnaire de sous-total qu'une certaine quantité a été ajoutée:

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

Quand il s'agit s du rendu de la cellule de pied de page, la cellule correspondante demande le sous-total gestionnaire sur le montant et l'imprime et remet à zéro le sous-total gestionnaire aprè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); 
    } 
} 

La sortie ressemble à ceci:

part of the output

+0

Merci, Alexey. Il a résolu le problème. –