我有一个 PdfPTable
命名表,在它的单元格中,我成功地创建了简单的文本、复选框和文本字段。现在我正在尝试创建一个包含组合框(下拉列表)的列,我使用了与添加前面的组件几乎相同的逻辑,但是,组合框不会显示在pdf中的表格单元格中。
private void insertComboBox(PdfPTable table) throws DocumentException, IOException {
PdfFormField selectGroup = PdfFormField.createEmpty(writer);
selectGroup.setFieldName("myCombos");
String[] options = {"Choose first option", "Choose second option", "Choose third option"};
String[] exports = {"option1", "option2", "option3"};
PdfPCell cell = new PdfPCell();
cell.setCellEvent(new SelectCellEvent(selectGroup, "combo1", exports, options));
cell.setMinimumHeight(20);
table.addCell(cell);
writer.addAnnotation(selectGroup);
}
``` `writer` 在上一个 `insertComboBox` 方法的执行方式如下:
ByteArrayOutputStream baos = createTemporaryOutputStream();
Document document = new Document(PageSize.A4, 30, 30, 30, 30);
this.writer = PdfWriter.getInstance(document, baos);
selectcellevent.java
public class SelectCellEvent implements PdfPCellEvent {
protected PdfFormField selectGroup;
protected String name;
protected String[] exports;
protected String[] options;
protected BaseFont font;
public SelectCellEvent(PdfFormField selectGroup, String name, String[] exports, String[] options)
throws DocumentException, IOException {
this.selectGroup = selectGroup;
this.name = name;
this.exports = exports;
this.options = options;
font = BaseFont.createFont(BaseFont.TIMES_ROMAN, BaseFont.CP1250, BaseFont.NOT_EMBEDDED);
font.setSubset(false);
}
public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) {
PdfWriter writer = canvases[0].getPdfWriter();
TextField tf = new TextField(writer, position, name);
tf.setFont(font);
tf.setBorderStyle(PdfBorderDictionary.STYLE_BEVELED);
tf.setVisibility(TextField.VISIBLE_BUT_DOES_NOT_PRINT);
tf.setBorderColor(BaseColor.GRAY);
tf.setChoiceExports(exports);
tf.setChoices(options);
tf.setAlignment(Element.ALIGN_CENTER);
try {
selectGroup.addKid(tf.getComboField());
} catch (Exception e) {
throw new ExceptionConverter(e);
}
}
}
请注意,当我运行我基本上复制的代码时,会在一个空白的pdf文档中创建一个组合框。所以我的适应一定有问题。与我的适应。
1条答案
按热度按时间daolsyd01#
我仍然不知道我的代码出了什么问题,但是,以下代码使我能够在表单元格中创建组合框:
customcombobox.java