Am I seeing this bug? https://issues.apache.org/jira/browse/PDFBOX-3280
If so, I'm not seeing any successful workaround: not with the PDFCloneUtility, not with "importedPage.setResources(pdPage.getResources())", nor with a combination of the two. Might 3.0.0-alpha2 have a solution? On Wed, Oct 20, 2021 at 3:15 PM Thad Humphries <thad.humphr...@gmail.com> wrote: > I am using PDFBox 2.0.24. I am attempting to copy a PDF file to a new > PDDocument and save that new document. The eventual goal is to use Overlay > to overlay various images onto pages of the old document before > adding them to the new document. > > > The problem I'm having is that when I close the old document or any > intermediary document (like one created when making the Overlay), I get an > error "COSStream has been closed and cannot be read" when I save the new > document. > > > I can demonstrate this error simply with just a simple function: > > > @Test > > *public* *void* testCopyAnnotation() *throws* Exception { > > File inPdfFile = *new* File(*this*.getClass().getResource( > "/patent-3_139_857.pdf").toURI()); > > PDDocument inDocument = PDDocument.*load*(inPdfFile); > > PDDocument outDocument = *new* PDDocument(); > > *int* pages = inDocument.getNumberOfPages(); > > *for* (*int* page = 0; page < pages; page++) { > > PDPage pdPage = inDocument.getPage(page); > > outDocument.importPage(pdPage); > > } > > inDocument.close(); > > outDocument.save(*OUT_DIR* + "/out-pdf-patente.pdf"); > > outDocument.close(); > > Assert.*assertTrue*(*true*); > > } > > > The stack trace below is triggered by the line "outDocument.save(*OUT_DIR* > + "/out-pdf-patente.pdf");". > > > What is wrong here and how might I correct this? > > > *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:1268) > > at org.apache.pdfbox.cos.COSStream.accept(COSStream.java:416) > > at org.apache.pdfbox.cos.COSObject.accept(COSObject.java:158) > > at org.apache.pdfbox.pdfwriter.COSWriter.doWriteObject(COSWriter.java:572) > > at org.apache.pdfbox.pdfwriter.COSWriter.doWriteObjects(COSWriter.java:497 > ) > > at org.apache.pdfbox.pdfwriter.COSWriter.doWriteBody(COSWriter.java:481) > > at org.apache.pdfbox.pdfwriter.COSWriter.visitFromDocument( > COSWriter.java:1162) > > at org.apache.pdfbox.cos.COSDocument.accept(COSDocument.java:452) > > at org.apache.pdfbox.pdfwriter.COSWriter.write(COSWriter.java:1435) > > at org.apache.pdfbox.pdfwriter.COSWriter.write(COSWriter.java:1322) > > at org.apache.pdfbox.pdmodel.PDDocument.save(PDDocument.java:1375) > > at org.apache.pdfbox.pdmodel.PDDocument.save(PDDocument.java:1342) > > at org.apache.pdfbox.pdmodel.PDDocument.save(PDDocument.java:1326) > > -->> at com.optix.server.rest.TestFileServlet.testCopyAnnotation( > TestFileServlet.java:152) > > at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) > > at sun.reflect.NativeMethodAccessorImpl.invoke( > NativeMethodAccessorImpl.java:62) > > at sun.reflect.DelegatingMethodAccessorImpl.invoke( > DelegatingMethodAccessorImpl.java:43) > > at java.lang.reflect.Method.invoke(Method.java:498) > > at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall( > FrameworkMethod.java:50) > > at org.junit.internal.runners.model.ReflectiveCallable.run( > ReflectiveCallable.java:12) > > at org.junit.runners.model.FrameworkMethod.invokeExplosively( > FrameworkMethod.java:47) > > at org.junit.internal.runners.statements.InvokeMethod.evaluate( > InvokeMethod.java:17) > > at org.junit.internal.runners.statements.RunBefores.evaluate( > RunBefores.java:26) > > at org.junit.internal.runners.statements.RunAfters.evaluate( > RunAfters.java:27) > > at org.junit.rules.TestWatcher$1.evaluate(TestWatcher.java:55) > > at org.junit.rules.RunRules.evaluate(RunRules.java:20) > > at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) > > at org.junit.runners.BlockJUnit4ClassRunner.runChild( > BlockJUnit4ClassRunner.java:78) > > at org.junit.runners.BlockJUnit4ClassRunner.runChild( > BlockJUnit4ClassRunner.java:57) > > at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) > > at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) > > at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) > > at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) > > at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) > > at org.junit.runners.ParentRunner.run(ParentRunner.java:363) > > at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run( > JUnit4TestReference.java:93) > > at org.eclipse.jdt.internal.junit.runner.TestExecution.run( > TestExecution.java:40) > > at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests( > RemoteTestRunner.java:529) > > at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests( > RemoteTestRunner.java:756) > > at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run( > RemoteTestRunner.java:452) > > at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main( > RemoteTestRunner.java:210) > > > > > -- > "Hell hath no limits, nor is circumscrib'd In one self-place; but where we > are is hell, And where hell is, there must we ever be" --Christopher > Marlowe, *Doctor Faustus* (v. 111-13) > -- "Hell hath no limits, nor is circumscrib'd In one self-place; but where we are is hell, And where hell is, there must we ever be" --Christopher Marlowe, *Doctor Faustus* (v. 111-13)