iText不设置复选框字段 [英] iText doesn't set checkbox field

查看:126
本文介绍了iText不设置复选框字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using iText library to generate pdf file based on template. But when I try to set checkbox field it won't filled. Class is here:

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文件。

I don't understand why the method getAppearanceStates() returns 'null', when we can see checkboxes in the output. Also checkbox doesn't filled in a result pdf file.

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

Can anybody help me to resolve this issue?

提前致谢,
Sergey

Thanks in advance, Sergey

推荐答案

为什么状态当你这样做时,结果是 null

Why does status turns out to be null when you do:

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

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

You are providing the answer in your own question: because there is no field with name "topmostSubform[0].Page1[0].FederalClassification[0].c1_1" in your form.

什么是<$当您使用以下行时,c $ c>州:

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

这篇关于iText不设置复选框字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆