Maruan Sahyoun created PDFBOX-4185:
--------------------------------------

             Summary: Fetching options for PDChoice causes ClassCastException 
                 Key: PDFBOX-4185
                 URL: https://issues.apache.org/jira/browse/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:

https://assets.publishing.service.gov.uk/government/uploads/system/uploads/attachment_data/file/697346/IHT405_online.pdf

I use this code to iterate over the fields:

{code}
        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.getOptions();
                        choice.getOptionsDisplayValues();
                        choice.getOptionsExportValues(); // internally just 
delegates to getOptions()
                }
        }
{code}
This is a stacktrace for e.g. the getOptionsExportValues() call:

{noformat}
        java.lang.ClassCastException: org.apache.pdfbox.cos.COSArray cannot be 
cast to org.apache.pdfbox.cos.COSString
                at 
org.apache.pdfbox.pdmodel.common.COSArrayList.convertCOSStringCOSArrayToList(COSArrayList.java:367)
                at 
org.apache.pdfbox.pdmodel.interactive.form.FieldUtils.getPairableItems(FieldUtils.java:182)
                at 
org.apache.pdfbox.pdmodel.interactive.form.PDChoice.getOptions(PDChoice.java:91)
                at 
org.apache.pdfbox.pdmodel.interactive.form.PDChoice.getOptionsExportValues(PDChoice.java:210)

{noformat}


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

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

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
(v7.6.3#76005)

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@pdfbox.apache.org
For additional commands, e-mail: dev-h...@pdfbox.apache.org

Reply via email to