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.
To add text in a Word document:
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"); |
To modify text in a Word document:
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"); |
To delete text in a Word document:
C# |
Copy Code |
---|---|
doc.Load("AddText.docx"); //Delete Text doc.Body.Sections.First.GetRange().Runs.First.GetRange().Texts.First.Delete(); doc.Save("DeleteText.docx"); |
To align text in a Word document:
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"); |
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); |
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.
To add subscript or superscript text:
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.
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:
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"); |
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.
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"); |
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:
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:
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"); |
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:
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"); |
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:
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; |
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:
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; } } |
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:
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"); |
For more information on how to work with text objects using DsWord, see DsWord sample browser.