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

Tilman Hausherr edited comment on PDFBOX-3328 at 4/26/16 6:05 PM:
------------------------------------------------------------------

I always thought that {{importPage}} does a deep copy (I even changed the 
javadoc not long ago in r1731095) but it doesn't, but it should. Currently it 
just copies the content stream (probably) and this isn't enough.

One solution would be to make {{PDFCloneUtility}} public and use it. Other 
ideas? It was made non public in PDFBOX-2680.

{code}
    public PDPage importPage(PDPage page) throws IOException
    {
        PDFCloneUtility cloner = new PDFCloneUtility(this);
        PDPage importedPage = new PDPage((COSDictionary) 
cloner.cloneForNewDocument(page.getCOSObject()), resourceCache);
        InputStream in = null;
        try
        {
            in = page.getContents();
            if (in != null)
            {
                PDStream dest = new PDStream(this, in, COSName.FLATE_DECODE);
                importedPage.setContents(dest);
            }
            addPage(importedPage);
        }
        catch (IOException e)
        {
            IOUtils.closeQuietly(in);
        }
        return importedPage;
    }
{code}


was (Author: tilman):
I always thought that {{importPage}} does a deep copy (I even changed the 
javadoc not long ago in r1731095) but it doesn't, but it should. Currently it 
just copies the content stream (probably) and this isn't enough.

One solution would be to make {{PDFCloneUtility}} public and use it. Other 
ideas? It was made non public in PDFBOX-2680.

> "COSStream has been closed and cannot be read" despite using importPage
> -----------------------------------------------------------------------
>
>                 Key: PDFBOX-3328
>                 URL: https://issues.apache.org/jira/browse/PDFBOX-3328
>             Project: PDFBox
>          Issue Type: Bug
>    Affects Versions: 2.0.0, 2.0.1, 2.1.0
>            Reporter: Tilman Hausherr
>
> By Simon G. on the user mailing list:
> {quote}
> I'm trying to copy a page from a PDF and adding it to a new (empty) document 
> but I'm running into an issue when I close the source PDF file before I save 
> the destination file.
> Here's a code snippet:
> {code}
>     // Load up some document from which to copy a page
>     PDDocument documentA = PDDocument.load(new File("a.pdf"));
>     // Create a new document and import the first page of the a document
>     PDDocument newDocument = new PDDocument();
>     newDocument.importPage(documentA.getPage(0));
>     // Close the A document as we don't need it any longer
>     documentA.close();
>     // Save the new document, this throws:
>     // COSStream has been closed and cannot be read. Perhaps its enclosing 
> PDDocument has been closed?
>     // java.io.IOException: COSStream has been closed and cannot be read. 
> Perhaps its enclosing PDDocument has been closed?
>     newDocument.save(new File("new.pdf"));
> {code}
> I get a similar exception when using the PDFMergerUtility class.
> I need to copy quite a few pages into a single output document and I'd like 
> to avoid keeping all those source documents open both at the source and in 
> the destination document.
> {quote}



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

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to