Articles in this section
Category / Section

How to add unicode text in the PDF text box fields using .NET?

3 mins read

The Syncfusion Essential® PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can draw Unicode text in text box fields in a PDF document using C#.

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

  1. Create a new console application project.ConsoleApp.png
  2. Install the Syncfusion.Pdf.Net.Core NuGet package as a reference to your console application from Nuget.org.
    NuGetPackage.png
  3. Include the following namespaces in the Program.cs file.

C#

using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf.Parsing;
  1. Use the following code sample in Program.cs to add Unicode text in the 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, you will get the PDF document as follows.Output.png

Take a moment to peruse the documentation for working with Text, where you can find the other options like drawing right-to-left text, multicolumn text, consuming TrueType fonts, Standards fonts, and CJK fonts.

Conclusion
I hope you enjoyed learning on how to add Unicode text in the PDF text box fields in .NET PDF library.

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