0

I'm having trouble retrieving the export value for checkboxes in PDFBox. I'm using PDFBox v2.0.21. I need the export values as I need to know what each checkbox field maps to in another application.

The checkboxes have the same partial name, but different export values. An example pdf is here.

If I call "getExportValues" on the field, I get all available checkbox export options for the group of checkboxes ["a","b"]. e.g. ((PDCheckBox) field).getExportValues(); //returns ["a","b"] Is there a way to determine which export value belongs to the current field?

If I call "getOnValue" it returns "0" (it does this for all the checkbox fields). e.g. onValue = ((PDCheckBox) field).getOnValue(); //returns "0" It returns "0" regardless of which field I'm looking at.

If I call field.check() it throws 'java.lang.IllegalArgumentException' exception (which I assume is because it's using "0" instead of "a" or "b".

If I call field.setValue() with either "a" or "b" they both succeed for the field, so it's not clear what other options to try. I had attempted this approach in the hope that I could determine what export value belongs to the field I'm currently querying.

Please let me know if you have any insights into the above.

Update: after further debugging it looks like each field has 2 widgets associated with it (so if field checkbox has the same name it has multiple widgets). Adding additional checkbox to pdf results in 3 widgets associated with each fields. If I use field.getWidgets().get(1).dictionary.getCOSObject(COSName.AP).getDictionaryObject(COSName.N) and get the key that isn't "off" (as outlined here) then it returns "1" which I assume is the index of that widgets export value (from getExportValues) however I don't see a way to determine which widget belongs to the field I'm currently looking at.

Thanks Rob

Tilman Hausherr
  • 17,731
  • 7
  • 58
  • 97
Rob Curtis
  • 2,245
  • 24
  • 33

1 Answers1

0

On further debugging I found that if checkbox fields share the same value there is only 1 Adobe Field associated with it.

So to get the export values I loop through the widgets of each field and get the "onValue" of each widget using this code.

    PDAppearanceDictionary apDictionary = widget.getAppearance();
    String onValue = "";
    if(apDictionary!=null)


    PDAppearanceEntry normalAppearance = apDictionary.getNormalAppearance();
    if (normalAppearance != null) {
        Set<COSName> entries = normalAppearance.getSubDictionary().keySet();
        Iterator var6 = entries.iterator();

        while (var6.hasNext()) {
            COSName entry = (COSName) var6.next();
            if (COSName.Off.compareTo(entry) != 0) {
                onValue = entry.getName();
            }
        }
    }


   List<String> exportValues = ((PDCheckBox) field).getExportValues();

   String exportValue = exportValues.get(Integer.parseInt(onValue)); 
Rob Curtis
  • 2,245
  • 24
  • 33