我想制作一个包含多行的 PdfPTable。在每一行中,我希望第一个单元格中有一个单选按钮,第二个单元格中有一个描述性文本。我希望所有单选按钮都属于同一单选按钮组。
我过去曾使用 PdfPCell.setCellEvent 和我自己的自定义 cellEvents 来呈现 PdfPTables 中的文本字段和复选框。但是,我似乎无法弄清楚如何使用单选按钮/单选组来做到这一点。
iText 可以做到这一点吗?有人有例子吗?
请看一下创建RadioInTable http://itextpdf.com/sandbox/acroforms/CreateRadioInTable例子。
在这个例子中,我们创建一个PdfFormField
对于无线电组,我们在构建后添加它并添加桌子:
PdfFormField radiogroup = PdfFormField.createRadioButton(writer, true);
radiogroup.setFieldName("Language");
PdfPTable table = new PdfPTable(2);
// add cells
document.add(table);
writer.addAnnotation(radiogroup);
当我们为单选按钮创建单元格时,我们添加一个事件,例如:
cell.setCellEvent(new MyCellField(radiogroup, "english"));
该事件如下所示:
class MyCellField implements PdfPCellEvent {
protected PdfFormField radiogroup;
protected String value;
public MyCellField(PdfFormField radiogroup, String value) {
this.radiogroup = radiogroup;
this.value = value;
}
public void cellLayout(PdfPCell cell, Rectangle rectangle, PdfContentByte[] canvases) {
final PdfWriter writer = canvases[0].getPdfWriter();
RadioCheckField radio = new RadioCheckField(writer, rectangle, null, value);
try {
radiogroup.addKid(radio.getRadioField());
} catch (final IOException ioe) {
throw new ExceptionConverter(ioe);
} catch (final DocumentException de) {
throw new ExceptionConverter(de);
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)