Articles in this section
Category / Section

Enable RTL language support in HTML-to-PDF Conversion in .NET Core

2 mins read

The Syncfusion® HTML-to-PDF converter is a robust .NET library designed to convert web pages, SVG, MHTML, and HTML files to PDFs while preserving the original layout. This guide demonstrates how to enhance your PDF documents by adding right-to-left (RTL) language support, including Arabic, within the Table of Contents (TOC).

Steps to add RTL language support for TOC in HTML to PDF conversion:

  1. Create a Console Application: Set up a new console application project.
    Screenshot (1337).png
  2. Install Syncfusion® Package: Add the Syncfusion.HtmlToPdfConverter.Net.Windows NuGet package to your console application.
    HTMLtoPDFWindows.png
  3. Include Required Namespaces: Add the following namespaces in Program.cs.

C#

using Syncfusion.Drawing;
using Syncfusion.HtmlConverter;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf.HtmlToPdf;
  1. Implementation of Multi-Language Support: Use the following code to integrate RTL language support into your TOC during HTML-to-PDF conversion:

C#

// Initialize the HTML to PDF converter
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();
BlinkConverterSettings settings = new BlinkConverterSettings();

// Enable TOC and define styles
settings.EnableToc = true;
HtmlToPdfTocStyle tocstyleH1 = new HtmlToPdfTocStyle();

// Load Arabic font
PdfFont arabicFont = new PdfTrueTypeFont(@"Data/arial-unicode-ms.ttf", 10, PdfFontStyle.Regular);
tocstyleH1.Font = arabicFont;
tocstyleH1.BackgroundColor = new PdfSolidBrush(new PdfColor(Color.FromArgb(68, 114, 196)));
tocstyleH1.ForeColor = PdfBrushes.White;
tocstyleH1.Padding = new PdfPaddings(5, 5, 3, 3);

// Apply TOC styles
for (int i = 1; i <= 4; i++)
{
   settings.Toc.SetItemStyle(i, tocstyleH1);
}

// Assign settings and perform conversion
htmlConverter.ConverterSettings = settings;
PdfDocument document = htmlConverter.Convert(Path.GetFullPath(@"Data/input1.html"));

// Save and close the PDF document
using (FileStream fileStream = new FileStream(Path.GetFullPath(@"Output/HTML-to-PDF.pdf"), FileMode.Create, FileAccess.ReadWrite))
{
   document.Save(fileStream);
}
document.Close(true);

A complete working sample can be downloaded from HTML-to-PDF-TOC-Arabic

By executing the program, the output PDF document will be generated as shown below.

Output.png

Take a moment to peruse the documentation, where you can find converting HTML pages to PDF document with custom style of TOC.

You can use fonts that support other languages beyond Arabic for the Table of Contents (TOC) in your HTML to PDF conversion. Please ensure that the chosen font fully supports the required language characters to avoid issues in rendering.

Conclusion
I hope you enjoyed learning about how to add RTL languages when converting HTML to PDF 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!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied