我正在使用Itext7創建一個帶有表格的pdf。
該表格佔用多個頁面。
我也重寫了單元格渲染器以將某個表單添加到某個單元格。
我注意到,當表進入一個新頁面時,第一行不會觸發CellRender類的繪製方法。
爲了更好的理解,我在下面放了一些代碼。Itext7 CellRenderer並不總是工作
...
//adding the cells to table
for (int i = 0; i < 10; i++) {
addRow(table,i);
}
...
//addRow implementation
private void addRow(Table table, int row) throws IOException {
table.startNewRow();
PdfFont zapfdingbats = PdfFontFactory.createFont(FontConstants.ZAPFDINGBATS);
Cell checkBoxCell = new Cell().add(new Paragraph("o").setFont(zapfdingbats).setMargins(9, 0, 0, 11)).setKeepTogether(true);
checkBoxCell.setNextRenderer(new CheckboxCellRenderer(checkBoxCell, "cb"+row));
table.addCell(checkBoxCell);
...
//other cells here
}
...
//The implementation of the custom CellRender
protected class CheckboxCellRenderer extends CellRenderer {
protected String name;
public CheckboxCellRenderer(Cell modelElement, String name) {
super(modelElement);
this.name = name;
}
//this method is not triggered for the first row when a new page is created
@Override
public void draw(DrawContext drawContext) {
System.out.println(name);
super.draw(drawContext);
float x = getOccupiedAreaBBox().getLeft();
float y = getOccupiedAreaBBox().getBottom();
float width = getOccupiedAreaBBox().getRight()-getOccupiedAreaBBox().getLeft();
float height = getOccupiedAreaBBox().getTop()-getOccupiedAreaBBox().getBottom();
Rectangle rect = new Rectangle(x+5, y+5, width-10, height-10);
PdfTextFormField pdfTextFormField = PdfFormField.createText(drawContext.getDocument(), rect, name);
PdfAcroForm.getAcroForm(drawContext.getDocument(), true).addField(pdfTextFormField);
}
}
我在做什麼錯?