You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@pdfbox.apache.org by Forgács Péter <pe...@appello.eu> on 2021/06/08 11:04:39 UTC

Possible NPE when merging a PDF with fillable fields

Hello

One of our users in production received an error with the following stack trace:

java.lang.NullPointerException
        at org.apache.pdfbox.multipdf.PDFMergerUtility.mergeAcroForm(PDFMergerUtility.java:678)
        at org.apache.pdfbox.multipdf.PDFMergerUtility.appendDocument(PDFMergerUtility.java:366)
        at org.apache.pdfbox.multipdf.PDFMergerUtility.mergeDocuments(PDFMergerUtility.java:261)
...

Relevant code from our side is:
...
try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
      PDFMergerUtility mergerUtility = new PDFMergerUtility();
      mergerUtility.addSource(new ByteArrayInputStream(pdfFileByteArray);
      mergerUtility.addSource(new ByteArrayInputStream(anotherPdfFileByteArray);
      mergerUtility.setDestinationStream(out);
      mergerUtility.mergeDocuments(MemoryUsageSetting.setupTempFileOnly());
...

Digging into the code, I think I have a suspicion of what could be the issue.

...
COSArray destFields = (COSArray) destAcroForm.getCOSObject().getItem(COSName.FIELDS);
for (PDField srcField : srcAcroForm.getFieldTree())
{
      ...
      destFields.add(dstField); // exception happens here
}
...

destfields is null because the destination COSObject/Dictionary has no Fields key, but the source does.
The destination pdf file is created earlier in the PDDocument constructor, but the CosName.FIELDS is not added to it.

Is this a bug, or an improper usage of the PDFBox?
We are using PDFBox 2.0.1. The closest issue I could find was this: https://issues.apache.org/jira/browse/PDFBOX-3663
I updated PDFBox to 2.0.5 and checked the code after the update, but it seems the constructor still does not set that value.

Kind Regards,

Forgács Péter
Junior Java Developer
[Appello logo]
Madách Trade Center, A building, 2. floor

H-1075 Budapest, Madách Imre út 13-14.
Tel: +36 1 474 0915
peter.forgacs@appellu.eu
www.appello.eu<http://www.appello.eu>
    [cid:image002.png@01D75BB1.4F3DD3F0] <https://www.linkedin.com/company/appello---banking-it>     [cid:image003.png@01D75BB1.4F3DD3F0] <https://www.facebook.com/ApPello-Banking-Software-1500303710198451/>

[finovateeurope2019_banner4]<https://appello.eu/appello-demo-presentation-at-finovate-europe-2019/>



AW: Possible NPE when merging a PDF with fillable fields

Posted by Tilman Hausherr <TH...@t-online.de>.
Please retry with the current version 2.0.23. Why did you try with 2.0.5? 
It is four years old.

Tilman


--- Original-Nachricht ---
Von: Forgács Péter
Betreff: Possible NPE when merging a PDF with fillable fields
Datum: 08. Juni 2021, 13:04
An: users@pdfbox.apache.org



Hello

One of our users in production received an error with the following stack trace:

java.lang.NullPointerException
     
   
       

        at 
org.apache.pdfbox.multipdf.PDFMergerUtility.mergeAcroForm(PDFMergerUtility.java:678)

        at 
org.apache.pdfbox.multipdf.PDFMergerUtility.appendDocument(PDFMergerUtility.java:366)

        at 
org.apache.pdfbox.multipdf.PDFMergerUtility.mergeDocuments(PDFMergerUtility.java:261)

…

 

Relevant code from our side is:
...

try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {

      PDFMergerUtility mergerUtility = new PDFMergerUtility();

      mergerUtility.addSource(new ByteArrayInputStream(pdfFileByteArray);

      mergerUtility.addSource(new 
ByteArrayInputStream(anotherPdfFileByteArray);

      mergerUtility.setDestinationStream(out);

      mergerUtility.mergeDocuments(MemoryUsageSetting.setupTempFileOnly());

...

Digging into the code, I think I have a suspicion of what could be the 
issue.

 

...

COSArray destFields = (COSArray) 
destAcroForm.getCOSObject().getItem(COSName.FIELDS);

for (PDField srcField : srcAcroForm.getFieldTree())

{

      ...

      destFields.add(dstField); // exception happens here

}

...

 

destfields is null because the destination COSObject/Dictionary has no 
Fields key, but the source does.
The destination pdf file is created earlier in the PDDocument constructor, 
but the CosName.FIELDS is not added to it.

Is this a bug, or an improper usage of the PDFBox?
We are using PDFBox 2.0.1. The closest issue I could find was this: 
https://issues.apache.org/jira/browse/PDFBOX-3663
<https://issues.apache.org/jira/browse/PDFBOX-3663>
I updated PDFBox to 2.0.5 and checked the code after the update, but it 
seems the constructor still does not set that value.

Kind Regards,

 

Forgács Péter

Junior Java Developer

[Appello logo]

Madách Trade Center, A building, 2. floor

 

H-1075 Budapest, Madách Imre út 13-14.

Tel: +36 1 474 0915

peter.forgacs@appellu.eu

www.appello.eu <http://www.appello.eu>

    <https://www.linkedin.com/company/appello---banking-it>     
<https://www.facebook.com/ApPello-Banking-Software-1500303710198451/>

 

[finovateeurope2019_banner4]
<https://appello.eu/appello-demo-presentation-at-finovate-europe-2019/>