0

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

Program output:

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

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?

Thanks in advance, Sergey

Dolzhenok
  • 78
  • 6
  • 1
    This is a possible duplicate of [Checking off pdf checkbox with itextsharp](http://stackoverflow.com/questions/19698771/checking-off-pdf-checkbox-with-itextsharp). In your question, you don't show how you are trying to fill out the check box. Moreover: your field names look as if you have a hybrid XFA form. Maybe that's causing the problem... Is your PDF an XFA form? – Bruno Lowagie Feb 02 '15 at 16:51

1 Answers1

0

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

String states[] = form.getAppearanceStates(
    "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.

What is states when you use the following line:

String states[] = form.getAppearanceStates(
    "topmostSubform[0].Page1[0].FederalClassification[0].c1_1[1]");
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • 1
    Thanks, Bruno! You was right. `code`String states[] = form.getAppearanceStates( "topmostSubform[0].Page1[0].FederalClassification[0].c1_1[1]");`code` This statement returns '2'. So, to set a checkbox I need to write the next code: form.setField("topmostSubform[0].Page1[0].FederalClassification[0].c1_1[1]", "2"); – Dolzhenok Feb 03 '15 at 06:31
  • Sometimes you need an extra pair of eyes to see what's wrong ;-) – Bruno Lowagie Feb 03 '15 at 07:34