我想制作一个包含多行的PdfPTable。在每一行中,我想在第一个单元格中有一个单选按钮,在第二个单元格中有描述性文本。我希望所有的单选按钮都成为同一个广播组的一部分。
我过去曾使用PdfPCell.setCellEvent和我自己的自定义cellEvents在PdfPTables中渲染TextFields和Checkboxes。但是,我似乎无法弄清楚如何使用单选按钮/无线电组进行操作。
这可以通过iText实现吗?有人有例子吗?
答案 0 :(得分:2)
请查看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);
}
}
}
答案 1 :(得分:1)
更进一步......
如果您在另一个表格中嵌入一个单选按钮表(广播组),则必须从Bruno的示例中更改以下内容:
而不是
document.add(table);
writer.addAnnotation(radiogroup);
使用(假设您在该表中创建了一个名为parentCell的父表和PdfPCell)
parentCell.addElement(table);
parentCell.setCellEvent(new RadioGroupCellEvent(radioGroup));
使用像这样的父细胞事件
public class RadioGroupCellEvent implements PdfPCellEvent {
private PdfFormField radioGroup;
public RadioGroupCellEvent(PdfFormField radioGroup) {
this.radioGroup = radioGroup;
}
@Override
public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) {
PdfWriter writer = canvases[0].getPdfWriter();
writer.addAnnotation(radioGroup);
}
}