Search code examples
javaitextacrofields

Why iText form.getAppearanceStates(key) method return inconsistent values for Check Box field?


I have PDF with acroForm that was created using Adobe Acrobat Standard DC.

There is Chekcbox field that I populate using Java iText 5. And I do not understand why, IMHO, form.getAppearanceStates(key) method return inconsistent values for this field.

String[] states = form.getAppearanceStates(key); 

Why I find them inconsistent because values are: ["Off", "Yes"]

Are these default values? Does each form creation software has different default values? Why I ask about it, because my software can be used with documents created in many different ways. I am trying to work out all cases in my code.

I know I can change Export Value in Options of this field, but is default one will always be "Yes"? And is default unchecked value will always be "Off"?

Consistent values in IMHO will be like: ["On", "Off"] or ["Yes", "No"]


Solution

  • This "inconsistency" is based in the PDF format as such, the specification enforces that the unchecked state is named Off while the name of the checked state can be freely chosen, and in an example it uses Yes as the name of the checked state:

    The appearance for the off state is optional but, if present, shall be stored in the appearance dictionary under the name Off.

    ...

    EXAMPLE 1 This example shows a typical check box definition.

    1 0 obj
    <</Type /Annot
    /Subtype /Widget
    /Rect [100 100 120 120]
    /FT /Btn
    /T (Urgent)
    /V /Yes
    /AS /Yes
    /AP <</N <</Yes 2 0 R /Off 3 0 R>>
    >>
    endobj
    

    (ISO 32000-2:2017, section 12.7.5.2.3 "Check boxes")

    Concerning your question, therefore:

    Are these default values? Does each form creation software has different default values?

    One checkbox state always is Off, the name of the checked state can be freely chosen but probably many forms will follow the example above and use Yes. Thus, expect to find Yes often but be ready to deal with anything.