[]
        
(Showing Draft Content)

Text

GcPdf provides the following two main approaches to render text through GcGraphics class which is a member of GrapeCity.Documents.Drawing namespace:

  • Using TextLayout/DrawTextLayout method: The DrawTextLayout method is the main method for rendering text in GcPdf. It uses an instance of TextLayout class to draw the text layout at a specified location.

  • Using MeasureString/DrawString pair: The DrawString method can be used in pair with MeasureString method to render a short string on a page at an arbitrary location when the string can fit in the available space.

In addition, GcPdf offers GrapeCity.Documents.Text namespace which supports the following features to work with text:

Text alignment

  • GcPdf provides TextAlignment property to control how text is aligned horizontally along the reading direction axis. The property takes the values from the TextAlignment enum.

Text layout

  • GcPdf provides TextLayout 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

  • GcPdf offers TextFormat 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, GcPdf allows you to mix different text formats in the same paragraph using TextLayout and GcPdfGraphics.DrawTextLayout method to draw the text layout at a specified location.

Text rotation

  • GcPdf allows text rotation in a PDF document using Transform property of GcPdfGraphics class to rotate a text string.

Vertical text

  • GcPdf allows rendering vertical text in LeftToRight and RightToLeft modes. The library provides FlowDirection 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

  • GcPdf allows rendering text with stroked glyph outlines and filling glyphs with solid or gradient color in a PDF document using Hollow and FillBrush property of the TextFormat class.

Text trimming and wrapping

  • GcPdf offers TrimmingGranularity and WrapMode properties of the TextLayout 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

  • GcPdf allows to display superscript and subscript texts through TextFormat class. This class provides Subscript property to set text as subscript (for example,"2" in H2O) and Superscript property to set text as superscript (for example, "3" in x3).

Paragraph alignment and formatting

  • GcPdf provides all the properties to align and format paragraph in TextLayout class. This class provides ParagraphAlignment property to set the alignment of paragraphs along the flow direction axis. The ParagraphAlignment property takes the values from ParagraphAlignment enum. In addition, basic paragraph formatting options such as line indentation and spacing can also be applied to the paragraphs using FirstLineIndent and LineSpacingScaleFactor properties of the TextLayout class.

    Text Features

Render Text

To render text in a PDF document using GcPdf, you can either use DrawString method provided by the GcGraphics class or the TextLayout 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 method. Finally, render the text in the document by calling the DrawTextLayoutDrawTextLayout method provided by the GcGraphics class.

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 GcPdf 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 GcPdf, see GcPdf sample browser.

Align Text

To set the text alignment in a PDF document, use TextAlignment property provided by the TextLayout class. This property accepts value from TextAlignment enum.

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.

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 property provided by the GcGraphics class, which accepts the value calculated by the CreateRotation method of Matrix3x2 class.

  2. Draw the rotated text and bounding rectangle using DrawTextLayout method.

    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

GcPdf supports vertical text through FlowDirection property of the GcGraphics class which accepts value from the FlowDirection enumeration. To set the vertical text alignment, this property needs to be set to VerticalLeftToRight or VerticalRightToLeft.

Additionally, the TextFormat class of GcPdf provides you an option to rotate the sideways text in counter clockwise direction using the RotateSidewaysCounterclockwise property. Further, SidewaysInVerticalText and UprightInVerticalText 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.

// 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 property, and then set the Hollow property to true. And, in case of fill text, use the FillBrush property provided by the TextFormat class.

// 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 property provided by the TextLayout class. This class also provides the TrimmingGranularity and EllipsisCharCode properties to set the trimming options and to display a particular character at the end of the text respectively.

// 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 and Superscript properties provided by the TextFormat class.

//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 class to set the paragraph alignment and formatting.

public void CreatePDF(Stream stream)
{
    var doc = new GcPdfDocument();
    var page = doc.NewPage();
    var g = page.Graphics;
    // By default, GcPdf 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 GcPdf, see GcPdf sample browser.