# Text

With DsPdf, you can render text in a PDF document, align and format the text, trim or wrap the text, render superscript or subscript text, and more.

## Content

DsPdf provides the following two main approaches to render text through [GcGraphics](/document-solutions/dot-net-pdf-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Drawing.GcGraphics.html) class which is a member of [GrapeCity.Documents.Drawing](/document-solutions/dot-net-pdf-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Drawing.html) namespace:

* **Using TextLayout/DrawTextLayout method**: The [DrawTextLayout](/document-solutions/dot-net-pdf-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Drawing.GcGraphics.DrawTextLayout.html) method is the main method for rendering text in DsPdf. It uses an instance of [TextLayout](/document-solutions/dot-net-pdf-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Text.TextLayout.html) class to draw the text layout at a specified location.
* **Using MeasureString/DrawString pair**: The [DrawString](/document-solutions/dot-net-pdf-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Drawing.GcGraphics.DrawString.html) method can be used in pair with [MeasureString](/document-solutions/dot-net-pdf-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Drawing.GcGraphics.MeasureString.html) method to render a short string on a page at an arbitrary location when the string can fit in the available space.

In addition, DsPdf offers [GrapeCity.Documents.Text](/document-solutions/dot-net-pdf-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Text.html) namespace which supports the following features to work with text:
**Text alignment**
DsPdf provides [TextAlignment](/document-solutions/dot-net-pdf-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Text.TextLayout.TextAlignment.html) property to control how text is aligned horizontally along the reading direction axis. The property takes the values from the [TextAlignment](/document-solutions/dot-net-pdf-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Text.TextAlignment.html) enum.

**Text layout**
DsPdf provides [TextLayout](/document-solutions/dot-net-pdf-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Text.TextLayout.html) class that represents one or more paragraphs of text with same formatting. It can also be directly used for text shaping and layout.

**Text formatting**
DsPdf offers [TextFormat](/document-solutions/dot-net-pdf-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Text.TextFormat.html) class to format text and set font color and decorations. Font is the mandatory property that must be set on a text format. In addition, DsPdf allows you to mix different text formats in the same paragraph using **TextLayout** and DsPdf[Graphics.DrawTextLayout](/document-solutions/dot-net-pdf-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Drawing.GcGraphics.DrawTextLayout.html) method to draw the text layout at a specified location.

**Text rotation**
DsPdf allows text rotation in a PDF document using [Transform](/document-solutions/dot-net-pdf-api/api/online/DS.Documents.Pdf/GrapeCity.Documents.Pdf.GcPdfGraphics.Transform.html) property of [GcPdfGraphics](/document-solutions/dot-net-pdf-api/api/online/DS.Documents.Pdf/GrapeCity.Documents.Pdf.GcPdfGraphics.html) class to rotate a text string.

**Vertical text**
DsPdf allows rendering vertical text in **LeftToRight** and **RightToLeft** modes. The library provides [FlowDirection](/document-solutions/dot-net-pdf-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Text.TextLayout.FlowDirection.html) property of **TextLayout** class which allows setting the direction of the text. Moreover, it allows you to render vertical text for many common East Asian languages, such as Chinese, Japanese and Korean.

**Text stroking and filling**
DsPdf allows rendering text with stroked glyph outlines and filling glyphs with solid or gradient color in a PDF document using [Hollow](/document-solutions/dot-net-pdf-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Text.TextFormat.Hollow.html) and [FillBrush](/document-solutions/dot-net-pdf-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Text.TextFormat.FillBrush.html) property of the TextFormat class.

**Text trimming and wrapping**
DsPdf offers [TrimmingGranularity](/document-solutions/dot-net-pdf-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Text.TextLayout.TrimmingGranularity.html) and [WrapMode](/document-solutions/dot-net-pdf-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Text.TextLayout.WrapMode.html) properties of the [TextLayout](/document-solutions/dot-net-pdf-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Text.TextLayout.html) class that allows trimming the text at word or character, and wrap text in a PDF document. This allows you to display ellipsis or any other character at the end of a text line that does not fit in the allocated space.

**Subscript and superscript**
DsPdf allows to display superscript and subscript texts through TextFormat class. This class provides [Subscript](/document-solutions/dot-net-pdf-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Text.TextFormat.Subscript.html) property to set text as subscript (for example, "2" in H2O) and [Superscript](/document-solutions/dot-net-pdf-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Text.TextFormat.Superscript.html) property to set text as superscript (for example, "3" in x3).

**Paragraph alignment and formatting**
DsPdf provides all the properties to align and format paragraph in **TextLayout** class. This class provides [ParagraphAlignment](/document-solutions/dot-net-pdf-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Text.TextLayout.ParagraphAlignment.html) property to set the alignment of paragraphs along the flow direction axis. The **ParagraphAlignment** property takes the values from [ParagraphAlignment](/document-solutions/dot-net-pdf-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Text.ParagraphAlignment.html) enum. In addition, basic paragraph formatting options such as line indentation and spacing can also be applied to the paragraphs using [FirstLineIndent](/document-solutions/dot-net-pdf-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Text.TextLayout.FirstLineIndent.html) and [LineSpacingScaleFactor](/document-solutions/dot-net-pdf-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Text.TextLayout.LineSpacingScaleFactor.html) properties of the **TextLayout** class.
![Text Features](https://cdn.mescius.io/document-site-files/images/884d2bfb-301b-49bc-8d90-656c5b910507/images/text.png)

## Render Text

To render text in a PDF document using DsPdf, you can either use [DrawString](/document-solutions/dot-net-pdf-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Drawing.GcGraphics.DrawString.html) method provided by the [GcGraphics](/document-solutions/dot-net-pdf-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Drawing.GcGraphics.html) class or the [TextLayout](/document-solutions/dot-net-pdf-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Text.TextLayout.html) class. In case you are using the TextLayout class, you need to create a layout using Append method, and then prepare it for rendering by calling the [PerformLayout](/document-solutions/dot-net-pdf-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Text.TextLayout.PerformLayout.html) method. Finally, render the text in the document by calling the [DrawTextLayout](/document-solutions/dot-net-pdf-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Drawing.GcGraphics.DrawTextLayout.html) method provided by the GcGraphics class.

```csharp
public void CreatePDF(Stream stream)
{
    var doc = new GcPdfDocument();
    var page = doc.NewPage();
    var g = page.Graphics;
    const float In = 150;
    PointF ip = new PointF(72, 72);
    var tl = g.CreateTextLayout();
    tl.DefaultFormat.Font = StandardFonts.Times;
    tl.DefaultFormat.FontSize = 12;
    // TextFormat class is used throughout all DsPdf text rendering to specify
    // font and other character formatting:
    var tf = new TextFormat(tl.DefaultFormat)
    {
        Font = StandardFonts.Times,
        FontSize = 12
    };

    // Render text using Append method
    tl.Append("Turpis non ante pulvinar et massa nibh laoreet amet volutpat laoreet " +
     "molestie aliquet massa ullamcorper ac nisi ante massa lobortis Massa at laoreet" +
     "mauris aliquamfelis feugiat et non euismod magna eget molestie euismod elit dolor " +
     "eget erat euismod laoreetPharetra sit mauris nibh molestie ac nunc proin felis" +
     " erat lorem volutpat elit mi nunc magnamauris molestie tincidunt" +
     " sedMassa congue nibh volutpat eget non", tf);
    tl.PerformLayout(true);
    g.DrawTextLayout(tl, ip);

    // Render text using DrawString method
    g.DrawString("1. Test string.", tf, new PointF(In, In));

    // Save document
    doc.Save(stream);
}
```

For more information about rendering text using DsPdf, see [DsPdf sample browser](https://developer.mescius.com/documents-api-pdf/demos/basics/text/text-rendering/code-cs).

## Align Text

To set the text alignment in a PDF document, use [TextAlignment](/document-solutions/dot-net-pdf-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Text.TextLayout.TextAlignment.html) property provided by the [TextLayout](/document-solutions/dot-net-pdf-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Text.TextLayout.html) class. This property accepts value from [TextAlignment](/document-solutions/dot-net-pdf-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Text.TextAlignment.html) enum.

```csharp
tl.TextAlignment = TextAlignment.Trailing;
```

## Format Text

To format text in a PDF document, use the TextFormat class. This class is used for any type of text rendering, to specify the font and other character formatting. You can use different properties, such as FontSize, FontStyle, etc. provided by the **TextFormat** class to apply required text format to the rendered text.

```csharp
TextFormat tf = new TextFormat() 
{
  Font = StandardFonts.Courier,
  FontSize = 14,
  FontStyle = FontStyle.Bold,
  ForeColor = GrapeCity.Documents.Drawing.Color.Cyan,
  Language = Language.English
};
```

## Rotate Text

To rotate text at various angles in a PDF document:

1. Rotate the text using [Transform](/document-solutions/dot-net-pdf-api/api/online/DS.Documents.Pdf/GrapeCity.Documents.Pdf.GcPdfGraphics.Transform.html) property provided by the [GcGraphics](/document-solutions/dot-net-pdf-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Drawing.GcGraphics.html) class, which accepts the value calculated by the [CreateRotation](https://docs.microsoft.com/en-us/dotnet/api/system.numerics.matrix3x2.createrotation?view=netframework-4.8) method of [Matrix3x2](https://docs.microsoft.com/en-us/dotnet/api/system.numerics.matrix3x2?view=netframework-4.8) class.
2. Draw the rotated text and bounding rectangle using [DrawTextLayout](/document-solutions/dot-net-pdf-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Drawing.GcGraphics.DrawTextLayout.html) method.

    ```csharp
    public void CreatePDF(Stream stream)
    {
        // Rotation angle, degrees clockwise
        float angle = -45;
        var doc = new GcPdfDocument();
        var g = doc.NewPage().Graphics;
        // Create a text layout, pick a font and font size:
        TextLayout tl = g.CreateTextLayout();
        tl.DefaultFormat.Font = StandardFonts.Times;
        tl.DefaultFormat.FontSize = 24;
        // Add a text, and perform layout:
        tl.Append("Rotated text.");
        tl.PerformLayout(true);
        // Text insertion point at (1",1"):
        var ip = new PointF(72, 72);
        // Now that we have text size, create text rectangle
        var rect = new RectangleF(ip.X, ip.Y, tl.ContentWidth, tl.ContentHeight);
        // Rotate the text around its bounding rect's center:
        // we now have the text size, and can rotate it about its center:
        g.Transform = Matrix3x2.CreateRotation((float)(angle * Math.PI) / 180f,
        new Vector2(ip.X + tl.ContentWidth / 2, ip.Y + tl.ContentHeight / 2));
        // Draw rotated text and bounding rectangle:
        g.DrawTextLayout(tl, ip);
        g.DrawRectangle(rect, Color.Black, 1);
        // Remove rotation and draw the bounding rectangle
        g.Transform = Matrix3x2.Identity;
        g.DrawRectangle(rect, Color.ForestGreen, 1);
        // Save Document
        doc.Save(stream);
    }
    ```

## Vertical Text

DsPdf supports vertical text through [FlowDirection](/document-solutions/dot-net-pdf-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Text.TextLayout.FlowDirection.html) property of the GcGraphics class which accepts value from the [FlowDirection](/document-solutions/dot-net-pdf-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Text.FlowDirection.html) enumeration. To set the vertical text alignment, this property needs to be set to VerticalLeftToRight or VerticalRightToLeft.
Additionally, the TextFormat class of DsPdf provides you an option to rotate the sideways text in counter clockwise direction using the [RotateSidewaysCounterclockwise](/document-solutions/dot-net-pdf-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Text.TextLayout.RotateSidewaysCounterclockwise.html) property. Further, [SidewaysInVerticalText](/document-solutions/dot-net-pdf-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Text.TextFormat.SidewaysInVerticalText.html) and [UprightInVerticalText](/document-solutions/dot-net-pdf-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Text.TextFormat.UprightInVerticalText.html) property of the TextFormat class also provides options to display the text sideways or upright respectively. These properties are especially useful for rendering Latin text within the East-Asian language text.

```csharp
// Set vertical text layout using TextLayout properties
tl.RotateSidewaysCounterclockwise = true;
tl.FlowDirection = FlowDirection.VerticalLeftToRight;
        
// Setup the vertical text layout for Chinese, Japanese and Korean characters       
TextFormat tfvertical = new TextFormat()
{
 UprightInVerticalText = false,
 GlyphWidths = GlyphWidths.Default,
 TextRunAsCluster = false,
};
tl.Append("日本語でのテスト文字列です", tfvertical);
```

## Outline Text and Fill Text

To render an outline text, draw the outline using the [StrokePen](/document-solutions/dot-net-pdf-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Text.TextFormat.StrokePen.html) property, and then set the [Hollow](/document-solutions/dot-net-pdf-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Text.TextFormat.Hollow.html) property to **true**. And, in case of fill text, use the [FillBrush](/document-solutions/dot-net-pdf-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Text.TextFormat.FillBrush.html) property provided by the TextFormat class.

```csharp
// Outline Text
TextFormat tf0 = new TextFormat() {
        StrokePen = Color.DarkGreen,
        Hollow = true,
        FontSize = 48,
};
tl.AppendLine("Outline Text", tf0);
        
// Filled Text  
TextFormat tf1 = new TextFormat() {
        StrokePen = Color.DarkMagenta,
        FillBrush = new SolidBrush(Color.Yellow),
        FontSize = 48,
};
tl.AppendLine("Filled Text", tf1);
```

## Text Trimming and Wrapping

There are two ways of handling the text that does not fit into the available space; one is to wrap the text and other is to trim a character or a word and append it with a character such as ellipsis. To wrap the text in a PDFdocument, use the [WrapMode](/document-solutions/dot-net-pdf-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Text.TextLayout.WrapMode.html) property provided by the [TextLayout](/document-solutions/dot-net-pdf-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Text.TextLayout.html) class. This class also provides the [TrimmingGranularity](/document-solutions/dot-net-pdf-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Text.TextLayout.TrimmingGranularity.html) and [EllipsisCharCode](/document-solutions/dot-net-pdf-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Text.TextLayout.EllipsisCharCode.html) properties to set the trimming options and to display a particular character at the end of the text respectively.

```csharp
// Character trimming
tl.TrimmingGranularity = TrimmingGranularity.Character;

tl.EllipsisCharCode = 0x007E;

// Set wrap mode to character wrap
tl.WrapMode = WrapMode.CharWrap;
```

## Subscript and Superscript

To render subscript and superscript text in a PDF document, use the [Subscript](/document-solutions/dot-net-pdf-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Text.TextFormat.Subscript.html) and [Superscript](/document-solutions/dot-net-pdf-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Text.TextFormat.Superscript.html) properties provided by the TextFormat class.

```csharp
//Apply Subscript
var tf = new TextFormat() {FontSize = 18};
var tfsub = new TextFormat() {Subscript = true, FontBold = true};
var tfbold = new TextFormat() {FontBold = true, FontSize = 18};
tl.Append("The chemical formula of water is ");
tl.Append("H", tfbold);
tl.Append("2", tfsub);
tl.Append("O.", tfbold);


//Apply Superscript
var tf = new TextFormat() {FontSize = 18};
var tfsup = new TextFormat() {Superscript = true, FontBold = true};
tl.Append("Example of a math equation : ");
tl.Append("x", tf);
tl.Append("2", tfsup);
tl.Append("+", tf);
tl.Append("y", tf);
tl.Append("2", tfsup);
```

## Handle Paragraph

To handle paragraph formatting, use the properties provided by [TextLayout](/document-solutions/dot-net-pdf-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Text.TextLayout.html) class to set the paragraph alignment and formatting.

```csharp
public void CreatePDF(Stream stream)
{
    var doc = new GcPdfDocument();
    var page = doc.NewPage();
    var g = page.Graphics;
    // By default, DsPdf uses 72dpi:
    PointF ip = new PointF(72, 72);
    var tl = g.CreateTextLayout();

    tl.MaxWidth = doc.PageSize.Width;
    tl.MaxHeight = doc.PageSize.Height;
    tl.MarginLeft = tl.MarginTop = tl.MarginRight = tl.MarginBottom = 72;
    var tf = new TextFormat(tl.DefaultFormat)
    {
        Font = StandardFonts.Times,
        FontSize = 12,
    };

    // Render text using Append method
    tl.Append("Turpis non ante pulvinar et massa nibh laoreet amet volutpat laoreet " +
     "molestie aliquet massa ullamcorper ac nisi ante massa lobortis Massa at laoreet" +
     "mauris aliquamfelis feugiat et non euismod magna eget molestie euismod elit dolor" +
     "eget erat euismod laoreetPharetra sit mauris nibh molestie ac nunc proin felis" +
     "erat lorem volutpat elit mi nunc magnamauris molestie tincidunt" +
     "sedMassa congue nibh volutpat eget non", tf);

    // Set first line offset
    tl.FirstLineIndent = 72 / 2;
    // Set line spacing
    tl.LineSpacingScaleFactor = 1.5f;
    tl.PerformLayout(true);
    g.DrawTextLayout(tl, ip);
    // Save document
    doc.Save(stream);
}
```

For more information about extracting table data from PDF documents by using DsPdf, see [DsPdf sample browser](https://developer.mescius.com/documents-api-pdf/demos/features/extract-tables).