Document Solutions for Word
Features / Text
In This Topic
    Text
    In This Topic

    DsWord provides Text class to handle text element in the body of a document. It allows you to add text to a word document using Add method of the TextCollection class. By default, DsWord adds text to a document in a single column which covers the body of the document from one margin to other. However, you can change this formatting and create multiple columns using the TextColumn class. This class represents a text column and all the columns of text in a section are represented through the TextColumnCollection class.

    Additionally, DsWord supports rendering text in multiple languages, including right-to-left, far eastern languages and vertical text with advanced paragraph formatting and multi-language font support. For right-to-left languages, you need to additionally set Bidi property of the ParagraphFormat class.

    For more information on vertical text, see Vertical text.

    Add Text

    To add text in a Word document:

    1. Access a section where you want to add text using the GetRange method. For example, access the first run of a section.
    2. Access the text collection using Texts property of the RangeBase class.
    3. Insert text into the text collection using Insert method of the TextCollection class.
      C#
      Copy Code
      doc.Load("SampleDoc.docx");
      
      //Add Text
      doc.Body.Sections.First.GetRange().Runs.First.GetRange().Texts.Insert(
          " Hello World! This document is created using DsWord. ", InsertLocation.End);
      
      //Insert font formatting for the run
      Style s = doc.Styles.Add("NewStyle", StyleType.Character);
      s.Font.Name = "Times New Roman";
      s.Font.Underline = Underline.Thick;
      doc.Body.Sections.First.GetRange().Runs.First.Style = s;
      
      //Add new run and text
      doc.Body.Sections.First.GetRange().Paragraphs.First.GetRange().Runs.Insert("New line " +
          "of the second run", InsertLocation.End);            
      
      //Save the document
      doc.Save("AddText.docx");
    Back to Top

    Modify Text

    To modify text in a Word document:

    1. Access a run and the run text to modify text using the Runs and Texts property of the RangeBase class. For example, access the first run and the first run text.
    2. Split the run text using Split method of the Text class and split the run using Split method of the Run class respectively.
    3. Apply styling to the first and the second run. For example, italicize and bold the run text. 
      C#
      Copy Code
      doc.Load("AddText.docx");
      
      //Access first run and change its font size
      Run firstRun = doc.Body.Sections.First.GetRange().Runs.First;
      firstRun.Font.Size = 16;
      
      //Get first run text
      Text firstRun_Text = firstRun.GetRange().Texts[0];
      
      //Split first run text
      Text splitText = firstRun_Text.Split(14);
      
      //Split Run
      Run splitRun = firstRun.Split(splitText, InsertLocation.Before);
      
      //Apply styling to first run
      firstRun_Text.ParentRun.Font.Bold = true;
      
      //Apply styling to second/new run
      splitRun.Font.Italic = true;
      
      //Save the document
      doc.Save("ModifyText.docx");
    Back to Top

    Delete Text

    To delete text in a Word document:

    1. Access the section where you want to delete text using the GetRange method. For example, access the first run of a section.
    2. Access the text from the text collection using Texts property of the RangeBase class.
    3. Delete the text using Delete method of the ContentObject class.
      C#
      Copy Code
      doc.Load("AddText.docx");
      
      //Delete Text
      doc.Body.Sections.First.GetRange().Runs.First.GetRange().Texts.First.Delete();
      
      doc.Save("DeleteText.docx");
    Back to Top

    Align Text

    To align text in a Word document:

    1. Access a paragraph from the paragraph collection using the Paragraphs property.
    2. Access a part of a paragraph from the run collection using the Runs property.
    3. Set the alignment of text in the paragraph using Alignment property of the ParagraphFormat class. For example, center align the text.
      C#
      Copy Code
      doc.Load("SampleDoc.docx");
      
      //Create a new style
      Style s = doc.Styles.Add("NewStyle", StyleType.Paragraph);
      s.Font.Name = "Times New Roman";
      s.Font.Underline = Underline.Thick;
      
      //Center align text
      s.ParagraphFormat.Alignment = ParagraphAlignment.Center;
      
      //Apply style to the paragraph
      Paragraph p = doc.Body.Sections.First.GetRange().Paragraphs.Add("Hello World!", s);
                  
      //Add text to the new paragraph
      p.GetRange().Runs.First.GetRange().Texts.Insert(" The text is center aligned.",
               InsertLocation.End);
      
      //Add new run and text to the first paragraph 
      doc.Body.Sections.First.GetRange().Paragraphs.First.GetRange().Runs.Insert(" It" +
          " includes an example of text alignment.", InsertLocation.End);
                  
      doc.Save("AlignText.docx");
    Back to Top

      Get Document Text

      To get whole text of the document, use the Body.Text property.

      C#
      Copy Code
      doc.Load("TestDoc.docx");
      
      //Fetch text at document level
      Console.WriteLine(doc.Body.Text);
      Back to Top

       

      Set Subscript and Superscript Text

      DsWord provides FontBase.VerticalPosition property of the SubSuperScript class for applying subscript and superscript styles on particular characters in the text to be rendered in the Word document. This property accepts values from the VerticalTextPosition enumeration.

      Superscript and subscript text in a Word document

      To add subscript or superscript text:

      1. Initialize the GcWordDocument class to create a Word document.
      2. Define a style using the Style class, with the StyleType set to character. This ensures that the mentioned style options apply to the individual characters in the paragraph or text run to which the style is applied.
      3. Set the VerticalPosition property of the defined character style to Subscript or Superscript, to allow the text to be repositioned as subscript or superscript.
      4. Loop through the characters of the paragraph to be rendered, and identify the characters which should be rendered as superscript or subscript. Apply the style with subscript/superscript settings to the character by adding a new run to the paragraph being rendered.
        C#
        Copy Code
        GcWordDocument doc = new GcWordDocument();
        
        //Create subscript and superscript styles
        Style subscriptStyle = doc.Styles.Add("Subscript Style",StyleType.Character);
        subscriptStyle.Font.VerticalPosition = VerticalTextPosition.Subscript;
        Style superscriptStyle = doc.Styles.Add("Superscript Style", StyleType.Character);
        superscriptStyle.Font.VerticalPosition = VerticalTextPosition.Superscript;
        
        //Add a paragraph
        var para1 = doc.Body.Sections.First.GetRange().Paragraphs.Add("Subscript: ");
        string chemicalFormula = "C6H12O6";
                    
        //Add text to paragraph1
        foreach(char c in chemicalFormula)
        {
            //If the character is a digit render it with the subscript style
            if (Char.IsDigit(c))
                para1.GetRange().Runs.Add(c.ToString(), subscriptStyle);
            else
                para1.GetRange().Runs.Add(c.ToString());
        }
        
        //Add another paragraph
        var para2= doc.Body.Sections.First.GetRange().Paragraphs.Add("Superscript: ");
        string date="January 1st";
        var dateFrags = Regex.Split(date, "(st)");
        
        //Add text to the paragraph2
        foreach (var frag in dateFrags)
        {
            //Render the text "st" with superscript style
            if (frag=="st")
                para2.GetRange().Runs.Add(frag, superscriptStyle);
            else
                para2.GetRange().Runs.Add(frag);
        }
        
        //Saves the document
        doc.Save("SubSuperScript.docx");

      For more information on how to render superscript and subscript text using DsWord, see DsWord sample browser.

      Fit Text Width

      DsWord lets you fit a text into the width specified by FontBase.FitTextWidth property of the FitTextWidth class which resizes every character of the text equally to achieve the purpose.

      To fit text into the specified width:

      1. Initialize the GcWordDocument class to create a Word document.
      2. Define a style using the Style class, with the StyleType set to character. This ensures that the mentioned style options apply to the individual characters in the paragraph or text run to which the style is applied.
      3. Set the FitTextWidth property of the defined style to a desired value, to define the width in which the rendered text should fit.
        C#
        Copy Code
        //Create new PDF document
        GcWordDocument doc = new GcWordDocument();
        Paragraph para = 
            doc.Body.Sections.First.GetRange().Paragraphs.Add(
                "FitTextWidth examples: \r\n");
        para.Style.Font.Size = 14;
        
        //Define styles with FitTextWidth and FitTextId settings
        Style run1Style = doc.Styles.Add("Style1", StyleType.Character);
        run1Style.Font.Bold = true;
        run1Style.Font.Size = 16;
        run1Style.Font.FitTextWidth = 400;
        run1Style.Font.FitTextId = 1;
        
        Style run2Style = doc.Styles.Add("Style2", StyleType.Character);
        run2Style.Font.Italic = true;
        run2Style.Font.Size = 18;
        run2Style.Font.FitTextWidth = 400;
        run2Style.Font.FitTextId = 1;
        
        //Apply the defined styles to the text
        para.GetRange().Runs.Add("This is run1 of paragraph1.", run1Style);
        para.GetRange().Runs.Add(" This is run2 of paragraph1 " +
            "with a different formatting.", run2Style);
        
        //Save the document
        doc.Save("FitTextWidth.docx");
      Back to Top

      Vertical Text

      DsWord supports rendering vertical text through TextFlowDirection property of the PageSetup class which accepts value from the TextFlowDirection enumeration. To set the vertical text alignment and reading order of the content in right to left direction, set this property to TopToBottomRightToLeft. This property draws the text in top-to-bottom, right-to-left layout. Additionally, DsWord supports rendering short upright Latin text or numbers in a block of vertical text referred to as 縦中横 (Tate Chu Yoko) in Japanese. You can set such blocks of horizontal text in vertical text through HorizontalInVertical property of the EastAsianLayout class.

      Vertical text in a Word document

      C#
      Copy Code
      doc.Load("Sample.docx");
      
      //Add heading
      doc.Body.Sections.First.GetRange().Paragraphs.Add("Japanese",
          doc.Styles[BuiltInStyleId.Heading1]);
      
      //Add a paragraph with Japanese text
      string text = "日本語(にほんご、にっぽんご)は、主として、" +
          "日本列島で使用されてきた言語である。日本手話を母語とする者などを除いて、" +
          "ほぼ全ての日本在住者は日本語を第一言語とする。";
      
      doc.Body.Sections.First.GetRange().Paragraphs.Add(text);
      
      //Use TopToBottomRightToLeft page layout to draw the japanese text
      doc.Body.Sections.First.PageSetup.TextFlowDirection = 
          TextFlowDirection.TopToBottomRightToLeft;
      
      //Save the document
      doc.Save("VerticalText.docx");
      Back to Top

      Find and Replace Text

      DsWord allows you to find and replace text in a document using the Find and Replace methods of RangeBaseFindReplaceExtensions class. The RangeBaseFindReplaceExtensions class extends RangeBase class and hence, its methods can be used on any range.

      The search pattern for the Find and Replace methods can be any static text, regex or special token. The supported special tokens are:

      Find Text

      The Find method finds all the occurences of a user-defined search pattern in a document. It also provides various find options using which you can search backwards, ignore case, define culture, use regular expressions or get formatting options

      To find text in a document:

      1. Specify the search pattern which needs to be found in a document.
      2. Load the document using Load method of GcWordDocument class.
      3. Find the specified search pattern using the Find method of RangeBaseFindReplaceExtensions class.   
      4. Get the count of total occurences of the search pattern by using the Count function.       
        C#
        Copy Code
        // find pattern contains static text and special token
        const string findPattern = "javascript framework&p";
        
        // Load the document
        var doc = new GcWordDocument();
        doc.Load("JsFrameworkExcerpt.docx");
        
        // Find all occurrences of the search string, ignoring the case
        var finds = doc.Body.Find(findPattern, new FindOptions(doc) { IgnoreCase = true });
        
        // Print the number of found occurrences at the top of the document
        doc.Body.Paragraphs.Insert($"Found {finds.Count()} occurrences of '{findPattern}' in the document.\n\n", InsertLocation.Start);
        
        // Done
        doc.Save("FindText.docx");
      Back to Top

      Replace Text

      The Replace method finds and replaces all the occurences of a user-defined search pattern in a document with the text to be replaced. The ReplaceAction enum specifies what action should be taken on finding a match, like replacing, skipping the current match or stopping the search.

      To find and replace text in a document using regex and special tokens:

      1. Create a new Word document by instantiating the GcWordDocument class.
      2. Add paragraphs using Add method of the ParagraphCollection class.
      3. Find and replace the specified search pattern (regex and special token) with a static string using Replace method of RangeBaseFindReplaceExtensions class. 
        C#
        Copy Code
        var doc = new GcWordDocument();
        
        // Add paragraph
        doc.Body.Paragraphs.Add("Document Solutions is a cross-platform solution for document management1");
        doc.Body.Paragraphs.Add("which aims to provide a universal document, editor and viewer solution for all popular document formats.");
        
        //Replace using regex and special token
        RangeBaseFindReplaceExtensions.Replace(doc.Body, "[0-9]&p", ". It");
        
        doc.Save("RemoveDigits.docx");
      Back to Top
      Note: A search pattern can also be removed from a document by passing the replaceText string as empty.

      If replacing text leads to any empty runs in a document, you can choose to delete those empty runs by using RemoveEmptyRuns property of FindReplaceOptions class.

      To replace text with empty text and remove empty runs in a document:

      1. Create a new Word document by instantiating the GcWordDocument class.
      2. Add a paragraph using Add method of the ParagraphCollection class.
      3. Add runs to the paragraph using Add method of RunCollection class
      4. Invoke the Replace method of the RangeBaseFindReplaceExtensions class to replace second and third runs with empty text and set RemoveEmptyRuns property to true.
        C#
        Copy Code
        var doc = new GcWordDocument();
        var para = doc.Body.Paragraphs.Add();
        para.GetRange().Runs.Add("first run");
        para.GetRange().Runs.Add(" second run");
        para.GetRange().Runs.Add(" third run");
        para.GetRange().Runs.Add(" fourth run");
        
        Console.WriteLine(String.Format(@"Current paragraph text is ""{0}"" and runs count = {1}", para.GetRange().Text, para.GetRange().Runs.Count));
        Console.WriteLine("Removed text from second and third runs");
        //Replace second and third runs with empty text and remove empty runs
        para.GetRange().Replace(" second run third run", "", new FindReplaceOptions(doc) { RemoveEmptyRuns = true });
        Console.WriteLine(String.Format(@"Current paragraph text is ""{0}"" and runs count = {1}", para.GetRange().Text, para.GetRange().Runs.Count));
        return doc;

      Replace Text using Regex and Special Tokens

      In case, each match is to be replaced by a different value (like numerals to textual representation), the FindReplaceOptions class provides callback functions for the same.

      To find and replace digits to their textual representation in a document using callback method:

      1. Create a new Word document by instantiating the GcWordDocument class.
      2. Add a paragraph using Add method of the ParagraphCollection class.
      3. Invoke the Replace method of the RangeBaseFindReplaceExtensions class to perform the replace operation.
      4. Pass the ReplacingCallback function with a callback method name as one of the parameters to it. This method is used to determine the replacement string.
        C#
        Copy Code
        /// Replace all digits with their textual representation
        /// <returns>Document without digits</returns>
        public void ReplaceDigitsWithText()
        {
            var doc = new GcWordDocument();
            var para = doc.Body.Paragraphs.Add("The game board was yellow with numbers; 2 digits, a space, and 3 digits. All the digits 0 to 9 were placed same width apart");
        
            doc.Body.Replace("[0-9]", "", new FindReplaceOptions(doc) { ReplacingCallback = new ReplaceDigitsWithTextCb().Replacing });
            doc.Save("NumeralsToTextualDigits.docx");
        }
        private class ReplaceDigitsWithTextCb
        {
            Dictionary<string, string> _numbers = new Dictionary<string, string>()
              {{"1","one"},{"2","two"},{"3","three"},{"4","four"},{"5","five"},{"6","six"},{"7","seven" },
                { "8","eight"},{"9","nine"},{"0","zero"}};
        
            //Callback method called on every found match. There we can examine found match
            //and change replacement string
            public ReplaceAction Replacing(ReplacingArgs args)
            {
                args.Replacement = _numbers.Keys.Contains(args.RegexDetails.Capture.Value) ? _numbers[args.RegexDetails.Capture.Value] : "_";
                return ReplaceAction.Replace;
            }
        }

      Back to Top

      Replace Formatting

      The find and replace operations can also be performed based on the formatting rules like any font, color or style etc.

      To find and replace green text in a document:

      1. Create a new Word document by instantiating the GcWordDocument class.
      2. Add a paragraph using Add method of the ParagraphCollection class.
      3. Add runs to the paragraph using Add method of RunCollection class.
      4. Apply red and green color to the runs separately by using the Color property of FontBase class.
      5. Create an instance of FindReplaceOptions class and configure its FormattingOptions property to search for text in the document which is green in color.
      6. Replace the green text with an empty string by using the Replace method of RangeBaseFindReplaceExtensions class.
        C#
        Copy Code
        //create document
        var doc = new GcWordDocument();
        //create paragraph
        var para = doc.Body.Paragraphs.Add();
        //add green text to paragraph
        var run_green = para.GetRange().Runs.Add("Document Solutions for Word library is a part of Document Solutions that " +
            "aims to be a complete solution to program and work with Word documents.");
        //add red text to paragraph
        var run_red = para.GetRange().Runs.Add("It offers a rich and comprehensive object model " +
            "which is simple to use and is based on Microsoft Office API, Word Javascript API and OpenXML SDK.");
        
        //color red text as red
        run_red.Font.Color.RGB = Color.Red;
        //color green text as green
        run_green.Font.Color.RGB = Color.Green;
        
        var fo = new FindReplaceOptions(doc);
        //find green text
        fo.FormattingOptions.Font.Color.RGB = Color.Green;
        
        //replace any found green text to empty string
        doc.Body.Replace("", "", fo);
        
        //save result document
        doc.Save("RemovingTextinGreenColor.docx");
      Back to Top

       

      For more information on how to work with text objects using DsWord, see DsWord sample browser.