You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@pdfbox.apache.org by "Travis Schneeberger (JIRA)" <ji...@apache.org> on 2017/06/08 15:23:18 UTC

[jira] [Comment Edited] (PDFBOX-3751) Flattening a pdf with refreshAppearances set to true causes a NullPointerException

    [ https://issues.apache.org/jira/browse/PDFBOX-3751?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16042842#comment-16042842 ] 

Travis Schneeberger edited comment on PDFBOX-3751 at 6/8/17 3:22 PM:
---------------------------------------------------------------------

[~msahyoun] So sorry I didn't get around to test this earlier.  I'm testing the 2.0.6 release and this problem still exists.  I see that you made PDPushButton.constructAppearances() no op.  I believe this same change might be needed in PDButton.java.  I see that PDButton is the base class for PDCheckbox, PDRadioButton, and PDPushButton.  Maybe there are cases where these subclasses should have their appearance refreshed?  Either way there are certain cases where refreshing is still causing a NPE 

The following stacktrace still happens without my documented workaround.

java.lang.NullPointerException
	at org.apache.pdfbox.pdmodel.interactive.form.PDButton.updateByValue(PDButton.java:356)
	at org.apache.pdfbox.pdmodel.interactive.form.PDButton.constructAppearances(PDButton.java:285)
	at org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm.refreshAppearances(PDAcroForm.java:348)
	at org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm.flatten(PDAcroForm.java:223)


was (Author: leo.herbie):
[~msahyoun] So sorry I didn't get around to test this earlier.  I'm testing your 2.0.6 release and this problem still exists.  I see that you made PDPushButton. constructAppearances() no op.  I believe this same change might be needed in  PDButton.java.

The following stacktrace still happens without my documented workaround.

java.lang.NullPointerException
	at org.apache.pdfbox.pdmodel.interactive.form.PDButton.updateByValue(PDButton.java:356)
	at org.apache.pdfbox.pdmodel.interactive.form.PDButton.constructAppearances(PDButton.java:285)
	at org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm.refreshAppearances(PDAcroForm.java:348)
	at org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm.flatten(PDAcroForm.java:223)

> Flattening a pdf with refreshAppearances set to true causes a NullPointerException
> ----------------------------------------------------------------------------------
>
>                 Key: PDFBOX-3751
>                 URL: https://issues.apache.org/jira/browse/PDFBOX-3751
>             Project: PDFBox
>          Issue Type: Bug
>          Components: AcroForm
>    Affects Versions: 2.0.5
>            Reporter: Travis Schneeberger
>            Assignee: Maruan Sahyoun
>             Fix For: 2.0.6, 3.0.0
>
>         Attachments: fdpAttachment2.pdf
>
>
> Flattening a pdf with refreshAppearances set to true causes a NullPointerException.  This is because some fields do not have a NormalAppearance set.
> For example:
> {code}
>         try(PDDocument pdfDocument = PDDocument.load(new File("/Users/travis/Desktop/fdpAttachment2.pdf"))) {
>             pdfDocument.getDocumentCatalog().getAcroForm().flatten(pdfDocument.getDocumentCatalog().getAcroForm().getFields(), true);
>         }
> {code}
> causes:
> {quote}
> Exception in thread "main" java.lang.NullPointerException
> 	at org.apache.pdfbox.pdmodel.interactive.form.PDButton.updateByValue(PDButton.java:356)
> 	at org.apache.pdfbox.pdmodel.interactive.form.PDButton.constructAppearances(PDButton.java:285)
> 	at org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm.refreshAppearances(PDAcroForm.java:348)
> 	at org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm.flatten(PDAcroForm.java:223)
> 	at org.kuali.coeus.sys.pdf.FlattenException.main(FlattenException.java:13)
> {quote}
> The workaround is pretty simple.  Just make sure every widget without a NormalAppearance has one before calling flatten.  I would think pdfbox could do a null check internally when trying to refresh appearance.
> {code}
>         pdfDocument.getDocumentCatalog().getAcroForm().getFields()
>                 .stream()
>                 .flatMap(f -> f.getWidgets().stream())
>                 .filter(w -> w.getAppearance() == null)
>                 .forEach(w -> {
>                     final PDAppearanceDictionary appearance = new PDAppearanceDictionary(new COSDictionary());
>                     appearance.setNormalAppearance(new PDAppearanceEntry(new COSDictionary()));
>                     w.setAppearance(appearance);
>                 });
> {code}



--
This message was sent by Atlassian JIRA
(v6.3.15#6346)

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