You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@pdfbox.apache.org by Matthias Gall <ma...@trimplement.com> on 2018/04/07 13:01:16 UTC
Fetching options for PDChoice causes ClassCastException
Dear all,
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:
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()
}
}
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
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)
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.
Following the advised practices for bug reports I raise this on the mailing list first. I appreciate your feedback on how to proceed.
Kind regards
Matt
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org
Re: Fetching options for PDChoice causes ClassCastException
Posted by Maruan Sahyoun <sa...@fileaffairs.de>.
Hi,
> Am 07.04.2018 um 15:01 schrieb Matthias Gall <ma...@trimplement.com>:
>
> Dear all,
>
> 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:
>
> 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()
> }
> }
>
> 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
> 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)
>
> 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 is a limitation in the current implementation which expects either an array fo strings or an array of arrays/pairs but not a mix.
> 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.
>
> Following the advised practices for bug reports I raise this on the mailing list first. I appreciate your feedback on how to proceed.
I've opened PDFBOX-4185 for that. Feel free to comment there and add yourself as a watcher.
BR
Maruan
>
> Kind regards
> Matt
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
> For additional commands, e-mail: users-help@pdfbox.apache.org
>
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org