A Range object represents an arbitrary contiguous range of a document body. It is used to identify specific portions of a document. DsWord provides the Range class which represents a contiguous area of content objects in a document. This class is inherited from the RangeBase class which is the base class for range and body that allows manipulation of content in a document. The RangeBase class provides GetPersistentRange method which allows you to access the Range objects.
To access a range of a content object in a document, use GetPersistentRange method of the RangeBase class. For example, access the range starting from the first paragraph to the end of the second paragraph in a document.
C# |
Copy Code |
---|---|
//Load the existing word document doc.Load("TestRange.docx"); //Get Range which starts from the first paragraph and ends at the second paragraph Range paraRange = doc.Body.GetPersistentRange(doc.Body.Paragraphs.First, doc.Body.Paragraphs[1]); |
To access all content within the range objects:
C# |
Copy Code |
---|---|
//Load the existing word document doc.Load("TestRange.docx"); //Get Range which starts from the first paragraph and ends at the second paragraph Range paraRange = doc.Body.GetPersistentRange(doc.Body.Paragraphs.First, doc.Body.Paragraphs[1]); //Get all the content objects in the range Console.WriteLine("List of all the content objects contained in the range \n"); for (int i = 0; i < paraRange.Count; i++) { Console.WriteLine(paraRange[i] + "\n"); } |
To insert objects before or after range objects:
C# |
Copy Code |
---|---|
//Load the existing word document doc.Load("TestRange.docx"); //Get Range which starts from the first paragraph and ends at the second paragraph Range paraRange = doc.Body.GetPersistentRange(doc.Body.Paragraphs.First, doc.Body.Paragraphs[1]); //Insert bookamrk before the range paraRange.Bookmarks.Insert("Bookmark1", RangeLocation.Before); //Insert table after the range string[][] tableContent = new string[][] { new string[]{"0", "1", "2", "3"} , new string[]{"4", "5", "6", "7"} , new string[]{"8", "9", "10", "11"} }; paraRange.Tables.Insert(tableContent, InsertLocation.After); //Save the document with the changes doc.Save("NewTestRange.docx"); |
To remove shapes and drawing objects from a document:
C# |
Copy Code |
---|---|
//Load the existing word document doc.Load("TestRange.docx"); //Get range which starts from the first paragraph and ends at the second paragraph Range pictureRange = doc.Body.GetPersistentRange(doc.Body.Pictures.First, doc.Body.Paragraphs[1]); //Delete the last picture in the range pictureRange.Pictures.First.Delete(); //Access the unknown object collection UnknownContentCollection unknowns = doc.Body.Unknowns; Console.WriteLine("\n " + unknowns.Count.ToString()); for (int i = unknowns.Count - 1; i >= 0; i--) { if (unknowns[i].OuterXml.ToString().Contains("Oval 1") || unknowns[i].OuterXml.ToString().Contains("Isosceles Triangle 2")) //Delete the two shapes that exist in the document unknowns[0].Delete(); } //Save the document with the changes doc.Save("RemoveRange.docx"); |
In DsWord, there are two types of range enumerators which differ on the basis of range of content objects:
To enumerate content objects with default and inner collection enumerator:
C# |
Copy Code |
---|---|
GcWordDocument doc = new GcWordDocument(); Paragraph para = doc.Body.Paragraphs.Add(); Run run = para.GetRange().Runs.Add("xxx"); Shape shape = run.GetRange().Shapes.Add(); TextFrame frame = shape.AddTextFrame(); Range frameRange = frame.GetRange(); frameRange.Paragraphs.Add("yyy"); //default enumerator int count = frameRange.Runs.Count; // includes parent run "xxx" and "yyy" frame run //inner collection enumerator int innerCount = frameRange.GetInnerCollection<Run>().Count; // includes only "yyy" frame run |
To enumerate content objects in a parent-child relationship, you can use GetChildren method of ContentObject class. It returns the content objects directly related to a parent.
C# |
Copy Code |
---|---|
var doc = new GcWordDocument(); var para = doc.Body.Paragraphs.Add("x"); var shape = para.GetRange().Runs.First.GetRange().Shapes.Add(); shape.AddTextFrame("textframe"); int count = para.GetRange().Runs.Count; // includes "x" and Shape run int childCount = para.GetChildren<Run>().Count; // includes Shape run |
The GetChildren<T> method enumerates only ContentObjects and not ContentRanges or its derived classes such as Bookmark, Comment, EditableRange, ComplexField.
For more information on how to work with range objects using DsWord, see DsWord sample browser.