Script Language Work in C# and .NET

In the modern world of programming and document development, managing script text is an important task. This control is used to process and manipulate text, which can include complex scenarios. This is especially true for documents containing multilingual text or text written in complex scripts such as Arabic, Hebrew or Indian languages.

The following example shows how to write text from complex scripts, such as Arabic, Hebrew, various Indic, and other.

  1. Add SautinSoft.PDF from NuGet.
  2. Create a new page.
  3. Fill the Pdf Formatted Text object with multilingual text.
  4. Draw text.
  5. Iterate through the remaining positions.
  6. Save the PDF Document.

Note that formatting Right-to-Left complex scripts, such as Arabic or Hebrew, currently works only via WPF (by setting the PdfFormattedText.TextFormattingMode property to PdfTextFormattingMode.WPF, which is set by default when running on Windows).

Formating Left-to-Right complex scripts, such as various Indic scripts, also works via HarfBuzz text-shaping engine (by setting the PdfFormattedText.TextFormattingMode property to PdfTextFormattingMode.HarfBuzz, which is set by default when running on non-Windows operating systems and the HarfBuzzSharp package is referenced by the project).

Output result:

Complete code

using System;
using SautinSoft.Pdf;
using System.IO;
using SautinSoft.Pdf.Content;

class Program
{
    /// <summary>
    /// How to set Font's settings and text formatting.
    /// </summary>
    /// <remarks>
    /// Details: https://sautinsoft.com/products/pdf/help/net/developer-guide/text-from-complex-scripts.php
    /// </remarks>
    static void Main()
    {
        // Before starting this example, please get a free 100-day trial key:
        // https://sautinsoft.com/start-for-free/

        // Apply the key here:
        // PdfDocument.SetLicense("...");

        using (var document = new PdfDocument())
        {
            // Create a new page.
            var page = document.Pages.Add();

            using (var formattedText = new PdfFormattedText())
            {
                // Set up and fill a PdfFormattedText object with multilingual text.
                formattedText.Language = new PdfLanguage("en-US");
                formattedText.Font = new PdfFont("Calibri", 12);

                formattedText.AppendLine("An example of a fully vocalised (vowelised or vowelled) Arabic ").
                Append("from the Basmala: ");

                formattedText.Language = new PdfLanguage("ar-SA");
                formattedText.Font = new PdfFont("Arial", 24);
                formattedText.Append("بِسْمِ ٱللَّٰهِ ٱلرَّحْمَٰنِ ٱلرَّحِيمِ");

                formattedText.Language = new PdfLanguage("en-US");
                formattedText.Font = new PdfFont("Calibri", 12);
                formattedText.AppendLine(", which means: ").
                Append("In the name of God, the All-Merciful, the Especially-Merciful.");

                // Draw this text.
                page.Content.DrawText(formattedText, new PdfPoint(50, 750));
                // Clear PdfFormattedText object.
                formattedText.Clear();
                // Set up and fill a PdfFormattedText object with multilingual text.
                formattedText.Append("An example of Hebrew: ");

                formattedText.Language = new PdfLanguage("he-IL");
                formattedText.Font = new PdfFont("Arial", 24);
                formattedText.Append("מה קורה");

                formattedText.Language = new PdfLanguage("en-US");
                formattedText.Font = new PdfFont("Calibri", 12);
                formattedText.AppendLine(", which means: What's going on, ").
                Append("and ");

                formattedText.Language = new PdfLanguage("he-IL");
                formattedText.Font = new PdfFont("Arial", 24);
                formattedText.Append("תודה לכולם");

                formattedText.Language = new PdfLanguage("en-US");
                formattedText.Font = new PdfFont("Calibri", 12);
                formattedText.Append(", which means: Thank you all.");
                // Draw this text.
                page.Content.DrawText(formattedText, new PdfPoint(50, 650));
                // Clear PdfFormattedText object.
                formattedText.Clear();
                // Set up and fill a PdfFormattedText object with multilingual text.
                formattedText.LineHeight = 50;

                formattedText.Append("An example of Thai: ");
                formattedText.Language = new PdfLanguage("th-TH");
                formattedText.Font = new PdfFont("Leelawadee UI", 16);
                formattedText.AppendLine("ภัำ");

                formattedText.Language = new PdfLanguage("en-US");
                formattedText.Font = new PdfFont("Calibri", 12);
                formattedText.Append("An example of Tamil: ");
                formattedText.Language = new PdfLanguage("ta-IN");
                formattedText.Font = new PdfFont("Nirmala UI", 16);
                formattedText.AppendLine("போது");

                formattedText.Language = new PdfLanguage("en-US");
                formattedText.Font = new PdfFont("Calibri", 12);
                formattedText.Append("An example of Bengali: ");
                formattedText.Language = new PdfLanguage("be-IN");
                formattedText.Font = new PdfFont("Nirmala UI", 16);
                formattedText.AppendLine("আবেদনকারীর মাতার পিতার বর্তমান স্থায়ী ঠিকানা নমিনি নাম");

                formattedText.Language = new PdfLanguage("en-US");
                formattedText.Font = new PdfFont("Calibri", 12);
                formattedText.Append("An example of Gujarati: ");
                formattedText.Language = new PdfLanguage("gu-IN");
                formattedText.Font = new PdfFont("Nirmala UI", 16);
                formattedText.AppendLine("કાર્બન કેમેસ્ટ્રી");

                formattedText.Language = new PdfLanguage("en-US");
                formattedText.Font = new PdfFont("Calibri", 12);
                formattedText.Append("An example of Osage: ");
                formattedText.Language = new PdfLanguage("osa");
                formattedText.Font = new PdfFont("Gadugi", 16);
                formattedText.Append("𐓏𐓘𐓻𐓘𐓻𐓟 𐒻𐓟");
                // Draw this text.
                page.Content.DrawText(formattedText, new PdfPoint(50, 350));
            }
            // Save PDF Document.
            document.Save("Complex scripts.pdf");
        }
    }
}

Download

Option Infer On

Imports System
Imports SautinSoft.Pdf
Imports System.IO
Imports SautinSoft.Pdf.Content

Friend Class Program
	''' <summary>
	''' How to set Font's settings and text formatting.
	''' </summary>
	''' <remarks>
	''' Details: https://sautinsoft.com/products/pdf/help/net/developer-guide/text-from-complex-scripts.php
	''' </remarks>
	Shared Sub Main()
		' Before starting this example, please get a free license:
		' https://sautinsoft.com/start-for-free/

		' Apply the key here:
		' PdfDocument.SetLicense("...");

		Using document = New PdfDocument()
			' Create a new page.
			Dim page = document.Pages.Add()

			Using formattedText = New PdfFormattedText()
				' Set up and fill a PdfFormattedText object with multilingual text.
				formattedText.Language = New PdfLanguage("en-US")
				formattedText.Font = New PdfFont("Calibri", 12)

				formattedText.AppendLine("An example of a fully vocalised (vowelised or vowelled) Arabic ").Append("from the Basmala: ")

				formattedText.Language = New PdfLanguage("ar-SA")
				formattedText.Font = New PdfFont("Arial", 24)
				formattedText.Append("بِسْمِ ٱللَّٰهِ ٱلرَّحْمَٰنِ ٱلرَّحِيمِ")

				formattedText.Language = New PdfLanguage("en-US")
				formattedText.Font = New PdfFont("Calibri", 12)
				formattedText.AppendLine(", which means: ").Append("In the name of God, the All-Merciful, the Especially-Merciful.")

				' Draw this text.
				page.Content.DrawText(formattedText, New PdfPoint(50, 750))
				' Clear PdfFormattedText object.
				formattedText.Clear()
				' Set up and fill a PdfFormattedText object with multilingual text.
				formattedText.Append("An example of Hebrew: ")

				formattedText.Language = New PdfLanguage("he-IL")
				formattedText.Font = New PdfFont("Arial", 24)
				formattedText.Append("מה קורה")

				formattedText.Language = New PdfLanguage("en-US")
				formattedText.Font = New PdfFont("Calibri", 12)
				formattedText.AppendLine(", which means: What's going on, ").Append("and ")

				formattedText.Language = New PdfLanguage("he-IL")
				formattedText.Font = New PdfFont("Arial", 24)
				formattedText.Append("תודה לכולם")

				formattedText.Language = New PdfLanguage("en-US")
				formattedText.Font = New PdfFont("Calibri", 12)
				formattedText.Append(", which means: Thank you all.")
				' Draw this text.
				page.Content.DrawText(formattedText, New PdfPoint(50, 650))
				' Clear PdfFormattedText object.
				formattedText.Clear()
				' Set up and fill a PdfFormattedText object with multilingual text.
				formattedText.LineHeight = 50

				formattedText.Append("An example of Thai: ")
				formattedText.Language = New PdfLanguage("th-TH")
				formattedText.Font = New PdfFont("Leelawadee UI", 16)
				formattedText.AppendLine("ภัำ")

				formattedText.Language = New PdfLanguage("en-US")
				formattedText.Font = New PdfFont("Calibri", 12)
				formattedText.Append("An example of Tamil: ")
				formattedText.Language = New PdfLanguage("ta-IN")
				formattedText.Font = New PdfFont("Nirmala UI", 16)
				formattedText.AppendLine("போது")

				formattedText.Language = New PdfLanguage("en-US")
				formattedText.Font = New PdfFont("Calibri", 12)
				formattedText.Append("An example of Bengali: ")
				formattedText.Language = New PdfLanguage("be-IN")
				formattedText.Font = New PdfFont("Nirmala UI", 16)
				formattedText.AppendLine("আবেদনকারীর মাতার পিতার বর্তমান স্থায়ী ঠিকানা নমিনি নাম")

				formattedText.Language = New PdfLanguage("en-US")
				formattedText.Font = New PdfFont("Calibri", 12)
				formattedText.Append("An example of Gujarati: ")
				formattedText.Language = New PdfLanguage("gu-IN")
				formattedText.Font = New PdfFont("Nirmala UI", 16)
				formattedText.AppendLine("કાર્બન કેમેસ્ટ્રી")

				formattedText.Language = New PdfLanguage("en-US")
				formattedText.Font = New PdfFont("Calibri", 12)
				formattedText.Append("An example of Osage: ")
				formattedText.Language = New PdfLanguage("osa")
				formattedText.Font = New PdfFont("Gadugi", 16)
				formattedText.Append("𐓏𐓘𐓻𐓘𐓻𐓟 𐒻𐓟")
				' Draw this text.
				page.Content.DrawText(formattedText, New PdfPoint(50, 350))
			End Using
			' Save PDF Document.
			document.Save("Complex scripts.pdf")
			System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo("Complex scripts.pdf") With {.UseShellExecute = True})
		End Using
	End Sub
End Class

Download


If you need a new code example or have a question: email us at support@sautinsoft.com or ask at Online Chat (right-bottom corner of this page) or use the Form below:



Questions and suggestions from you are always welcome!

We are developing .Net components since 2002. We know PDF, DOCX, RTF, HTML, XLSX and Images formats. If you need any assistance with creating, modifying or converting documents in various formats, we can help you. We will write any code example for you absolutely free.