Documents for PDF .NET Edition
Features / Text
In This Topic
    Text
    In This Topic

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

    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 DrawTextLayout method provided by the GcGraphics class.

    C#
    Copy Code
    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);
    }
    
    Back to Top

    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.

    C#
    Copy Code
    tl.TextAlignment = TextAlignment.Trailing;
    
    Back to Top

    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.

    C#
    Copy Code
    TextFormat tf = new TextFormat() 
    {
      Font = StandardFonts.Courier,
      FontSize = 14,
      FontStyle = FontStyle.Bold,
      ForeColor = GrapeCity.Documents.Drawing.Color.Cyan,
      Language = Language.English
    };
    
    Back to Top

    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.
      C#
      Copy Code
      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);
      }
      
    Back to Top

    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.

    C#
    Copy Code
    // 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);
    
    Back to Top

    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.

    C#
    Copy Code
    // 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);
    
    Back to Top

    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.

    C#
    Copy Code
    // Character trimming
    tl.TrimmingGranularity = TrimmingGranularity.Character;
    
    tl.EllipsisCharCode = 0x007E;
    
    // Set wrap mode to character wrap
    tl.WrapMode = WrapMode.CharWrap;
    
    Back to Top

    Subscript and Superscript

    To render subscript and superscript text in a PDF document, use the Subscript and Superscript properties provided by the TextFormat class.

    C#
    Copy Code
    //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);
    
    Back to Top

    Handle Paragraph

    To handle paragraph formatting, use the properties provided by TextLayout class to set the paragraph alignment and formatting.

    C#
    Copy Code
    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);
    }
    
    Back to Top

    Extract Text

    To extract text from a PDF:

    1. Load a PDF document using Load method of the GcPdfDocument class.
    2. Extract text from the last page of the PDF using GetText method of the Page class.
    3. Add the extracted text to another PDF document using the Graphics.DrawString method.
    4. Save the document using Save method of the GcPdfDocument class.
      C#
      Copy Code
      GcPdfDocument doc = new GcPdfDocument();
      
      FileStream fs = new FileStream("GcPdf.pdf",FileMode.Open,FileAccess.Read);
      doc.Load(fs);
      
      //Extract text present on the last page
      String text=doc.Pages.Last.GetText();
      
      //Add extracted text to a new pdf 
      GcPdfDocument doc1 = new GcPdfDocument();
      PointF textPt = new PointF(72, 72);
      doc1.NewPage().Graphics.DrawString(text, new TextFormat()
              { FontName = "ARIAL", FontItalic = true }, textPt);
      
      doc1.Save("NewDocument.pdf"); 
      
      Console.WriteLine("Press any key to exit");  
      Console.ReadKey();
      

    Similarly, you can also extract all the text from a document by using GetText method of the GcPdfDocument class.

    Back to Top

    Extract Text using ITextMap

    GcPdf provides ITextMap interface that represents the text map of a page in a GcPdf document. It helps you to find the geometric positions of the text lines on a page and extract the text from a specific position.

    The text map for a specific page in the document can be retrieved using the GetTextMap method of the Page class, which returns an object of type ITextMap. ITextMap provides four overloads of the GetFragment method, which helps to retrieve the text range and the text within the range. The text range is represented by the TextMapFragment class and each line of text in this range is represented by the TextLineFragment class.

    The example code below uses the GetFragment(out TextMapFragment range, out string text) overload to retrieve the geometric positions of all the text lines on a page and the GetFragment(MapPos startPos, MapPos endPos, out TextMapFragment range, out string text) overload to retrieve the text from a specific position in the page.

    C#
    Copy Code
    // Open an arbitrary PDF, load it into a temp document and use the map to find some texts:
    using (var fs = new FileStream("Test.pdf", FileMode.Open, FileAccess.Read))
    {
        var doc1 = new GcPdfDocument();
        doc1.Load(fs);
        var tmap = doc1.Pages[0].GetTextMap();
    
        // We retrieve the text at a specific (known to us) geometric location on the page:
        float tx0 = 2.1f, ty0 = 3.37f, tx1 = 3.1f, ty1 = 3.5f;
        HitTestInfo htiFrom = tmap.HitTest(tx0 * 72, ty0 * 72);
        HitTestInfo htiTo = tmap.HitTest(ty0 * 72, ty1 * 72);
        tmap.GetFragment(htiFrom.Pos, htiTo.Pos, out TextMapFragment range1, out string text1);
        tl.AppendLine($"Looked for text inside rectangle x={tx0:F2}\", y={ty0:F2}\", " +
            $"width={tx1 - tx0:F2}\", height={ty1 - ty0:F2}\", found:");
        tl.AppendLine(text1);
        tl.AppendLine();
    
        // Get all text fragments and their locations on the page:
        tl.AppendLine("List of all texts found on the page");
        tmap.GetFragment(out TextMapFragment range, out string text);
        foreach (TextLineFragment tlf in range)
        {
            var coords = tmap.GetCoords(tlf);
            tl.Append($"Text at ({coords.B.X / 72:F2}\",{coords.B.Y / 72:F2}\"):\t");
            tl.AppendLine(tmap.GetText(tlf));
        }
        // Print the results:
        tl.PerformLayout(true);
    }
    
    Back to Top

    Extract Text Paragraphs

    GcPdf allows extracting text paragraphs from a PDF document by using Paragraphs property of ITextMap interface. It returns a collection of ITextParagraph objects associated with the text map.

    Sometimes, PDF documents might contain some repeating text (for example, overlap of same text to show it as bold) but GcPdf extracts such text without returning the redundant lines. Also the tables with multi-line text in cells are correctly recognized as text paragraphs.

    The example code below shows how to extract all text paragraphs of a PDF document:

    C#
    Copy Code
    GcPdfDocument doc = new GcPdfDocument();
    var page = doc.NewPage();
    var tl = page.Graphics.CreateTextLayout();
    tl.MaxWidth = doc.PageSize.Width;
    tl.MaxHeight = doc.PageSize.Height;
    
    //Text split options for widow/orphan control
    TextSplitOptions to = new TextSplitOptions(tl)
    {
        MinLinesInFirstParagraph = 2,
        MinLinesInLastParagraph = 2,
    };
    
    //Open a PDF, load it into a temp document and get all page texts
    using (var fs=new FileStream("Wetlands.pdf", FileMode.Open, FileAccess.Read))
    {
        var doc1 = new GcPdfDocument();
        doc1.Load(fs);
    
        for (int i = 0; i < doc1.Pages.Count; ++i)
        {
            tl.AppendLine(string.Format("Paragraphs from page {0} of the original PDF:", i + 1));
    
            var pg = doc1.Pages[i];
            var pars = pg.GetTextMap().Paragraphs;
            foreach (var par in pars)
            {
                tl.AppendLine(par.GetText());
            }
        }
    
        tl.PerformLayout(true);
        while (true)
        {
            //'rest' will accept the text that did not fit
            var splitResult = tl.Split(to, out TextLayout rest);
            doc.Pages.Last.Graphics.DrawTextLayout(tl, PointF.Empty);
            if (splitResult != SplitResult.Split)
                break;
            tl = rest;
            doc.NewPage();
        }
        //Append the original document for reference
        doc.MergeWithDocument(doc1, new MergeDocumentOptions());
    }
    //Save document
    doc.Save(stream);
    return doc.Pages.Count;
    

    Limitations

    For more information about implementation of text properties using GcPdf, see GcPdf sample browser.