Uploaded image for project: 'PDFBox'
  1. PDFBox
  2. PDFBOX-4908

PDFMergerUtility.mergeInto() does not deep copy metadata

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Open
    • Minor
    • Resolution: Unresolved
    • 2.0.18, 2.0.20
    • None
    • Utilities
    • None
    • Windows, JDK12

    Description

      After merging two documents, closing the source document prevents the destination document from being saved.

      // mainDoc can be any existing PDF
      PDDocument mainDoc = PDDocument.load(new File("blank.pdf"));
      PDDocument appendDoc = PDDocument.load(new File("bad1.pdf"));
      //PDDocument appendDoc = PDDocument.load(new File("bad2.pdf"));
      
      PDFMergerUtility pdfMerger = new PDFMergerUtility();
      pdfMerger.appendDocument(mainDoc, appendDoc);
      appendDoc.close();
      // Exception thrown during save()
      mainDoc.save("temp.pdf");
      mainDoc.close();
      

      Exception:

      java.io.IOException: COSStream has been closed and cannot be read. Perhaps its enclosing PDDocument has been closed?
      	at org.apache.pdfbox.cos.COSStream.checkClosed(COSStream.java:83)
      	at org.apache.pdfbox.cos.COSStream.createRawInputStream(COSStream.java:133)
      	at org.apache.pdfbox.pdfwriter.COSWriter.visitFromStream(COSWriter.java:1219)
      	at org.apache.pdfbox.cos.COSStream.accept(COSStream.java:404)
      	at org.apache.pdfbox.cos.COSObject.accept(COSObject.java:158)
      	at org.apache.pdfbox.pdfwriter.COSWriter.doWriteObject(COSWriter.java:526)
      	at org.apache.pdfbox.pdfwriter.COSWriter.doWriteObjects(COSWriter.java:464)
      	at org.apache.pdfbox.pdfwriter.COSWriter.doWriteBody(COSWriter.java:448)
      	at org.apache.pdfbox.pdfwriter.COSWriter.visitFromDocument(COSWriter.java:1113)
      	at org.apache.pdfbox.cos.COSDocument.accept(COSDocument.java:449)
      	at org.apache.pdfbox.pdfwriter.COSWriter.write(COSWriter.java:1386)
      	at org.apache.pdfbox.pdfwriter.COSWriter.write(COSWriter.java:1273)
      	at org.apache.pdfbox.pdmodel.PDDocument.save(PDDocument.java:1357)
      	at org.apache.pdfbox.pdmodel.PDDocument.save(PDDocument.java:1328)
      	at org.apache.pdfbox.pdmodel.PDDocument.save(PDDocument.java:1316)
      	at Main.main(Main.java:60)
      

      Attached are two different PDFs, from different sources, that both cause the bug.  All sensitive data has been removed, so the PDFs only contain blank pages, but the structure is still present which causes the above Exception.  Also attached is blank.pdf (another blank doc) that I've been testing with as the destination.

      The cause seems to be these lines in PDFMergerUtility:

       PDDocumentInformation destInfo = destination.getDocumentInformation();
       PDDocumentInformation srcInfo = source.getDocumentInformation();
       mergeInto(srcInfo.getCOSObject(), destInfo.getCOSObject(), Collections.<COSName>emptySet());
      
      

      I've tried altering the code to use PDFCloneUtility to clone the srcInfo.getCOSObject() before passing it to mergeInto().  That seems to fix the issue, but I'm not familiar enough with the code to say if that is the correct way to fix this.

       

      Attachments

        1. bad1.pdf
          4 kB
          Tim Shaffer
        2. bad2.pdf
          6 kB
          Tim Shaffer
        3. blank.pdf
          29 kB
          Tim Shaffer

        Activity

          People

            Unassigned Unassigned
            Tim Shaffer Tim Shaffer
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated: