Documents for Word .NET Edition
Features / Range Objects
In This Topic
    Range Objects
    In This Topic

    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.

    C#
    Copy Code
    //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]);
    Back to Top

    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.
      C#
      Copy Code
      //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");
      }
    Back to Top

    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.
      C#
      Copy Code
      //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");
    Back to Top

    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.
      C#
      Copy Code
      //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");
    Back to Top

    For more information on how to work with range objects using GcWord, see GcWord sample browser.