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

How to create a combobox or dropdown list inside a pdf table cell in itext

我有一个名为 table 的 PdfPTable,我在其单元格中成功创建了简单的文本、复选框和文本字段。现在,我正在尝试创建一个包含组合框(下拉列表)的列,并且我使用的逻辑与添加先前组件的逻辑几乎相同,但是,组合框不会显示在 table 单元格中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);
            }
        }
}

Please Note that when I run this code which I basically copied, a combobox is created inside a blank pdf document. So there must be something wrong with my adaptation. with my adaptation.

我仍然不知道我的代码有什么问题,但是,以下代码使我可以在 table 个单元格中创建组合框:

Rectangle rect = new Rectangle(80, 20);

TextField textList = new TextField(writer, rect, "combobox field name");

String[] optionList = new String[] { "Empty String", "One", "Two"};
String[] valueList = new String[] { "", 1, 2 };

textList.setChoices(optionList);
textList.setChoiceExports(valueList);

textList.setBorderWidth(1);
textList.setBorderColor(BaseColor.BLACK);
textList.setBorderStyle(PdfBorderDictionary.STYLE_SOLID);
textList.setFontSize(10);
        
PdfFormField dropDown = textList.getComboField();

PdfPCell cell = new PdfPCell();
cell.setCellEvent(new CustomComboBox(dropDown, rect.getWidth(), rect.getHeight(), writer));
table.addCell(cell);

CustomComboBox.java

public class CustomComboBox implements PdfPCellEvent {

    private static final float OFFSET_TOP = 0.5f;

    private static final float OFFSET_LEFT = 3.0f;

    private PdfFormField formField;

    private PdfWriter writer;

    private float width;

    private float height;

    public CustomComboBox(PdfFormField formField, float width, float height, PdfWriter writer) {
        this.formField = formField;
        this.width = width;
        this.height = height;
        this.writer = writer;
    }

    @Override
    public void cellLayout(PdfPCell cell, Rectangle rect, PdfContentByte[] canvases) {

        formField.setWidget(
                new Rectangle(rect.getLeft() + OFFSET_LEFT, rect.getTop() - height - OFFSET_TOP,
                        rect.getLeft() + width + OFFSET_LEFT, rect.getTop() - OFFSET_TOP),
                PdfAnnotation.HIGHLIGHT_NONE);
        
        writer.addAnnotation(formField);

    }
}