Creating a Table of Contents with Unicode fonts when converting HTML to PDF
The Syncfusion® HTML-to-PDF converter is a .NET PDF library designed for converting webpages, SVG, MHTML, and HTML files to PDF using C#. It leverages the Blink rendering engine (used by Google Chrome) to ensure high-fidelity rendering. The converter delivers reliable and accurate results, preserving all graphics, images, text, fonts, and the original layout of the HTML document or webpage.
This guide shows how to set TOC styles, use Unicode fonts for multilingual content, and ensure proper rendering with the Blink engine.
Steps for creating a Table of Contents (TOC) with Unicode fonts during HTML to PDF conversion:
- Create a New Project: Start a new Console application in .NET Core to facilitate the HTML-to-PDF conversion process.
- Install Required Packages: Add the Syncfusion.HtmlToPdfConverter.Net.Windows NuGet package from Nuget.org to your project.
- Set Up Your Environment: In the
Program.cs
file, include these namespaces.
C#
using Syncfusion.Drawing;
using Syncfusion.HtmlConverter;
using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf.HtmlToPdf;
using Syncfusion.Pdf;
- Create a TOC with Unicode Fonts: Use the following code sample to apply Unicode font styles to the Table of Contents during HTML-to-PDF conversion.
C#
// Initialize the HTML to PDF converter with Blink rendering engine
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter(HtmlRenderingEngine.Blink);
// Create Blink converter settings
BlinkConverterSettings settings = new BlinkConverterSettings
{
EnableToc = true
};
using (FileStream fontStream = new FileStream(Path.GetFullPath(@"ARIALUNI.TTF"), FileMode.Open, FileAccess.Read))
{
// Load a Unicode TrueType font from system or custom path
PdfTrueTypeFont unicodeFont = new PdfTrueTypeFont(fontStream, 14);
// Set the style for level 1 (H1) items in table of contents
HtmlToPdfTocStyle tocStyleH1 = new HtmlToPdfTocStyle
{
Font = unicodeFont,
BackgroundColor = new PdfSolidBrush(new PdfColor(Color.FromArgb(68, 114, 196))),
ForeColor = PdfBrushes.White,
Padding = new PdfPaddings(5, 5, 3, 3)
};
// Apply the style to TOC level 1
settings.Toc.SetItemStyle(1, tocStyleH1);
// Assign Blink converter settings to HTML converter
htmlConverter.ConverterSettings = settings;
// Convert HTML to PDF
PdfDocument document = htmlConverter.Convert(Path.GetFullPath(@"Input.html"));
// Save and close the PDF document
using (FileStream fileStream = new FileStream(Path.GetFullPath(@"Output.pdf"), FileMode.Create, FileAccess.ReadWrite))
{
document.Save(fileStream);
}
//Close the PDF document
document.Close(true);
}
A complete working sample can be downloaded from GitHub.
By executing the program, you will generate the following PDF document.
Conclusion
I hope you enjoyed learning how to create a Table of Contents (TOC) with Unicode fonts during HTML to PDF conversion using C# in .NET Core.
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!