Text Processing with C# and .NET

The following example shows how to apply various transformations, such as rotation and scaling, to text drawn on a PDF page.

Complete code

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

class Program
{
    /// <summary>
    /// Text transformation and rotation.
    /// </summary>
    /// <remarks>
    /// Details: https://sautinsoft.com/products/pdf/help/net/developer-guide/text-transformations.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 the PdfFormattedText object.
                var text = "Rotated by 30 degrees around origin.";
                formattedText.Opacity = 0.2;
                formattedText.Append(text);
                var origin = new PdfPoint(50, 650);
                // Draw this text.
                page.Content.DrawText(formattedText, origin);
                // Clear the PdfFormattedText object.
                formattedText.Clear();
                // Set up and fill the PdfFormattedText object.
                formattedText.Opacity = 1;
                formattedText.Append(text);
                // Create a trasformation matrix.
                var transform = PdfMatrix.Identity;
                transform.Translate(origin.X, origin.Y);
                transform.Rotate(30);
                // Draw this text using a transformation matrix.
                page.Content.DrawText(formattedText, transform);
                // Clear the PdfFormattedText object.
                formattedText.Clear();
                // Set up and fill the PdfFormattedText object.
                text = "Rotated by 30 degrees around center.";
                formattedText.Opacity = 0.2;
                formattedText.Append(text);
                origin = new PdfPoint(300, 650);
                // Draw this text.
                page.Content.DrawText(formattedText, origin);
                // Clear the PdfFormattedText object.
                formattedText.Clear();
                // Set up and fill the PdfFormattedText object.
                formattedText.Opacity = 1;
                formattedText.Append(text);
                // Create a trasformation matrix.
                transform = PdfMatrix.Identity;
                transform.Translate(origin.X, origin.Y);
                transform.Rotate(30, formattedText.Width / 2, formattedText.Height / 2);
                // Draw this text using a transformation matrix.
                page.Content.DrawText(formattedText, transform);
                // Clear the PdfFormattedText object.
                formattedText.Clear();
                // Set up and fill the PdfFormattedText object.
                text = "Scaled horizontally by 0.5 around origin.";
                formattedText.Opacity = 0.2;
                formattedText.Append(text);
                origin = new PdfPoint(50, 500);
                // Draw this text.
                page.Content.DrawText(formattedText, origin);
                // Clear the PdfFormattedText object.
                formattedText.Clear();
                // Set up and fill the PdfFormattedText object.
                formattedText.Opacity = 1;
                formattedText.Append(text);
                // Create a trasformation matrix.
                transform = PdfMatrix.Identity;
                transform.Translate(origin.X, origin.Y);
                transform.Scale(0.5, 1);
                // Draw this text using a transformation matrix.
                page.Content.DrawText(formattedText, transform);
                // Clear the PdfFormattedText object.
                formattedText.Clear();
                // Set up and fill the PdfFormattedText object.
                text = "Scaled horizontally by 0.5 around center.";
                formattedText.Opacity = 0.2;
                formattedText.Append(text);
                origin = new PdfPoint(300, 500);
                // Draw this text.
                page.Content.DrawText(formattedText, origin);
                // Clear the PdfFormattedText object.
                formattedText.Clear();
                // Set up and fill the PdfFormattedText object.
                formattedText.Opacity = 1;
                formattedText.Append(text);
                // Create a trasformation matrix.
                transform = PdfMatrix.Identity;
                transform.Translate(origin.X, origin.Y);
                transform.Scale(0.5, 1, formattedText.Width / 2, formattedText.Height / 2);
                // Draw this text using a transformation matrix.
                page.Content.DrawText(formattedText, transform);
                // Clear the PdfFormattedText object.
                formattedText.Clear();
                // Set up and fill the PdfFormattedText object.
                text = "Scaled vertically by 2 around origin.";
                formattedText.Opacity = 0.2;
                formattedText.Append(text);
                origin = new PdfPoint(50, 400);
                // Draw this text.
                page.Content.DrawText(formattedText, origin);
                // Clear the PdfFormattedText object.
                formattedText.Clear();
                // Set up and fill the PdfFormattedText object.
                formattedText.Opacity = 1;
                formattedText.Append(text);
                // Create a trasformation matrix.
                transform = PdfMatrix.Identity;
                transform.Translate(origin.X, origin.Y);
                transform.Scale(1, 2);
                // Draw this text using a transformation matrix.
                page.Content.DrawText(formattedText, transform);
                // Clear the PdfFormattedText object.
                formattedText.Clear();
                // Set up and fill the PdfFormattedText object.
                text = "Scaled vertically by 2 around center.";
                formattedText.Opacity = 0.2;
                formattedText.Append(text);
                origin = new PdfPoint(300, 400);
                // Draw this text.
                page.Content.DrawText(formattedText, origin);
                // Clear the PdfFormattedText object.
                formattedText.Clear();
                // Set up and fill the PdfFormattedText object.
                formattedText.Opacity = 1;
                formattedText.Append(text);
                // Create a trasformation matrix.
                transform = PdfMatrix.Identity;
                transform.Translate(origin.X, origin.Y);
                transform.Scale(1, 2, formattedText.Width / 2, formattedText.Height / 2);
                // Draw this text using a transformation matrix.
                page.Content.DrawText(formattedText, transform);
                // Clear the PdfFormattedText object.
                formattedText.Clear();
                // Set up and fill the PdfFormattedText object.
                text = "Rotated by 30 degrees around origin and ";
                var text2 = "scaled horizontally by 0.5 and ";
                var text3 = "vertically by 2 around origin.";
                formattedText.Opacity = 0.2;
                formattedText.AppendLine(text).AppendLine(text2).Append(text3);
                origin = new PdfPoint(50, 200);
                // Draw this text.
                page.Content.DrawText(formattedText, origin);
                // Clear the PdfFormattedText object.
                formattedText.Clear();
                // Set up and fill the PdfFormattedText object.
                formattedText.Opacity = 1;
                formattedText.AppendLine(text).AppendLine(text2).Append(text3);
                // Create a trasformation matrix.
                transform = PdfMatrix.Identity;
                transform.Translate(origin.X, origin.Y);
                transform.Rotate(30);
                transform.Scale(0.5, 2);
                // Draw this text using a transformation matrix.
                page.Content.DrawText(formattedText, transform);
                // Clear the PdfFormattedText object.
                formattedText.Clear();
                // Set up and fill the PdfFormattedText object.
                text = "Rotated by 30 degrees around center and ";
                text2 = "scaled horizontally by 0.5 and ";
                text3 = "vertically by 2 around center.";
                formattedText.Opacity = 0.2;
                formattedText.AppendLine(text).AppendLine(text2).Append(text3);
                origin = new PdfPoint(300, 200);
                // Draw this text.
                page.Content.DrawText(formattedText, origin);
                // Clear the PdfFormattedText object.
                formattedText.Clear();
                // Set up and fill the PdfFormattedText object.
                formattedText.Opacity = 1;
                formattedText.AppendLine(text).AppendLine(text2).Append(text3);
                // Create a trasformation matrix.
                transform = PdfMatrix.Identity;
                transform.Translate(origin.X, origin.Y);
                transform.Rotate(30, formattedText.Width / 2, formattedText.Height / 2);
                transform.Scale(0.5, 2, formattedText.Width / 2, formattedText.Height / 2);
                // Draw this text using a transformation matrix.
                page.Content.DrawText(formattedText, transform);
            }
            // Save PDF Document.
            document.Save("Transformations.pdf");
        }
    }
}

Download

The above example shows how to convert text on a single page. To convert text on multiple pages, see the watermark example.

The previous examples show how to write text in Latin script. The following examples show how to write text in other scripts, including Arabic, Hebrew, various Indian scripts, and other complex scripts.


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.