You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@pdfbox.apache.org by "Chenyue Gao (JIRA)" <ji...@apache.org> on 2018/04/20 03:36:00 UTC

[jira] [Updated] (PDFBOX-4202) PDDocument is closed before calling close()

     [ https://issues.apache.org/jira/browse/PDFBOX-4202?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Chenyue Gao updated PDFBOX-4202:
--------------------------------
    Description: 
 

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)

  was:
 

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.chenyuesoft.vendorcentral.commands.PrintPackingSlipToPDFCommand.run(PrintPackingSlipToPDFCommand.java:116)
at com.chenyuesoft.vendorcentral.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)


> 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: dev-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: dev-help@pdfbox.apache.org