pdfbox-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Simon Gaeremynck <gaeremyn...@gmail.com>
Subject Copy a page to a new PDDocument
Date Tue, 26 Apr 2016 06:08:08 GMT
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


Mime
View raw message