Am 01.03.2017 um 12:29 schrieb Thad Humphries:
On Wed, Mar 1, 2017 at 3:29 AM, Tilman Hausherr <[email protected]>
wrote:

Am 28.02.2017 um 23:51 schrieb Thad Humphries:

No, the document has not been closed prematurely.

and what's that?

inDoc.close();

Well how about that?! When I comment out closing the second document, it
works. Why? I've merged many PDFs, and all work when inDoc is closed.


Hi,

The probably reason is that the merged document uses some resources of the original documents. Maybe it's a bug, maybe not (if we clone too much the files may get too big); but the point is that if you close the original document too early (by closing actively, or by letting the objects running out of scope) you close parts that have to stay open. Solution: first save and close your destination document, then close your source documents. The downside is that it will use more memory.

Tilman



inDoc is the second file (source; my odd file, moroccan_chicken.pdf). It's
the second parameter to PDFMergerUtility appendDocument():

public void appendDocument(PDDocument
<http://localhost/~thad/pdfbox/pdfbox-2.0.4/org/apache/pdfbox/pdmodel/PDDocument.html>
destination,
                   PDDocument
<http://localhost/~thad/pdfbox/pdfbox-2.0.4/org/apache/pdfbox/pdmodel/PDDocument.html>
source)
                     throws IOException
<http://download.oracle.com/javase/1.6.0/docs/api/java/io/IOException.html?is-external=true>

PDFDocument document is destination.
PDDocument inDoc is source.

inDoc is out of scope by the time document.save() is called.

Is there any harm in keeping inDoc open? There could be *many* instances of
it before I'm done: I open a PDDocument, and add images and other PDFs to
it before sending it to a browser with

     ServletUtils.sendPDFHeader(filename, response); // my utility.
     ServletOutputStream out = response.getOutputStream();
     document.save(out);
     document.close();


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

Reply via email to