Integrate Custom Fonts in HTML-to-PDF Conversion in .NET Core
Explore the capabilities of the Syncfusion® HTML-to-PDF converter, a robust .NET library, to seamlessly convert web pages, SVGs, and HTML files to PDFs with precision. One of its standout features is the ability to incorporate custom fonts into your PDF’s header and footer, enhancing the document’s aesthetic and readability.
Steps to add custom fonts in PDF footer during HTML to PDF conversion
- Create a Console Application: Begin by setting up a new console application project
- Add Syncfusion® NuGet Package: Install the Syncfusion.HtmlToPdfConverter.Winforms package via Nuget.org.
- Include Required Namespaces: Add the following namespaces in
Program.cs
.
C#
using System.Drawing;
using Syncfusion.HtmlConverter;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
- Implementation of Custom Fonts: Use the following C# code to embed a custom font in the PDF footer during the HTML-to-PDF conversion.
C#
// Initialize the HTML to PDF converter
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();
BlinkConverterSettings blinkConverterSettings = new BlinkConverterSettings();
// Load the custom font from a file
FileStream fontStream = new FileStream(@"calibri.ttf", FileMode.Open, FileAccess.Read);
PdfFont font = new PdfTrueTypeFont(fontStream, 10);
// Create brush for the footer element
PdfBrush brush = new PdfSolidBrush(Color.Black);
// Create a footer with specified bounds
PdfPageTemplateElement footer = new PdfPageTemplateElement(new RectangleF(0, 0, blinkConverterSettings.PdfPageSize.Width, 50));
// Create a page number field
PdfPageNumberField pageNumber = new PdfPageNumberField(font, PdfBrushes.Black);
// Create a page count field
PdfPageCountField count = new PdfPageCountField(font, PdfBrushes.Black);
// Format fields and draw in the footer
PdfCompositeField compositeField = new PdfCompositeField(font, PdfBrushes.Black, "Page {0} of {1}", pageNumber, count);
compositeField.Draw(footer.Graphics, PointF.Empty);
// Assign the footer to PDF settings
blinkConverterSettings.PdfFooter = footer;
blinkConverterSettings.ViewPortSize = new Size(1024, 0);
htmlConverter.ConverterSettings = blinkConverterSettings;
// Convert URL to PDF
PdfDocument document = htmlConverter.Convert("https://www.google.com/");
// Save the PDF document
using (FileStream fileStream = new FileStream("Output.pdf", FileMode.Create, FileAccess.ReadWrite))
{
document.Save(fileStream);
}
document.Close(true);
A complete working sample can be downloaded from HTML-to-PDF-Framework-Footer-Custom-Font
By executing the program, a PDF file will be generated with the footer text rendered in the Calibri font, as shown in the output below.
Take a moment to peruse the documentation, where you can find converting HTML to PDF document with Header and Footer.
Conclusion
I hope you enjoyed learning about how to add custom fonts in PDF footer during HTML to PDF conversion.
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!