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

Saving merged documents causes IOException

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 2.0.0
    • 2.0.0
    • Writing
    • None
    • windows 7

    Description

      When some documents are used within a merge, it is not more possible to save the resulting PDDocument, calling method PDDocument.save() throws "java.io.IOException: COSStream has been closed and cannot be read. Perhaps its enclosing PDDocument has been closed?"

      Exception in thread "main" java.io.IOException: COSStream has been closed and cannot be read. Perhaps its enclosing PDDocument has been closed?
      	at org.apache.pdfbox.cos.COSStream.getFilteredStream(COSStream.java:163)
      	at org.apache.pdfbox.pdfwriter.COSWriter.visitFromStream(COSWriter.java:1147)
      	at org.apache.pdfbox.cos.COSStream.accept(COSStream.java:280)
      	at org.apache.pdfbox.cos.COSObject.accept(COSObject.java:158)
      	at org.apache.pdfbox.pdfwriter.COSWriter.doWriteObject(COSWriter.java:538)
      	at org.apache.pdfbox.pdfwriter.COSWriter.doWriteBody(COSWriter.java:450)
      	at org.apache.pdfbox.pdfwriter.COSWriter.visitFromDocument(COSWriter.java:1031)
      	at org.apache.pdfbox.cos.COSDocument.accept(COSDocument.java:464)
      	at org.apache.pdfbox.pdfwriter.COSWriter.write(COSWriter.java:1307)
      	at org.apache.pdfbox.pdfwriter.COSWriter.write(COSWriter.java:1215)
      	at org.apache.pdfbox.pdmodel.PDDocument.save(PDDocument.java:944)
      	at ch.ge.afc.ael.commun.piecejointe.Test.main(Test.java:37)
      

      For instance, problem occures with testPopupAnnotation.pdf when I try to merge it to testPDF.pdf (see http://svn.apache.org/repos/asf/tika/trunk/tika-parsers/src/test/resources/test-documents/ for these 2 documents)

      Test.java
      public static void main(String[] args) throws Exception {
      	File dir = new File("D:/temp/b");
      
      	PDFMergerUtility pdfMerge = new PDFMergerUtility();
      	PDDocument targetDocument = new PDDocument();
      
      
      	PDFParser ns = new PDFParser(new File(dir, "testPDF.pdf"));
      	ns.parse();
      	PDDocument pddDocument1 = ns.getPDDocument();
      	pdfMerge.appendDocument(targetDocument, pddDocument1);
      	pddDocument1.close();
      
      	ns = new PDFParser(new File(dir, "testPopupAnnotation.pdf"));
      	ns.parse();
      	PDDocument pddDocument2 = ns.getPDDocument();
      	pdfMerge.appendDocument(targetDocument, pddDocument2);
      	pddDocument2.close();
      
      	pdfMerge.mergeDocuments();
      
      	FileOutputStream fout = new FileOutputStream(new File(dir, "targetDocument.pdf"));
      	targetDocument.save(fout);
      
      }
      
      

      Attachments

        Activity

          People

            tilman Tilman Hausherr
            jgi74 jerome girardini
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: