You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@pdfbox.apache.org by "Maruan Sahyoun (JIRA)" <ji...@apache.org> on 2015/04/01 10:48:53 UTC

[jira] [Commented] (PDFBOX-2739) Saving merged documents causes IOException

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

Maruan Sahyoun commented on PDFBOX-2739:
----------------------------------------

Hi,

the reason is that you are closing pddDocument1 before the target document has been saved. In addition the code could be simplified doing

{code}

        PDFMergerUtility merger = new PDFMergerUtility();
        merger.addSource("testPDF.pdf");
        merger.addSource("testPopupAnnotation.pdf");
        String destinationFileName = args[args.length-1];
        merger.setDestinationFileName("targetDocument.pdf");
        merger.mergeDocuments();
{code}

this way merger will handle the closing of the source documents for you.



> Saving merged documents causes IOException
> ------------------------------------------
>
>                 Key: PDFBOX-2739
>                 URL: https://issues.apache.org/jira/browse/PDFBOX-2739
>             Project: PDFBox
>          Issue Type: Bug
>          Components: Writing
>    Affects Versions: 2.0.0
>         Environment: windows 7
>            Reporter: jerome girardini
>
> 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?"
> {code:borderStyle=solid}
> 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)
> {code}
> 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)
> {code:title=Test.java|borderStyle=solid}
> 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);
> }
> {code}



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

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