我正在创建一个表单,其中有一个按钮表。以下显示了这个想法:
Good OK Bad
Item1 [button] [button] [button]
Item2 [button] [button] [button]
每一行都是一个按钮组。
我的程序能够像上面这样的小桌子工作。但是,当行或列要保留多个页面时,显示很奇怪。这是我的计划:
PdfPTable table = new PdfPTable(colHeaders.size() + 1 );
table.setWidthPercentage(100);
PdfPCell cell;
table.setSpacingBefore(10);
table.setSpacingAfter(0);
table.setKeepTogether(false); //tested "true" different display error
//upper-left corner empty cell
cell = new CustomPdfPCell();
cell.setBackgroundColor(BaseColor.LIGHT_GRAY);
table.addCell(cell);
//header row
for (TableOfChoicesHeader c: colHeaders) {
String text = c.getText();
cell = new PdfPCell(new Phrase(text));
cell.setBackgroundColor(BaseColor.LIGHT_GRAY);
table.addCell(cell);
}
List<PdfFormField> radioGroups= new ArrayList<PdfFormField>();
//for each row
for (TableOfChoicesHeader r: rowHeaders) {
//row header
String rowText = r.getText();
cell = new PdfPCell(new Phrase(rowText));
cell.setBackgroundColor(BaseColor.LIGHT_GRAY);
table.addCell(cell);
//for the current row row
String fieldName = "question" + q.getId() +"_" + r.getId().toString();
PdfFormField radiogroup = PdfFormField.createRadioButton(writer, true);
radiogroup.setFieldName(fieldName);
radioGroups.add(radiogroup);
for (TableOfChoicesHeader c: colHeaders) {
cell = new PdfPCell();
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setPadding(5);
cell.setCellEvent(new CheckboxCellEvent(fieldName, r.getId() + "_" + c.getId()));
table.addCell(cell);
}
}
document.add(table);
for (PdfFormField g: radioGroups) {
writer.addAnnotation(g);
}
以下是table.setKeepTogether(false)
时的屏幕截图。显示有点不同但如果值为true
则仍然很奇怪。
更新
通过遵循Samuel的方法,我能够看到第一页上显示的单选按钮。但是,第一页上的按钮显示在第二页上。请参阅屏幕截图。
更新2
我通过在MyCellField中添加以下内容来实现它:
writer.addAnnotation(radiogroup);
而不是
PdfFormField field = radio.getRadioField();
writer.addAnnotation(field);
我无法解释原因。感谢Samuel和Bruno的帮助!
答案 0 :(得分:2)
再次查看您的代码,是否有任何理由为什么在最后一次添加无线电按钮的注释? 您是否在添加了一行单元格时尝试添加注释?像这样:
for (TableOfChoicesHeader r: rowHeaders) {
//row header
String rowText = r.getText();
cell = new PdfPCell(new Phrase(rowText));
cell.setBackgroundColor(BaseColor.LIGHT_GRAY);
table.addCell(cell);
//for the current row row
String fieldName = "question" + q.getId() +"_" + r.getId().toString();
PdfFormField radiogroup = PdfFormField.createRadioButton(writer, true);
radiogroup.setFieldName(fieldName);
radioGroups.add(radiogroup);
for (TableOfChoicesHeader c: colHeaders) {
cell = new PdfPCell();
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setPadding(5);
cell.setCellEvent(new CheckboxCellEvent(fieldName, r.getId() + "_" + c.getId()));
table.addCell(cell);
}
writer.addAnnotation(radiogroup)
}
编辑:
正如布鲁诺所说,在多个页面上分发无线电组的示例是RadioGroupMultiPage1和RadioGroupMultiPage2。可以在此处找到在表格中分发无线电组的示例:Distributing radio buttons of a radio field across multiple PdfCells。
编辑2,电动Bogaloo:我基本上把你的代码从上面的问题中拿出来(使用自定义数据类的一些存根实现),并使用RadioGroupMultiPage2中的MyCellField
PdfCellEvent扩展:
public MyCellField(PdfWriter writer, PdfFormField radiogroup, String value) {
this.writer = writer;
this.radiogroup = radiogroup;
this.value = value;
}
public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) {
try {
RadioCheckField radio = new RadioCheckField(writer, position, null, value);
PdfFormField field = radio.getRadioField();
writer.addAnnotation(field);
radiogroup.addKid(field);
} catch (IOException ex) {
// do nothing
} catch (DocumentException ex) {
// do nothing
}
}