我有一张用表创建的表单。在表格的单元格中,我有复选框。其中一些复选框需要检查,而其他复选框则不需要。
我用Google搜索并想出了将复选框放入表格的方法。这是我创建一些表格单元格的方法。
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的checked布尔值和第二个复选框标记为false,但它总是创建一个选中了复选框的pdf。我试过删除复选标记,只是绘制一个矩形,但它一直无济于事。需要做些什么来使rf.setChecked(false)
像它一样工作。谢谢。
答案 0 :(得分:0)
虽然你有double posted我会给你一些提示代码有什么问题:
好的,这里的代码工作正常:
public void createFourColumnBody(String[] rowLabels, PdfPTable table) throws DocumentException {
this.doc.open();
ArrayList<PdfFormField> list = new ArrayList<PdfFormField>();
for (int i=0;i<rowLabels.length;i++) {
//PdfFormField checkboxGroupField = PdfFormField.createRadioButton(this.writer,false);
PdfFormField checkbox1 = PdfFormField.createCheckBox(this.writer);
//create a unique name
checkbox1.setFieldName("checkbox-"+i);
PdfFormField checkbox2 = PdfFormField.createCheckBox(this.writer);
checkbox2.setFieldName("checkbox+"+i);
//use for autofill
//checkbox2.setFieldName("checkbox-"+i);
PdfPCell cell = table.getDefaultCell();
cell = new PdfPCell(new Paragraph(rowLabels[i]));
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
table.addCell(cell);
cell = new PdfPCell(table.getDefaultCell());
cell.setCellEvent(new CellField(this.writer, checkbox1, true,"1"));
table.addCell(cell);
cell = new PdfPCell(table.getDefaultCell());
cell.setCellEvent(new CellField(this.writer, checkbox2, false,"1"));
//use for autofill
//cell.setCellEvent(new CellField(this.writer, checkbox1, true,"1"));
table.addCell(cell);
cell = new PdfPCell(new Paragraph(" "));
table.addCell(cell);
list.add(checkbox1);
list.add(checkbox2);
}
this.doc.add(table);
for(PdfFormField field: list){
this.writer.addAnnotation(field);
}
}
protected class CellField implements PdfPCellEvent {
private PdfFormField parent;
private PdfWriter writer;
private boolean checked;
private String onValue;
public CellField(PdfWriter writer, PdfFormField parent, boolean checked, String onValue) {
this.writer = writer;
this.parent = parent;
this.checked = checked;
this.onValue = onValue;
}
public void cellLayout(PdfPCell cell, Rectangle rect, PdfContentByte[] cb) {
try {
this.createCheckboxField(rect);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private void createCheckboxField(Rectangle rect) throws IOException, DocumentException {
RadioCheckField bt = new RadioCheckField(this.writer, rect, null, this.onValue);
bt.setCheckType(RadioCheckField.TYPE_CROSS);
bt.setChecked(this.checked);
this.parent.addKid(bt.getCheckField());
}
}
关于setCheckType
比较另一篇文章。