[]
        
(Showing Draft Content)

Range Objects

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.

Access Range of Objects in a Document

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]);

Access all Content within the Range Objects

To access all content within the range objects:

  1. 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.

  2. 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");
    }

Insert Objects Before or After Range Objects

To insert objects before or after range objects:

  1. 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.

  2. 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");

Remove shapes and drawing objects from a document

To remove shapes and drawing objects from a document:

  1. Access the unknown object collection from the document body using Unknowns property of the RangeBase class.

  2. 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");

Enumerate Content Objects in a Range

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
        

Limitation

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.