You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@pdfbox.apache.org by Simon Gaeremynck <ga...@gmail.com> on 2016/04/26 08:08:08 UTC

Copy a page to a new PDDocument

Hello,

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:
    // 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"));

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.

Any ideas?

Kind regards,
Simon
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org


Re: Copy a page to a new PDDocument

Posted by Andreas Lehmkuehler <an...@lehmi.de>.
Am 26.04.2016 um 20:06 schrieb Tilman Hausherr:
> Am 26.04.2016 um 15:59 schrieb Simon Gaeremynck:
>> Hi,
>>
>> So far this has happened with any document I’ve tried.
>> Here’s the latest one: https://s3.amazonaws.com/ally-dev/files/e.pdf
>> I’m running pdfbox 2.0.1.
>
> Thanks. I have opened https://issues.apache.org/jira/browse/PDFBOX-3328 .  You
> can also comment there if you're registered (if the lockdown is over).
IMHO the lockdown should be over, at least the banner in JIRA is gone

>
> If you're in a hurry, just copy the source code of PDFCloneUtility and create
> your own importPage as described in the issue.
>
> Tilman
>
>
>
>
>> The full stack trace:
>> 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.checkClosed(COSStream.java:78)
>>     at org.apache.pdfbox.cos.COSStream.createRawInputStream(COSStream.java:126)
>>     at org.apache.pdfbox.pdfwriter.COSWriter.visitFromStream(COSWriter.java:1177)
>>     at org.apache.pdfbox.cos.COSStream.accept(COSStream.java:372)
>>     at org.apache.pdfbox.cos.COSObject.accept(COSObject.java:158)
>>     at org.apache.pdfbox.pdfwriter.COSWriter.doWriteObject(COSWriter.java:561)
>>     at org.apache.pdfbox.pdfwriter.COSWriter.doWriteObjects(COSWriter.java:490)
>>     at org.apache.pdfbox.pdfwriter.COSWriter.doWriteBody(COSWriter.java:474)
>>     at
>> org.apache.pdfbox.pdfwriter.COSWriter.visitFromDocument(COSWriter.java:1073)
>>     at org.apache.pdfbox.cos.COSDocument.accept(COSDocument.java:419)
>>     at org.apache.pdfbox.pdfwriter.COSWriter.write(COSWriter.java:1331)
>>     at org.apache.pdfbox.pdfwriter.COSWriter.write(COSWriter.java:1229)
>>     at org.apache.pdfbox.pdmodel.PDDocument.save(PDDocument.java:1101)
>>     at org.apache.pdfbox.pdmodel.PDDocument.save(PDDocument.java:1073)
>>
>> Thanks!
>> Simon
>>
>>> On 25 Apr 2016, at 23:40, Tilman Hausherr <TH...@t-online.de> wrote:
>>>
>>> Am 26.04.2016 um 08:08 schrieb Simon Gaeremynck:
>>>> Hello,
>>>>
>>>> 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:
>>>>      // 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"));
>>>>
>>>> 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.
>>>>
>>>> Any ideas?
>>> This shouldn't happen when using importPage, as that one should do a deep
>>> copy. Could you upload a PDF somewhere? (Attachments don't work) Please do
>>> also mention what version you are using
>>>
>>> Tilman
>>>
>>>> Kind regards,
>>>> Simon
>>>> ---------------------------------------------------------------------
>>>> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
>>>> For additional commands, e-mail: users-help@pdfbox.apache.org
>>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
>>> For additional commands, e-mail: users-help@pdfbox.apache.org
>>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
>> For additional commands, e-mail: users-help@pdfbox.apache.org
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
> For additional commands, e-mail: users-help@pdfbox.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org


Re: Copy a page to a new PDDocument

Posted by Tilman Hausherr <TH...@t-online.de>.
There will be a new snapshot version soon at
https://repository.apache.org/content/groups/snapshots/org/apache/pdfbox/pdfbox-app/2.0.2-SNAPSHOT/

please retest your application.

Btw you wrote "I get a similar exception when using the PDFMergerUtility 
class". Please post a test for this, if it still happens.

Tilman



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org


Re: Copy a page to a new PDDocument

Posted by Tilman Hausherr <TH...@t-online.de>.
Duplicate of https://issues.apache.org/jira/browse/PDFBOX-3280 :-(

Tilman

Am 26.04.2016 um 20:06 schrieb Tilman Hausherr:
> Am 26.04.2016 um 15:59 schrieb Simon Gaeremynck:
>> Hi,
>>
>> So far this has happened with any document I’ve tried.
>> Here’s the latest one: https://s3.amazonaws.com/ally-dev/files/e.pdf
>> I’m running pdfbox 2.0.1.
>
> Thanks. I have opened 
> https://issues.apache.org/jira/browse/PDFBOX-3328 .  You can also 
> comment there if you're registered (if the lockdown is over).
>
> If you're in a hurry, just copy the source code of PDFCloneUtility and 
> create your own importPage as described in the issue.
>
> Tilman
>
>
>
>
>> The full stack trace:
>> 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.checkClosed(COSStream.java:78)
>>     at 
>> org.apache.pdfbox.cos.COSStream.createRawInputStream(COSStream.java:126)
>>     at 
>> org.apache.pdfbox.pdfwriter.COSWriter.visitFromStream(COSWriter.java:1177)
>>     at org.apache.pdfbox.cos.COSStream.accept(COSStream.java:372)
>>     at org.apache.pdfbox.cos.COSObject.accept(COSObject.java:158)
>>     at 
>> org.apache.pdfbox.pdfwriter.COSWriter.doWriteObject(COSWriter.java:561)
>>     at 
>> org.apache.pdfbox.pdfwriter.COSWriter.doWriteObjects(COSWriter.java:490)
>>     at 
>> org.apache.pdfbox.pdfwriter.COSWriter.doWriteBody(COSWriter.java:474)
>>     at 
>> org.apache.pdfbox.pdfwriter.COSWriter.visitFromDocument(COSWriter.java:1073)
>>     at org.apache.pdfbox.cos.COSDocument.accept(COSDocument.java:419)
>>     at org.apache.pdfbox.pdfwriter.COSWriter.write(COSWriter.java:1331)
>>     at org.apache.pdfbox.pdfwriter.COSWriter.write(COSWriter.java:1229)
>>     at org.apache.pdfbox.pdmodel.PDDocument.save(PDDocument.java:1101)
>>     at org.apache.pdfbox.pdmodel.PDDocument.save(PDDocument.java:1073)
>>
>> Thanks!
>> Simon
>>
>>> On 25 Apr 2016, at 23:40, Tilman Hausherr <TH...@t-online.de> 
>>> wrote:
>>>
>>> Am 26.04.2016 um 08:08 schrieb Simon Gaeremynck:
>>>> Hello,
>>>>
>>>> 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:
>>>>      // 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"));
>>>>
>>>> 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.
>>>>
>>>> Any ideas?
>>> This shouldn't happen when using importPage, as that one should do a 
>>> deep copy. Could you upload a PDF somewhere? (Attachments don't 
>>> work) Please do also mention what version you are using
>>>
>>> Tilman
>>>
>>>> Kind regards,
>>>> Simon
>>>> ---------------------------------------------------------------------
>>>> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
>>>> For additional commands, e-mail: users-help@pdfbox.apache.org
>>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
>>> For additional commands, e-mail: users-help@pdfbox.apache.org
>>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
>> For additional commands, e-mail: users-help@pdfbox.apache.org
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
> For additional commands, e-mail: users-help@pdfbox.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org


Re: Copy a page to a new PDDocument

Posted by Tilman Hausherr <TH...@t-online.de>.
Am 26.04.2016 um 15:59 schrieb Simon Gaeremynck:
> Hi,
>
> So far this has happened with any document I’ve tried.
> Here’s the latest one: https://s3.amazonaws.com/ally-dev/files/e.pdf
> I’m running pdfbox 2.0.1.

Thanks. I have opened https://issues.apache.org/jira/browse/PDFBOX-3328 
.  You can also comment there if you're registered (if the lockdown is 
over).

If you're in a hurry, just copy the source code of PDFCloneUtility and 
create your own importPage as described in the issue.

Tilman




> The full stack trace:
> 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.checkClosed(COSStream.java:78)
> 	at org.apache.pdfbox.cos.COSStream.createRawInputStream(COSStream.java:126)
> 	at org.apache.pdfbox.pdfwriter.COSWriter.visitFromStream(COSWriter.java:1177)
> 	at org.apache.pdfbox.cos.COSStream.accept(COSStream.java:372)
> 	at org.apache.pdfbox.cos.COSObject.accept(COSObject.java:158)
> 	at org.apache.pdfbox.pdfwriter.COSWriter.doWriteObject(COSWriter.java:561)
> 	at org.apache.pdfbox.pdfwriter.COSWriter.doWriteObjects(COSWriter.java:490)
> 	at org.apache.pdfbox.pdfwriter.COSWriter.doWriteBody(COSWriter.java:474)
> 	at org.apache.pdfbox.pdfwriter.COSWriter.visitFromDocument(COSWriter.java:1073)
> 	at org.apache.pdfbox.cos.COSDocument.accept(COSDocument.java:419)
> 	at org.apache.pdfbox.pdfwriter.COSWriter.write(COSWriter.java:1331)
> 	at org.apache.pdfbox.pdfwriter.COSWriter.write(COSWriter.java:1229)
> 	at org.apache.pdfbox.pdmodel.PDDocument.save(PDDocument.java:1101)
> 	at org.apache.pdfbox.pdmodel.PDDocument.save(PDDocument.java:1073)
>
> Thanks!
> Simon
>
>> On 25 Apr 2016, at 23:40, Tilman Hausherr <TH...@t-online.de> wrote:
>>
>> Am 26.04.2016 um 08:08 schrieb Simon Gaeremynck:
>>> Hello,
>>>
>>> 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:
>>>      // 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"));
>>>
>>> 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.
>>>
>>> Any ideas?
>> This shouldn't happen when using importPage, as that one should do a deep copy. Could you upload a PDF somewhere? (Attachments don't work) Please do also mention what version you are using
>>
>> Tilman
>>
>>> Kind regards,
>>> Simon
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
>>> For additional commands, e-mail: users-help@pdfbox.apache.org
>>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
>> For additional commands, e-mail: users-help@pdfbox.apache.org
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
> For additional commands, e-mail: users-help@pdfbox.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org


Re: Copy a page to a new PDDocument

Posted by Simon Gaeremynck <ga...@gmail.com>.
Hi,

So far this has happened with any document I’ve tried.
Here’s the latest one: https://s3.amazonaws.com/ally-dev/files/e.pdf
I’m running pdfbox 2.0.1.
The full stack trace:
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.checkClosed(COSStream.java:78)
	at org.apache.pdfbox.cos.COSStream.createRawInputStream(COSStream.java:126)
	at org.apache.pdfbox.pdfwriter.COSWriter.visitFromStream(COSWriter.java:1177)
	at org.apache.pdfbox.cos.COSStream.accept(COSStream.java:372)
	at org.apache.pdfbox.cos.COSObject.accept(COSObject.java:158)
	at org.apache.pdfbox.pdfwriter.COSWriter.doWriteObject(COSWriter.java:561)
	at org.apache.pdfbox.pdfwriter.COSWriter.doWriteObjects(COSWriter.java:490)
	at org.apache.pdfbox.pdfwriter.COSWriter.doWriteBody(COSWriter.java:474)
	at org.apache.pdfbox.pdfwriter.COSWriter.visitFromDocument(COSWriter.java:1073)
	at org.apache.pdfbox.cos.COSDocument.accept(COSDocument.java:419)
	at org.apache.pdfbox.pdfwriter.COSWriter.write(COSWriter.java:1331)
	at org.apache.pdfbox.pdfwriter.COSWriter.write(COSWriter.java:1229)
	at org.apache.pdfbox.pdmodel.PDDocument.save(PDDocument.java:1101)
	at org.apache.pdfbox.pdmodel.PDDocument.save(PDDocument.java:1073)

Thanks!
Simon

> On 25 Apr 2016, at 23:40, Tilman Hausherr <TH...@t-online.de> wrote:
> 
> Am 26.04.2016 um 08:08 schrieb Simon Gaeremynck:
>> Hello,
>> 
>> 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:
>>     // 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"));
>> 
>> 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.
>> 
>> Any ideas?
> 
> This shouldn't happen when using importPage, as that one should do a deep copy. Could you upload a PDF somewhere? (Attachments don't work) Please do also mention what version you are using
> 
> Tilman
> 
>> 
>> Kind regards,
>> Simon
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
>> For additional commands, e-mail: users-help@pdfbox.apache.org
>> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
> For additional commands, e-mail: users-help@pdfbox.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org


Re: Copy a page to a new PDDocument

Posted by Tilman Hausherr <TH...@t-online.de>.
Am 26.04.2016 um 08:08 schrieb Simon Gaeremynck:
> Hello,
>
> 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:
>      // 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"));
>
> 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.
>
> Any ideas?

This shouldn't happen when using importPage, as that one should do a 
deep copy. Could you upload a PDF somewhere? (Attachments don't work) 
Please do also mention what version you are using

Tilman

>
> Kind regards,
> Simon
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
> For additional commands, e-mail: users-help@pdfbox.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org