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

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

我有一个 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文档中创建一个组合框。所以我的适应一定有问题。与我的适应。
daolsyd0

daolsyd01#

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

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);

    }
}

相关问题