[]
A Range object represents an arbitrary contiguous range of a document body. It is used to identify specific portions of a document. GcWord 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.
//Load the existing word document in GcWord instance
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:
Get the range of a content object using GetPersistentRange method of the RangeBase class. For example, get the paragraph range starting from the first paragraph to the end of the second paragraph.
Display all the content objects within the accessed range on the console.
//Load the existing word document in GcWord instance
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:
Get the range of a content object using GetPersistentRange method of the RangeBase class. For example, get the paragraph range starting from the first paragraph to the end of the second paragraph.
Insert an object before or after the range. For example, create and insert a table after the paragraph using Insert method of the TableCollection class.
//Load the existing word document in GcWord instance
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:
Access the unknown object collection from the document body using Unknowns property of the RangeBase class.
Access the shapes and delete them from the document using Delete method of the ContentObject class.
//Load the existing word document in GcWord instance
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 GcWord, there are two types of range enumerators which differ on the basis of range of content objects:
The inner collection enumerator includes only those content objects which start and/or end inside the range. These can be enumerated by using GetInnerCollection method of RangeBase class.
The default range enumerator includes content objects lying in the inner collection range as well as those which start before the range and end after the range.
To enumerate content objects with default and inner collection enumerator:
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().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.
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().Count; // includes Shape run
The GetChildren 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 GcWord, see GcWord sample browser.