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?