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.
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]); |
To access all content within the range objects:
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"); } |
To insert objects before or after range objects:
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"); |
To remove shapes and drawing objects from 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 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"); |
For more information on how to work with range objects using GcWord, see GcWord sample browser.