Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Tue Jan 17, 2017 5:12 pm

Hi,

What is the intended use of Dispose() for a PdfTrueTypeFont? Is it necessary to dispose of PdfTrueTypeFonts before disposing the actual PdfDocument? It appears that disposing of a PdfTrueTypeFont before a PdfDocument is saved can lead to side effects in the PdfDocument. Here is an example:

Code: Select all
PdfDocument document = new PdfDocument();
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 12f, FontStyle.Italic));
document.Pages.Add();
document.Pages[0].Canvas.DrawString(
        "Hello there!", font, PdfBrushes.Black, new System.Drawing.PointF(0, 0));

document.SaveToFile(@"C:\FontUndisposed.pdf");   // displays as italic
font.Dispose();
document.SaveToFile(@"C:\FontDisposed.pdf");   // displays as regular

document.Dispose();


I've written a complex recursive method to render a PdfDocument from XML, and it would be unfeasible to wait until the PdfDocument itself was disposed before disposing of all the PdfTrueTypeFonts used by it. So my question is, is it safe to leave the PdfTrueTypeFonts undisposed and call Dispose() only on the PdfDocument itself?

Scott7261
 
Posts: 7
Joined: Fri Feb 05, 2016 7:47 pm

Wed Jan 18, 2017 6:29 am

Dear Scott7261,

Thanks for your inquiry.
There will be a font cache when creating a font. The method Dispose() for PdfTrueTypeFont is to dispose the font cache for that font. And while saving the document, it will visit the font cache. So the font is incorrect if you dispose the font before saving. If you want to dispose the font cache, please do that after saving document.
If there is any question, welcome to get it back to us.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Wed Jan 18, 2017 3:39 pm

Hi Betsy,

Thank you for your response. Since Dispose() methods are typically used to clean up unmanaged resources, my next question is: will it be safe to call Dispose() on the PdfDocument itself without calling Dispose() on the PdfTrueTypeFont used by it? Or, if I called Dispose() only on the PdfDocument, would there still be a risk of memory leaks or problems concerning the use of unmanaged resources? If there would be such a risk, is there a way to dispose of the font cache from the PdfDocument as the calling object rather than the PdfTrueTypeFont?

Scott7261
 
Posts: 7
Joined: Fri Feb 05, 2016 7:47 pm

Thu Jan 19, 2017 6:19 am

Dear Scott7261,

Thanks for your inquiry.
I have posted your requirement that only need to dispose PdfDocument with the font cache also is disposed to our Dev team.
Once there is any news, we will let you know.
Sorry for inconvenience caused.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Return to Spire.PDF