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:

Полный код

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


Если вам нужен пример кода или у вас есть вопрос: напишите нам по адресу support@sautinsoft.ru или спросите в онлайн-чате (правый нижний угол этой страницы) или используйте форму ниже:



Вопросы и предложения всегда приветствуются!

Мы разрабатываем компоненты .Net с 2002 года. Мы знаем форматы PDF, DOCX, RTF, HTML, XLSX и Images. Если вам нужна помощь в создании, изменении или преобразовании документов в различных форматах, мы можем вам помочь. Мы напишем для вас любой пример кода абсолютно бесплатно.