Maruan Sahyoun created PDFBOX-4185:

             Summary: Fetching options for PDChoice causes ClassCastException 
                 Key: PDFBOX-4185
             Project: PDFBox
          Issue Type: Bug
          Components: AcroForm
    Affects Versions: 2.0.9, 2.0.4, 3.0.0 PDFBox
            Reporter: Maruan Sahyoun

I am trying to fetch the options available for a PDChoice field in a form but 
get a ClassCastException from the PDFBox internals.

The problematic PDF is an Inheritance Tax form from the UK's Revenue and 
Customs, specifically I am currently looking at IHT405:

I use this code to iterate over the fields:

        PDDocument doc = PDDocument.load(resource.getFile());
        PDDocumentCatalog catalog = doc.getDocumentCatalog();
        PDAcroForm form = catalog.getAcroForm();
        for (PDField field : form.getFields()) {
                if ("Ch".equals(field.getFieldType())) {
                        PDChoice choice = (PDChoice) field;
                        // All these variants fail with a ClassCastException:
                        choice.getOptionsExportValues(); // internally just 
delegates to getOptions()
This is a stacktrace for e.g. the getOptionsExportValues() call:

        java.lang.ClassCastException: org.apache.pdfbox.cos.COSArray cannot be 
cast to org.apache.pdfbox.cos.COSString


The problem is that the expected "stringArray" also contains COSArrays with 
value and label for the options:

        COSArray{[COSString{ }, COSArray{[COSString{Mr}, COSString{MR}]}, 
COSArray{[COSString{Mrs}, COSString{MRS}]}, COSArray{[COSString{Miss}, 
COSString{MISS}]}, COSArray{[COSString{Ms}, COSString{MS}]}]}

This does not seem to be expected in FieldUtils.getPairableItems, which 
introspects only the first item of the array and thus treats the array as an 
array of strings.

I found the bug with PDFBox 2.0.4 and upgraded to 2.0.9 which didn't help.

This message was sent by Atlassian JIRA

To unsubscribe, e-mail:
For additional commands, e-mail:

Reply via email to