yes, but you still have coverDoc.close() in the if branch after doc.addPage()
BR Maruan Am 17.02.2015 um 22:08 schrieb Ilya Kantor <[email protected]>: > Hi, > > Still getting the error > > That's the updated code: > ================= > PDDocument doc = PDDocument.load(inputFile); > PDDocument coverDoc = null; > > if (coverFile != null) { > coverDoc = PDDocument.load(coverFile); > PDPage coverPage = coverDoc.getPage(0); > doc.addPage(coverPage); > coverDoc.close(); > } > > PDDocumentOutline outline = new PDDocumentOutline(); > // ... > doc.getDocumentCatalog().setDocumentOutline(outline); > > doc.save(outputFile); > if (coverDoc != null) coverDoc.close(); > ================= > > I'm getting the error in doc.save(): > ================= > 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:1151) > at org.apache.pdfbox.cos.COSStream.accept(COSStream.java:298) > at org.apache.pdfbox.cos.COSObject.accept(COSObject.java:156) > at org.apache.pdfbox.pdfwriter.COSWriter.doWriteObject(COSWriter.java:542) > at org.apache.pdfbox.pdfwriter.COSWriter.doWriteBody(COSWriter.java:449) > at > org.apache.pdfbox.pdfwriter.COSWriter.visitFromDocument(COSWriter.java:1035) > at org.apache.pdfbox.cos.COSDocument.accept(COSDocument.java:464) > at org.apache.pdfbox.pdfwriter.COSWriter.write(COSWriter.java:1312) > at org.apache.pdfbox.pdfwriter.COSWriter.write(COSWriter.java:1220) > at org.apache.pdfbox.pdmodel.PDDocument.save(PDDocument.java:950) > at org.apache.pdfbox.pdmodel.PDDocument.save(PDDocument.java:922) > at PdfBookPolisher.run(PdfBookPolisher.java:129) > at PdfBookPolisher.main(PdfBookPolisher.java:69) > at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) > at > sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) > at > sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) > at java.lang.reflect.Method.invoke(Method.java:606) > at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134) > ================ > > --- > Best Regards, > Ilya Kantor > > 2015-02-18 0:05 GMT+03:00 Maruan Sahyoun <[email protected]>: > >> try closing the coverDoc after you've saved doc. >> >> BR >> Maruan >> >> Am 17.02.2015 um 22:00 schrieb Ilya Kantor <[email protected]>: >> >>> Hi, >>> >>> I'm trying to import a page from one document into another one and >> getting >>> an error with PDFBox 2.0.0 snapshot. >>> >>> That's the code: >>> ================== >>> PDDocument doc = PDDocument.load(inputFile); >>> >>> if (coverFile != null) { >>> PDDocument coverDoc = PDDocument.load(coverFile); >>> PDPage coverPage = coverDoc.getPage(0); >>> doc.importPage(coverPage); >>> coverDoc.close(); >>> } >>> >>> PDDocumentOutline outline = new PDDocumentOutline(); >>> // ... I'm also working with the outline >>> doc.getDocumentCatalog().setDocumentOutline(outline); >>> >>> doc.save(outputFile); >>> ================= >>> >>> And here's the error when saving (the last line): >>> >>> ================ >>> 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:1151) >>> at org.apache.pdfbox.cos.COSStream.accept(COSStream.java:298) >>> at org.apache.pdfbox.cos.COSObject.accept(COSObject.java:156) >>> at >> org.apache.pdfbox.pdfwriter.COSWriter.doWriteObject(COSWriter.java:542) >>> at org.apache.pdfbox.pdfwriter.COSWriter.doWriteBody(COSWriter.java:449) >>> at >>> >> org.apache.pdfbox.pdfwriter.COSWriter.visitFromDocument(COSWriter.java:1035) >>> at org.apache.pdfbox.cos.COSDocument.accept(COSDocument.java:464) >>> at org.apache.pdfbox.pdfwriter.COSWriter.write(COSWriter.java:1312) >>> at org.apache.pdfbox.pdfwriter.COSWriter.write(COSWriter.java:1220) >>> at org.apache.pdfbox.pdmodel.PDDocument.save(PDDocument.java:950) >>> at org.apache.pdfbox.pdmodel.PDDocument.save(PDDocument.java:922) >>> at PdfBookPolisher.run(PdfBookPolisher.java:128) >>> at PdfBookPolisher.main(PdfBookPolisher.java:69) >>> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) >>> at >>> >> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) >>> at >>> >> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) >>> at java.lang.reflect.Method.invoke(Method.java:606) >>> at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134) >>> ====== >>> >>> The problem does not happen when I do not import page. >>> >>> P.S. Can I insert the new page as the first one, not the last one? >>> Something like doc.getDocumentCatalog().getPages().getKids().add(0, >>> (PDPage) cover.getDocumentCatalog().getAllPages().get(0)); >>> But for 2.0.0. >>> >>> >>> --- >>> Best Regards, >>> Ilya Kantor >> >>

