iText不设置复选框字段

时间:2015-02-02 16:33:02

标签: pdf checkbox itext

我使用iText库生成基于模板的pdf文件。但是当我尝试设置复选框字段时,它不会被填充。 上课在这里:

public class MainClass {
    public static void main(String[] args) {
        try {
            PdfReader reader = new PdfReader("pdf/fw9_template.pdf");
            PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("test.pdf"));
            AcroFields form = stamper.getAcroFields();

            String states[] = form.getAppearanceStates("topmostSubform[0].Page1[0].FederalClassification[0].c1_1");
            System.out.println(states);


            for (Iterator i = form.getFields().keySet().iterator(); i.hasNext(); ) {
                String key = (String) i.next();
                System.out.print(key + " : ");
                switch(form.getFieldType(key)) {
                    case AcroFields.FIELD_TYPE_CHECKBOX:
                        System.out.println("Checkbox");
                        break;
                    case AcroFields.FIELD_TYPE_COMBO:
                        System.out.println("Combobox");
                        break;
                    case AcroFields.FIELD_TYPE_LIST:
                        System.out.println("List");
                        break;
                    case AcroFields.FIELD_TYPE_NONE:
                        System.out.println("None");
                        break;
                    case AcroFields.FIELD_TYPE_PUSHBUTTON:
                        System.out.println("Pushbutton");
                        break;
                    case AcroFields.FIELD_TYPE_RADIOBUTTON:
                        System.out.println("Radiobutton");
                        break;
                    case AcroFields.FIELD_TYPE_SIGNATURE:
                        System.out.println("Signature");
                        break;
                    case AcroFields.FIELD_TYPE_TEXT:
                        System.out.println("Text");
                        break;
                    default:
                        System.out.println("?");
                }
            }
            form.setField("topmostSubform[0].Page1[0].FederalClassification[0].c1_1[0]", "true");

            stamper.close();
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}

节目输出:

null
topmostSubform[0].Page1[0].EmployerID[0].f1_14[0] : Text
topmostSubform[0].Page1[0].FederalClassification[0].f1_4[0] : Text
topmostSubform[0].Page1[0].f1_9[0] : Text
topmostSubform[0].Page1[0].f1_10[0] : Text
topmostSubform[0].Page1[0].FederalClassification[0].c1_1[1] : Checkbox
topmostSubform[0].Page1[0].f1_2[0] : Text
topmostSubform[0].Page1[0].EmployerID[0].f1_15[0] : Text
topmostSubform[0].Page1[0].SSN[0].f1_12[0] : Text
topmostSubform[0].Page1[0].SSN[0].f1_13[0] : Text
topmostSubform[0].Page1[0].Address[0].f1_8[0] : Text
topmostSubform[0].Page1[0].Address[0].f1_7[0] : Text
topmostSubform[0].Page1[0].FederalClassification[0].c1_1[0] : Checkbox
topmostSubform[0].Page1[0].FederalClassification[0].c1_1[2] : Checkbox
topmostSubform[0].Page1[0].FederalClassification[0].c1_7[0] : Checkbox
topmostSubform[0].Page1[0].FederalClassification[0].c1_1[4] : Checkbox
topmostSubform[0].Page1[0].FederalClassification[0].c1_1[3] : Checkbox
topmostSubform[0].Page1[0].f1_1[0] : Text
topmostSubform[0].Page1[0].FederalClassification[0].c1_1[5] : Checkbox
topmostSubform[0].Page1[0].SSN[0].f1_11[0] : Text
topmostSubform[0].Page1[0].FederalClassification[0].f1_3[0] : Text
topmostSubform[0].Page1[0].Exemptions[0].f1_6[0] : Text
topmostSubform[0].Page1[0].Exemptions[0].f1_5[0] : Text

当我们在输出中看到复选框时,我不明白为什么方法getAppearanceStates()返回' null'复选框也没有填写结果pdf文件。

有人可以帮我解决这个问题吗?

提前致谢, 塞吉

1 个答案:

答案 0 :(得分:0)

为什么status结果为null

String states[] = form.getAppearanceStates(
    "topmostSubform[0].Page1[0].FederalClassification[0].c1_1");

您在自己的问题中提供答案:因为表单中没有名称为"topmostSubform[0].Page1[0].FederalClassification[0].c1_1"的字段。

使用以下行时states是什么:

String states[] = form.getAppearanceStates(
    "topmostSubform[0].Page1[0].FederalClassification[0].c1_1[1]");