[
https://issues.apache.org/jira/browse/PDFBOX-4202?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16445309#comment-16445309
]
Chenyue Gao edited comment on PDFBOX-4202 at 4/20/18 5:09 AM:
--------------------------------------------------------------
Thank you for the quick response.
I'm using JDK 1.8.0_65
Yes, the caller call close() on the mainDocument
The logic is like
{code:java}
main(){
PDDocument mainDocument= new PDDocument();
for(int i=0;i<totalPageNum;i++)
{
File file= new File("path/to/file_"+i+".pdf");
addPDPage(mainDocument, file, i);
}
mainDocument.save(new File("path/to/output.pdf"));
mainDocument.close();
}
{code}
The exception is thrown at the line marked in red
By the way, I tried the workaround. It worked for me
{code:java}
main(){
List<PDDocument> pdDocumentRefs= new ArrayList<PDDocument>();
PDDocument mainDocument= new PDDocument();
for(int i=0;i<totalPageNum;i++)
{
File file= new File("path/to/file_"+i+".pdf");
pdDocumentRefs.add(addPDPage(mainDocument, file, i));
}
mainDocument.save(new File("path/to/output.pdf"));
mainDocument.close();
}
{code}
addPDPage is changed to return the local PDDocument
was (Author: cgao):
Thank you for the quick response.
I'm using JDK 1.8.0_65
Yes, the caller call close() on the mainDocument
The logic is like
{code:java}
main(){
PDDocument mainDocument= new PDDocument();
for(int i=0;i<totalPageNum;i++)
{
File file= new File("path/to/file_"+i+".pdf");
addPDPage(mainDocument, file, i);
}
mainDocument.save(new File("path/to/output.pdf"));
mainDocument.close();
}
{code}
The exception is thrown at the line marked in red
By the way, I tried the workaround. It worked for me
main(){
List<PDDocument> pdDocumentRefs= new ArrayList<PDDocument>();
PDDocument mainDocument= new PDDocument();
for(int i=0;i<totalPageNum;i++)
{ File file= new File("path/to/file_"+i+".pdf");
pdDocumentRefs.add(addPDPage(mainDocument, file, i)); }
{color:#333333} mainDocument.save(new File("path/to/output.pdf"));{color}
mainDocument.close();
}
addPDPage is changed to return the local PDDocument
> PDDocument is closed before calling close()
> -------------------------------------------
>
> Key: PDFBOX-4202
> URL: https://issues.apache.org/jira/browse/PDFBOX-4202
> Project: PDFBox
> Issue Type: Bug
> Components: PDModel
> Affects Versions: 2.0.9
> Environment: WIndow 10 x64
> Reporter: Chenyue Gao
> Priority: Critical
>
>
> The following code append PDDocument read from a file to the mainDocument
> which is passed as parameter. When I save the mainDocument, it throws the
> exception below.
> {code:java}
> public static void addPDPage(PDDocument mainDocument, File file, int pagenum)
> throws IOException {
> PDDocument pkDocument = PDDocument.load(file);
> PDPageTree pdpageTree = pkDocument.getPages();
> pdpageTree.forEach(page -> {
> mainDocument.addPage(page);
> });
> }
> {code}
> It seems that the pkDocument inside this function automatically closed itself
> due to Java garbage collection. As a result the mainDocument can't save the
> page associated with the pkDocument.
> A workaround could be to return pkDcoument to the caller and keep the
> reference at the same level of mainDocument until mainDocument saves
>
>
> [2018-04-16 11:11:58] COSStream has been closed and cannot be read. Perhaps
> its enclosing PDDocument has been closed?
> [2018-04-16 11:11:58] 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:77)
> at org.apache.pdfbox.cos.COSStream.createRawInputStream(COSStream.java:125)
> at org.apache.pdfbox.pdfwriter.COSWriter.visitFromStream(COSWriter.java:1200)
> at org.apache.pdfbox.cos.COSStream.accept(COSStream.java:383)
> at org.apache.pdfbox.cos.COSObject.accept(COSObject.java:158)
> at org.apache.pdfbox.pdfwriter.COSWriter.doWriteObject(COSWriter.java:522)
> at org.apache.pdfbox.pdfwriter.COSWriter.doWriteObjects(COSWriter.java:460)
> at org.apache.pdfbox.pdfwriter.COSWriter.doWriteBody(COSWriter.java:444)
> at
> org.apache.pdfbox.pdfwriter.COSWriter.visitFromDocument(COSWriter.java:1096)
> at org.apache.pdfbox.cos.COSDocument.accept(COSDocument.java:419)
> at org.apache.pdfbox.pdfwriter.COSWriter.write(COSWriter.java:1367)
> at org.apache.pdfbox.pdfwriter.COSWriter.write(COSWriter.java:1254)
> at org.apache.pdfbox.pdmodel.PDDocument.save(PDDocument.java:1232)
> at org.apache.pdfbox.pdmodel.PDDocument.save(PDDocument.java:1204)
> at
> com.commands.PrintPackingSlipToPDFCommand.run(PrintPackingSlipToPDFCommand.java:116)
> at com.commands.AbstractCommand.execute(AbstractCommand.java:74)
> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
> at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
> at java.lang.reflect.Method.invoke(Unknown Source)
--
This message was sent by Atlassian JIRA
(v7.6.3#76005)
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]