You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@pdfbox.apache.org by Thad Humphries <th...@gmail.com> on 2017/06/21 21:30:52 UTC

PDPageContentStream#close() vs PDDocument#close()

Is it necessary to call PDDocument#close() after calling
PDPageContentStream#close()? Does the answer apply all cases or only
certain cases? If the latter, what certain cases?

For example, in the following code snippet:

PDDocument  document = new PDDocument();
PDPageContentStream cos = new PDPageContentStream(document, 0);
cos.drawImage(... , etc.
cos.close();
document.close();


Is the last line, `document.close()`, necessary, or has that been handled
sufficiently by the `cos.close()` immediately before it?

Finally, I'm assuming that it is safe to call #close() a second (or third?)
time on a PDDocument or PDPageContentStream. Is that correct? My use case
would be in the finally block where an exception might have left PDDocument
or PDPageContentStream open.

-- 
"Hell hath no limits, nor is circumscrib'd In one self-place; but where we
are is hell, And where hell is, there must we ever be" --Christopher
Marlowe, *Doctor Faustus* (v. 121-24)

Re: PDPageContentStream#close() vs PDDocument#close()

Posted by Tilman Hausherr <TH...@t-online.de>.
Am 21.06.2017 um 23:30 schrieb Thad Humphries:
> Is it necessary to call PDDocument#close() after calling
> PDPageContentStream#close()? Does the answer apply all cases or only
> certain cases? If the latter, what certain cases?

Yes it is needed to close your document too. If you don't, you'll risk 
memory leaks and other weird effects. The best is to use jdk7 
try-with-resources.

>
> For example, in the following code snippet:
>
> PDDocument  document = new PDDocument();
> PDPageContentStream cos = new PDPageContentStream(document, 0);
> cos.drawImage(... , etc.
> cos.close();
> document.close();
>
>
> Is the last line, `document.close()`, necessary, or has that been handled
> sufficiently by the `cos.close()` immediately before it?

Yes it is needed.

>
> Finally, I'm assuming that it is safe to call #close() a second (or third?)
> time on a PDDocument or PDPageContentStream. Is that correct? My use case
> would be in the finally block where an exception might have left PDDocument
> or PDPageContentStream open.
>

And yes you can close several times, the Closeable interface requires 
this. Although I'd suggest to work on your design (if still possible) if 
you're not sure where you're closing. IMHO it is always best to close 
where you open. And yes, even PDFBox is "guilty" at some places....

Tilman


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


Re: PDPageContentStream#close() vs PDDocument#close()

Posted by Andreas Lehmkühler <an...@lehmi.de>.
> Thad Humphries <th...@gmail.com> hat am 21. Juni 2017 um 23:30 geschrieben:
> 
> 
> Is it necessary to call PDDocument#close() after calling
> PDPageContentStream#close()? Does the answer apply all cases or only
> certain cases? If the latter, what certain cases?
> 
> For example, in the following code snippet:
> 
> PDDocument  document = new PDDocument();
> PDPageContentStream cos = new PDPageContentStream(document, 0);
> cos.drawImage(... , etc.
> cos.close();
> document.close();
> 
> 
> Is the last line, `document.close()`, necessary, or has that been handled
> sufficiently by the `cos.close()` immediately before it?
You have to close both, PDDocument at the end.

> Finally, I'm assuming that it is safe to call #close() a second (or third?)
> time on a PDDocument or PDPageContentStream. Is that correct? My use case
> would be in the finally block where an exception might have left PDDocument
> or PDPageContentStream open.
It shouldn't be a problem to close PDDocument several times. I'm not sure about PDPageContentStream

Andreas
> 
> -- 
> "Hell hath no limits, nor is circumscrib'd In one self-place; but where we
> are is hell, And where hell is, there must we ever be" --Christopher
> Marlowe, *Doctor Faustus* (v. 121-24)

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