How to create a multi-page PDF document efficiently without duplicating fonts in C#
The Syncfusion Essential® PDF is a comprehensive, high-performance .NET PDF library that enables you to create, read, and edit PDF documents. Using this library, you can avoid repeatedly creating fonts in a PDF document, which helps optimize performance.
Steps to avoid multiple font creation in a PDF document programmatically
- Create a new console application project.
- Install the Syncfusion.Pdf.Net.Core NuGet package as a reference in your console application from Nuget.org.
- Include the following namespaces in the Program.cs file.
C#
using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf.Interactive;
using Syncfusion.Pdf;
using Syncfusion.Drawing;
- Use the following code sample in Program.cs to avoid repeatedly creating fonts in a PDF document.
C#
// Create a new PDF document
PdfDocument document = new PdfDocument();
// Open the font stream once to use it for all pages instead of opening it repeatedly in each iteration
FileStream fontStream = new FileStream("Arial.ttf", FileMode.Open, FileAccess.Read);
// Load the font from the font stream with a size of 14
PdfFont font = new PdfTrueTypeFont(fontStream, 14);
// Add 10 pages to the PDF document
for (int i = 0; i < 10; i++)
{
// Add a new page to the document
PdfPage page = document.Pages.Add();
// Create PDF graphics for the page
PdfGraphics graphics = page.Graphics;
// Create a solid black brush to draw text on the page
PdfBrush brush = new PdfSolidBrush(Color.Black);
// Draw the text on the page at coordinates (20, 20), appending the iteration number (i) to each string
graphics.DrawString("Hello world!-" + i, font, brush, new PointF(20, 20));
}
// Create a file stream to save the PDF document to a file in the specified output path
using (FileStream stream = new FileStream("Output.pdf", FileMode.Create, FileAccess.Write))
{
// Save the PDF document to the file stream
document.Save(stream);
}
// Close the document and release resources
document.Close(true);
// Dispose of the font stream to free up resources
fontStream.Dispose();
A complete working sample is available for download from GitHub.
By executing the program, you will generate the following PDF document.
Take a moment to peruse the documentation to learn how to add different font in a PDF document.
Conclusion
I hope you enjoyed learning on how to avoid multiple font creation in PDF document.
You can refer to our ASP.NET Core PDF feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our ASP.NET Core PDF example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!