Details
Description
When trying to set value for a field in pdf using,
PDDocument pdf = PDDocument.load(<pdfFileName>);
PDDocumentCatalog docCatalog = pdf.getDocumentCatalog();
PDAcroForm acroForm = docCatalog.getAcroForm();
PDTextbox t = (PDTextbox) acroForm.getField(<fieldName>);
t.setValue(<someValue>);
got following exception :
java.lang.NullPointerException
at org.apache.pdfbox.pdmodel.common.COSDictionaryMap.put(COSDictionaryMap.java:106)
at org.apache.pdfbox.pdmodel.interactive.form.PDAppearance.getFontAndUpdateResources(PDAppearance.java:440)
at org.apache.pdfbox.pdmodel.interactive.form.PDAppearance.setAppearanceValue(PDAppearance.java:268)
at org.apache.pdfbox.pdmodel.interactive.form.PDVariableText.setValue(PDVariableText.java:131)
at qs.common.GenerateReports.generateBidderDeclaration_OBS_05_105(GenerateReports.java:71)
Source of the exception :
NullPointerException thrown at retval = (PDFont)formResources.getFonts().get( fontName ); in org.apache.pdfbox.pdmodel.interactive.form.PDAppearance
Tried suggestion from following link :
i.e. intruduced a try-catch for
retval = (PDFont)formResources.getFonts().get( fontName );
resolved the issue for me as well.
Also, I had to add try-catch for following :
printWriter.println(getTextPosition(boundingBox, pdFont, fontSize, tokens));
as getTextPosition() is throwing IOException as the pdFont is null.
Creating this issue so that the bug is documented and would like to see the change in upcoming releases.
Shall attach the jar file that contains the changes which resolved the issue.