Test in production without watermarks.
Works wherever you need it to.
Get 30 days of fully functional product.
Have it up and running in minutes.
Full access to our support engineering team during your product trial
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
Further Reading: How to Use UTF-8 Encoding and International Languages in PDFs