You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@pdfbox.apache.org by Masseau Olivier <Ol...@inetum.com.INVALID> on 2023/06/12 17:32:00 UTC

Should I close PDPageContentStream in try-finally if I already close PDocument ?

Hello,

Let's say I have this which creates a new Pdf document and adds content to it:
public static void createPdfDocumentAndAddContent() throws Exception {
    PDDocument document = null;
    PDPageContentStream contentStream = null;

    try {
        document = new PDDocument();
        PDPage page = new PDPage();
        document.addPage(page);

        contentStream = new PDPageContentStream(document, page);

        contentStream.setFont(PDType1Font.COURIER, 12);
        contentStream.beginText();
        contentStream.showText("Hello World");
        contentStream.endText();
        contentStream.close();

        // ...

        document.save("pdfBoxHelloWorld.pdf");
        document.close();
    } finally {
        // Necessary to handle exceptions
        IOUtils.closeQuietly(contentStream); // In case an exception occurs, do I really need this or closing the document is enough ?
        IOUtils.closeQuietly(document);
    }
}
Do I really need to close the content stream in the finally block or closing the document is enough ?
From all the examples I see online it is hard to tell as 99% of the examples do not care about potential errors.
The rare examples I see where they handle errors, only the document is closed. But the content stream being a Closeable for me it also needs to be closed, not knowing what exactly document.close() does and if it is enough.

Thanks a lot.


Re: Should I close PDPageContentStream in try-finally if I already close PDocument ?

Posted by Tilman Hausherr <TH...@t-online.de>.
Sorry, ignore previous answer.

It's a good practice to use try-with-resources everywhere to avoid 
potential memory leaks. I don't know if you will get any.

Tilman

On 13.06.2023 04:29, Tilman Hausherr wrote:
> On 12.06.2023 19:32, Masseau Olivier wrote:
>> Do I really need to close the content stream in the finally block or 
>> closing the document is enough ?
>
> Depends if you want to end up with a blank page or not
>
> https://pdfbox.apache.org/2.0/faq.html#i'm-creating-a-pdf-but-my-page-is-empty.-why%3F 
>
>
> if you don't close before saving, you'll do.
>
>
> ---------------------------------------------------------------------
> 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: Should I close PDPageContentStream in try-finally if I already close PDocument ?

Posted by Tilman Hausherr <TH...@t-online.de>.
On 12.06.2023 19:32, Masseau Olivier wrote:
> Do I really need to close the content stream in the finally block or closing the document is enough ?

Depends if you want to end up with a blank page or not

https://pdfbox.apache.org/2.0/faq.html#i'm-creating-a-pdf-but-my-page-is-empty.-why%3F

if you don't close before saving, you'll do.


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


RE: Should I close PDPageContentStream in try-finally if I already close PDocument ?

Posted by Masseau Olivier <Ol...@inetum.com.INVALID>.
The thing is that I am doing a code review of a large code base and I see that developpers forgot to close the PDPageContentStream in thousands of places. They only closed the PDocument.

That's way I would like to know if it is strictly necessary or not when the PDocument is closed, as the devs ask me if it is really necessary.

Adding comments for the code review and having the devs fix the code could take a lot of precious time.

-----Message d'origine-----
De : Gilad Denneboom <gi...@gmail.com> 
Envoyé : lundi 12 juin 2023 22:21
À : users@pdfbox.apache.org
Objet : Re: Should I close PDPageContentStream in try-finally if I already close PDocument ?

[Vous ne recevez pas souvent de courriers de gilad.denneboom@gmail.com. Découvrez pourquoi ceci est important à https://aka.ms/LearnAboutSenderIdentification ]

Is there any good reason to not close it?
Even if it was not strictly necessary, what's the difference in processing time? Probably negligible.

On Mon, Jun 12, 2023 at 7:32 PM Masseau Olivier <Ol...@inetum.com.invalid> wrote:

> Hello,
>
> Let's say I have this which creates a new Pdf document and adds 
> content to
> it:
> public static void createPdfDocumentAndAddContent() throws Exception {
>     PDDocument document = null;
>     PDPageContentStream contentStream = null;
>
>     try {
>         document = new PDDocument();
>         PDPage page = new PDPage();
>         document.addPage(page);
>
>         contentStream = new PDPageContentStream(document, page);
>
>         contentStream.setFont(PDType1Font.COURIER, 12);
>         contentStream.beginText();
>         contentStream.showText("Hello World");
>         contentStream.endText();
>         contentStream.close();
>
>         // ...
>
>         document.save("pdfBoxHelloWorld.pdf");
>         document.close();
>     } finally {
>         // Necessary to handle exceptions
>         IOUtils.closeQuietly(contentStream); // In case an exception 
> occurs, do I really need this or closing the document is enough ?
>         IOUtils.closeQuietly(document);
>     }
> }
> Do I really need to close the content stream in the finally block or 
> closing the document is enough ?
> From all the examples I see online it is hard to tell as 99% of the 
> examples do not care about potential errors.
> The rare examples I see where they handle errors, only the document is 
> closed. But the content stream being a Closeable for me it also needs 
> to be closed, not knowing what exactly document.close() does and if it is enough.
>
> Thanks a lot.
>
>

Re: Should I close PDPageContentStream in try-finally if I already close PDocument ?

Posted by Gilad Denneboom <gi...@gmail.com>.
Is there any good reason to not close it?
Even if it was not strictly necessary, what's the difference in processing
time? Probably negligible.

On Mon, Jun 12, 2023 at 7:32 PM Masseau Olivier
<Ol...@inetum.com.invalid> wrote:

> Hello,
>
> Let's say I have this which creates a new Pdf document and adds content to
> it:
> public static void createPdfDocumentAndAddContent() throws Exception {
>     PDDocument document = null;
>     PDPageContentStream contentStream = null;
>
>     try {
>         document = new PDDocument();
>         PDPage page = new PDPage();
>         document.addPage(page);
>
>         contentStream = new PDPageContentStream(document, page);
>
>         contentStream.setFont(PDType1Font.COURIER, 12);
>         contentStream.beginText();
>         contentStream.showText("Hello World");
>         contentStream.endText();
>         contentStream.close();
>
>         // ...
>
>         document.save("pdfBoxHelloWorld.pdf");
>         document.close();
>     } finally {
>         // Necessary to handle exceptions
>         IOUtils.closeQuietly(contentStream); // In case an exception
> occurs, do I really need this or closing the document is enough ?
>         IOUtils.closeQuietly(document);
>     }
> }
> Do I really need to close the content stream in the finally block or
> closing the document is enough ?
> From all the examples I see online it is hard to tell as 99% of the
> examples do not care about potential errors.
> The rare examples I see where they handle errors, only the document is
> closed. But the content stream being a Closeable for me it also needs to be
> closed, not knowing what exactly document.close() does and if it is enough.
>
> Thanks a lot.
>
>