如何使用iText制作交互式PDF格式?

时间:2014-03-16 15:51:39

标签: java itext

任何人都可以告诉我们如何开发申请PDF表格包含文本框,复选框和单选按钮。我们要求使用iText java API开发具有不同表单字段的pdf吗?

1 个答案:

答案 0 :(得分:3)

  

任何人都可以告诉我们如何开发申请PDF表格包含文本框,

比照。 iText原作者的样本......

Keyword TEXT FIELDS例如TextFields.java

    TextField text = new TextField(writer, rectangle,
            String.format("text_%s", tf));
    text.setBackgroundColor(new GrayColor(0.75f));
    switch(tf) {
    case 1:
        text.setBorderStyle(PdfBorderDictionary.STYLE_BEVELED);
        text.setText("Enter your name here...");
        text.setFontSize(0);
        text.setAlignment(Element.ALIGN_CENTER);
        text.setOptions(TextField.REQUIRED);
        break;
    case ...:
        ...
    }
    try {
        PdfFormField field = text.getTextField();
        ...
        writer.addAnnotation(field);
    }
    catch(IOException ioe) {
        throw new ExceptionConverter(ioe);
    }
    catch(DocumentException de) {
        throw new ExceptionConverter(de);
    }
  

复选框和

Keyword BUTTON FIELDS > CHECKBOX,例如Buttons.java

    RadioCheckField checkbox;
    for (int i = 0; i < LANGUAGES.length; i++) {
        rect = new Rectangle(180, 806 - i * 40, 200, 788 - i * 40);
        checkbox = new RadioCheckField(writer, rect, LANGUAGES[i], "Yes");
        field = checkbox.getCheckField();
        field.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, "Off", onOff[0]);
        field.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, "Yes", onOff[1]);
        writer.addAnnotation(field);
        ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT,
            new Phrase(LANGUAGES[i], font), 210, 790 - i * 40, 0);
    }
  

单选按钮。

Keyword BUTTON FIELDS > RADIO FIELD,例如RadioButtons.java

    BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
    // create a radio field spanning different pages
    PdfFormField radiogroup = PdfFormField.createRadioButton(writer, true);
    radiogroup.setFieldName("language");
    Rectangle rect = new Rectangle(40, 806, 60, 788);
    RadioCheckField radio;
    PdfFormField radiofield;
    for (int page = 0; page < LANGUAGES.length; ) {
        radio = new RadioCheckField(writer, rect, null, LANGUAGES[page]);
        radio.setBackgroundColor(new GrayColor(0.8f));
        radiofield = radio.getRadioField();
        radiofield.setPlaceInPage(++page);
        radiogroup.addKid(radiofield);
    }
    writer.addAnnotation(radiogroup);

当然,请查看完整的样本来源,将其置于上下文中。如果可能,请阅读书籍ITEXT IN ACTION — SECOND EDITION以了解您在做什么。