Font Stream Disposed Before Saving PDF Document
24 Jul 20265 minutes to read
Overview
This error occurs when the font stream is disposed (released from memory) before the PDF document is saved. The Syncfusion® PDF library requires that the font stream remains valid until the document is saved. Disposing of it too early can cause the document to fail to render correctly or throw exceptions during the saving process.
Solution
To avoid this error, follow these best practices:
- Keep the font stream open until after the PDF is saved – Ensure the font stream remains valid throughout the entire document creation and saving process.
- Dispose of the font stream only after saving the document – Once the PDF document has been saved successfully, you can safely dispose of the font stream.
Code Example
The following code samples demonstrate how to manage the font stream so that it remains valid until after the PDF document is saved.
using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
// Open the font stream and keep it valid until after saving the PDF.
using (FileStream fontStream = new FileStream("path_to_custom_font.ttf", FileMode.Open, FileAccess.Read))
{
// Create a PDF document.
using (PdfDocument document = new PdfDocument())
{
// Define the font from the stream.
PdfFont font = new PdfTrueTypeFont(fontStream, 12);
// Create a page and draw the text.
PdfPage page = document.Pages.Add();
page.Graphics.DrawString("Hello, Syncfusion PDF!", font, PdfBrushes.Black, new Syncfusion.Drawing.PointF(100, 100));
// Save the document to a file.
using (FileStream outputStream = new FileStream("Output.pdf", FileMode.Create, FileAccess.Write))
{
document.Save(outputStream);
}
} // PdfDocument is disposed here.
} // Font stream is disposed here after saving.using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
// Open the font stream and keep it valid until after saving the PDF.
using (FileStream fontStream = new FileStream("path_to_custom_font.ttf", FileMode.Open, FileAccess.Read))
{
// Create a PDF document.
using (PdfDocument document = new PdfDocument())
{
// Define the font from the stream.
PdfFont font = new PdfTrueTypeFont(fontStream, 12);
// Create a page and draw the text.
PdfPage page = document.Pages.Add();
page.Graphics.DrawString("Hello, Syncfusion PDF!", font, PdfBrushes.Black, new System.Drawing.PointF(100, 100));
// Save the document to a file.
document.Save("Output.pdf");
} // PdfDocument is disposed here.
} // Font stream is disposed here after saving.Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Graphics
' Open the font stream and keep it valid until after saving the PDF.
Using fontStream As New FileStream("path_to_custom_font.ttf", FileMode.Open, FileAccess.Read)
' Create a PDF document.
Using document As New PdfDocument()
' Define the font from the stream.
Dim font As New PdfTrueTypeFont(fontStream, 12)
' Create a page and draw the text.
Dim page As PdfPage = document.Pages.Add()
page.Graphics.DrawString("Hello, Syncfusion PDF!", font, PdfBrushes.Black, New System.Drawing.PointF(100, 100))
' Save the document to a file.
document.Save("Output.pdf")
End Using ' PdfDocument is disposed here.
End Using ' Font stream is disposed here after saving.NOTE
In the C# [Cross-platform] tab,
Syncfusion.Drawing.PointFis used because .NET Core and .NET 5+ do not includeSystem.Drawingby default. For Windows-specific projects, you can useSystem.Drawing.PointFdirectly.