How to Embed UTF-8 Characters in PDF Files Using C#

In this tutorial, we explore how to embed UTF-8 characters in PDF files using C# and IronPDF. Start by ensuring IronPDF is installed in your project through the NuGet Package Manager. Incorporate the IronPDF namespace in your program to access its functions and classes. Set your official IronPDF license key, retrievable from the IronPDF website if necessary. Define an HTML string with UTF-8 encoding, crucial for preserving multilingual text. Create an instance of the ChromePdfRenderer class, setting its input encoding property to System.Text.Encoding.UTF8. This ensures the renderer interprets the HTML string correctly. Use the RenderHtmlAsPdf method to convert the HTML string into a PDF document, then save it using the SaveAs method. Upon running the project, the resulting PDF file should accurately display all multilingual text, showcasing the capabilities of IronPDF and UTF-8 encoding. Subscribe for more Iron Software tutorials and explore trial subscriptions through the link provided.

Here is a detailed C# example:

using IronPdf; // Include the IronPDF Namespace

class Program
{
    static void Main(string[] args)
    {
        // Set your IronPDF license key if required
        // IronPdf.License.LicenseKey = "YOUR_LICENSE_KEY";

        // Define an HTML string with UTF-8 encoding specific content
        string htmlContent = "<html><body><p>こんにちは世界</p><p>Hello, World!</p></body></html>"; // Multilingual text

        // Create an instance of ChromePdfRenderer
        var renderer = new ChromePdfRenderer
        {
            // Set the input encoding to UTF-8 to handle international characters correctly
            RenderingOptions = { InputEncoding = System.Text.Encoding.UTF8 }
        };

        // Convert the HTML string to a PDF document
        var pdfDocument = renderer.RenderHtmlAsPdf(htmlContent);

        // Save the rendered PDF document to a file
        pdfDocument.SaveAs("DocumentWithUTF8.pdf");

        // Inform the user that the PDF has been generated
        Console.WriteLine("PDF successfully created with UTF-8 content.");
    }
}
using IronPdf; // Include the IronPDF Namespace

class Program
{
    static void Main(string[] args)
    {
        // Set your IronPDF license key if required
        // IronPdf.License.LicenseKey = "YOUR_LICENSE_KEY";

        // Define an HTML string with UTF-8 encoding specific content
        string htmlContent = "<html><body><p>こんにちは世界</p><p>Hello, World!</p></body></html>"; // Multilingual text

        // Create an instance of ChromePdfRenderer
        var renderer = new ChromePdfRenderer
        {
            // Set the input encoding to UTF-8 to handle international characters correctly
            RenderingOptions = { InputEncoding = System.Text.Encoding.UTF8 }
        };

        // Convert the HTML string to a PDF document
        var pdfDocument = renderer.RenderHtmlAsPdf(htmlContent);

        // Save the rendered PDF document to a file
        pdfDocument.SaveAs("DocumentWithUTF8.pdf");

        // Inform the user that the PDF has been generated
        Console.WriteLine("PDF successfully created with UTF-8 content.");
    }
}
Imports IronPdf ' Include the IronPDF Namespace

Friend Class Program
	Shared Sub Main(ByVal args() As String)
		' Set your IronPDF license key if required
		' IronPdf.License.LicenseKey = "YOUR_LICENSE_KEY";

		' Define an HTML string with UTF-8 encoding specific content
		Dim htmlContent As String = "<html><body><p>こんにちは世界</p><p>Hello, World!</p></body></html>" ' Multilingual text

		' Create an instance of ChromePdfRenderer
		Dim renderer = New ChromePdfRenderer With {
			.RenderingOptions = { InputEncoding = System.Text.Encoding.UTF8 }
		}

		' Convert the HTML string to a PDF document
		Dim pdfDocument = renderer.RenderHtmlAsPdf(htmlContent)

		' Save the rendered PDF document to a file
		pdfDocument.SaveAs("DocumentWithUTF8.pdf")

		' Inform the user that the PDF has been generated
		Console.WriteLine("PDF successfully created with UTF-8 content.")
	End Sub
End Class
$vbLabelText   $csharpLabel

Further Reading: How to Use UTF-8 Encoding and International Languages in PDFs

Chipego
Software Engineer
Chipego has a natural skill for listening that helps him to comprehend customer issues, and offer intelligent solutions. He joined the Iron Software team in 2023, after studying a Bachelor of Science in Information Technology. IronPDF and IronOCR are the two products Chipego has been focusing on, but his knowledge of all products is growing daily, as he finds new ways to support customers. He enjoys how collaborative life is at Iron Software, with team members from across the company bringing their varied experience to contribute to effective, innovative solutions. When Chipego is away from his desk, he can often be found enjoying a good book or playing football.
< PREVIOUS
How to Use Virtual Viewport and Zoom in C#
NEXT >
How to Add Stamps & Watermarks to PDFs in C#