pdfbox-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Matthias Gall <matthias.g...@trimplement.com>
Subject Fetching options for PDChoice causes ClassCastException
Date Sat, 07 Apr 2018 13:01:16 GMT
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


Mime
View raw message