我有一个用表格创建的表格。在表格的单元格中,我有复选框。其中一些复选框需要选中,而其他复选框则不需要。
我四处搜索并想出了将复选框放在表格中的方法。这是我创建一些表格单元格的方法。
private void createFourColumnBody(String[] rowLabels, PdfPTable table) throws DocumentException {
PdfFormField checkboxGroupField = PdfFormField.createCheckBox(writer);
for (String label : rowLabels) {
PdfPCell cell = table.getDefaultCell();
cell = new PdfPCell(new Paragraph(label));
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
table.addCell(cell);
cell = new PdfPCell(table.getDefaultCell());
cell.setCellEvent(new CellField(writer, checkboxGroupField, true));
table.addCell(cell);
cell = new PdfPCell(table.getDefaultCell());
cell.setCellEvent(new CellField(writer, checkboxGroupField, false));
table.addCell(cell);
cell = new PdfPCell(new Paragraph(" "));
table.addCell(cell);
}
getDocument().add(table);
writer.addAnnotation(checkboxGroupField);
}
这是被调用来创建复选框的类。
protected class CellField implements PdfPCellEvent {
private PdfFormField parent;
private String partialFieldName;
private PdfWriter writer;
private boolean checked;
public CellField(PdfWriter writer, PdfFormField parent, boolean checked) {
this.writer = writer;
this.parent = parent;
this.checked = checked;
}
public void cellLayout(PdfPCell cell, Rectangle rect, PdfContentByte[] cb) {
try {
createCheckboxField(rect);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private void createCheckboxField(Rectangle rect) throws IOException, DocumentException {
RadioCheckField rf = new RadioCheckField(writer, new Rectangle(rect.getLeft(2), rect.getBottom(2),
rect.getRight(2), rect.getTop(2)), partialFieldName, "");
rf.setChecked(checked);
rf.setBorderColor(GrayColor.GRAYBLACK);
rf.setBackgroundColor(GrayColor.GRAYWHITE);
rf.setCheckType(RadioCheckField.TYPE_CHECK);
parent.addKid(rf.getCheckField());
}
}
您可以在第一种方法中看到,我将第一个复选框的选中布尔值标记为 true,第二个复选框标记为 false,但它总是创建一个选中复选框的 pdf。我曾尝试删除复选标记并仅绘制一个矩形,但无济于事。需要发生什么才能使rf.setChecked(false)
作品看起来像它应该的那样。谢谢。