如何在itext中的pdf表格单元格内创建组合框或下拉列表

j2qf4p5b  于 2021-08-25  发布在  Java
关注(0)|答案(1)|浏览(566)

我有一个 PdfPTable 命名表,在它的单元格中,我成功地创建了简单的文本、复选框和文本字段。现在我正在尝试创建一个包含组合框(下拉列表)的列,我使用了与添加前面的组件几乎相同的逻辑,但是,组合框不会显示在pdf中的表格单元格中。

  1. private void insertComboBox(PdfPTable table) throws DocumentException, IOException {
  2. PdfFormField selectGroup = PdfFormField.createEmpty(writer);
  3. selectGroup.setFieldName("myCombos");
  4. String[] options = {"Choose first option", "Choose second option", "Choose third option"};
  5. String[] exports = {"option1", "option2", "option3"};
  6. PdfPCell cell = new PdfPCell();
  7. cell.setCellEvent(new SelectCellEvent(selectGroup, "combo1", exports, options));
  8. cell.setMinimumHeight(20);
  9. table.addCell(cell);
  10. writer.addAnnotation(selectGroup);
  11. }
  12. ``` `writer` 在上一个 `insertComboBox` 方法的执行方式如下:

ByteArrayOutputStream baos = createTemporaryOutputStream();
Document document = new Document(PageSize.A4, 30, 30, 30, 30);
this.writer = PdfWriter.getInstance(document, baos);

  1. selectcellevent.java

public class SelectCellEvent implements PdfPCellEvent {
protected PdfFormField selectGroup;
protected String name;
protected String[] exports;
protected String[] options;
protected BaseFont font;

  1. public SelectCellEvent(PdfFormField selectGroup, String name, String[] exports, String[] options)
  2. throws DocumentException, IOException {
  3. this.selectGroup = selectGroup;
  4. this.name = name;
  5. this.exports = exports;
  6. this.options = options;
  7. font = BaseFont.createFont(BaseFont.TIMES_ROMAN, BaseFont.CP1250, BaseFont.NOT_EMBEDDED);
  8. font.setSubset(false);
  9. }
  10. public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) {
  11. PdfWriter writer = canvases[0].getPdfWriter();
  12. TextField tf = new TextField(writer, position, name);
  13. tf.setFont(font);
  14. tf.setBorderStyle(PdfBorderDictionary.STYLE_BEVELED);
  15. tf.setVisibility(TextField.VISIBLE_BUT_DOES_NOT_PRINT);
  16. tf.setBorderColor(BaseColor.GRAY);
  17. tf.setChoiceExports(exports);
  18. tf.setChoices(options);
  19. tf.setAlignment(Element.ALIGN_CENTER);
  20. try {
  21. selectGroup.addKid(tf.getComboField());
  22. } catch (Exception e) {
  23. throw new ExceptionConverter(e);
  24. }
  25. }

}

  1. 请注意,当我运行我基本上复制的代码时,会在一个空白的pdf文档中创建一个组合框。所以我的适应一定有问题。与我的适应。
daolsyd0

daolsyd01#

我仍然不知道我的代码出了什么问题,但是,以下代码使我能够在表单元格中创建组合框:

  1. Rectangle rect = new Rectangle(80, 20);
  2. TextField textList = new TextField(writer, rect, "combobox field name");
  3. String[] optionList = new String[] { "Empty String", "One", "Two"};
  4. String[] valueList = new String[] { "", 1, 2 };
  5. textList.setChoices(optionList);
  6. textList.setChoiceExports(valueList);
  7. textList.setBorderWidth(1);
  8. textList.setBorderColor(BaseColor.BLACK);
  9. textList.setBorderStyle(PdfBorderDictionary.STYLE_SOLID);
  10. textList.setFontSize(10);
  11. PdfFormField dropDown = textList.getComboField();
  12. PdfPCell cell = new PdfPCell();
  13. cell.setCellEvent(new CustomComboBox(dropDown, rect.getWidth(), rect.getHeight(), writer));
  14. table.addCell(cell);

customcombobox.java

  1. public class CustomComboBox implements PdfPCellEvent {
  2. private static final float OFFSET_TOP = 0.5f;
  3. private static final float OFFSET_LEFT = 3.0f;
  4. private PdfFormField formField;
  5. private PdfWriter writer;
  6. private float width;
  7. private float height;
  8. public CustomComboBox(PdfFormField formField, float width, float height, PdfWriter writer) {
  9. this.formField = formField;
  10. this.width = width;
  11. this.height = height;
  12. this.writer = writer;
  13. }
  14. @Override
  15. public void cellLayout(PdfPCell cell, Rectangle rect, PdfContentByte[] canvases) {
  16. formField.setWidget(
  17. new Rectangle(rect.getLeft() + OFFSET_LEFT, rect.getTop() - height - OFFSET_TOP,
  18. rect.getLeft() + width + OFFSET_LEFT, rect.getTop() - OFFSET_TOP),
  19. PdfAnnotation.HIGHLIGHT_NONE);
  20. writer.addAnnotation(formField);
  21. }
  22. }
展开查看全部

相关问题