Там в настоящее время нет прямых альтернативой функциональности событий ячейки в iText7
. Howerver, желаемый результат может быть достигнут с использованием богатого механизма рендеринга, который позволяет намного больше, чем просто событий.
Я предоставлю лишь один из возможных способов реализации промежуточного итога таблицы на странице. Всего легко, и поэтому я оставляю его вне сферы действия ответа.
Для начала нам понадобится класс, ответственный за расчеты:
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);
Renderers клетки с ценой только сказать промежуточный обработчик, что некоторая сумма была добавлена:
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);
}
}
Результат выглядит следующим образом:
Спасибо, Алексей. Он решил проблему. –