Articles in this section
Category / Section

Embed Unicode Text in PDF TextBox Fields using .NET Core

3 mins read

The Syncfusion Essential® PDF is a .NET PDF library used to create, read, and edit PDF documents. This guide will walk you through adding Unicode text to text box fields within a PDF document using C#.

Steps to add Unicode text in PDF text box fields programmatically:

  1. Create a Console Application: Set up a new console application project.
    ConsoleApp.png
  2. Install Syncfusion® Package: Add the Syncfusion.Pdf.Net.Core NuGet package to your console application.
    NuGetPackage.png
  3. Include Required Namespaces: Add the following namespaces in Program.cs.

C#

using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf.Parsing;
  1. Implement Unicode text in text box fields: Use the following code to integrate Unicode text within your PDF text box fields.

C#

// Open the existing PDF document using a FileStream
using (FileStream docStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read))
{
   // Load the existing PDF document
   PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream);

   // Get the form from the loaded document
   PdfLoadedForm loadedForm = loadedDocument.Form;

   // Load a Unicode font (e.g., Arial Unicode) from a file stream
   using (FileStream fontStream = new FileStream("arial.ttf", FileMode.Open, FileAccess.Read))
   {
       // Create a Unicode TrueType font to render special characters
       PdfTrueTypeFont unicodeFont = new PdfTrueTypeFont(fontStream, 10, true);

       // Loop through each form field to update text and font
       for (int i = 0; i < loadedForm.Fields.Count; i++)
       {
           // Try to cast the field to a TextBox field
           if (loadedForm.Fields[i] is PdfLoadedTextBoxField field)
           {
               // Set Unicode text (including umlaut characters)
               field.Text = "ä, ü, ï, ö, ë, ÿ";

               // Apply the loaded Unicode font to the text field
               field.Font = unicodeFont;
           }
       }

       // Remove the default appearance settings (to ensure font styling is applied)
       loadedDocument.Form.SetDefaultAppearance(false);

       // Save the modified PDF document using a FileStream
       using (FileStream outputStream = new FileStream("Output.pdf", FileMode.Create, FileAccess.Write))
       {
           // Save to output file
           loadedDocument.Save(outputStream); 
       }

       // Close the document to release resources
       loadedDocument.Close(true);
   }
}

A complete working sample can be downloaded from Draw_unicode_text.zip

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

Output.png

Take a moment to explore the documentation on working with Text, where you can find additional options such as drawing right-to-left text, multi-column text, and using TrueType fonts, standard fonts, and CJK fonts.

Conclusion
I hope you enjoyed learning about how to add Unicode text in PDF text box fields using C#.

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