Positions and Ranges
- 3 minutes to read
Position and range are cornerstone Rich Edit entities that define the location of every object within a logical document structure.
Position is a zero-based index of every character within a text string. It is represented by the DocumentPosition class. The best visual presentation of the position is a text cursor (caret). Placed in the beginning of the document, it is located before the first character. This is a 0 position. When the cursor is moved forward to the next character, it moves to the position 1, and so on.
The range in turn is a distance between two positions, represented by the DocumentRange.Start and DocumentRange.End properties. The difference between the ending and starting position defines the DocumentRange.Length.
To create a DocumentRange, use the SubDocument.CreateRange method. To obtain a range of the selected document fragment, use the Document.Selection property.
The text can be added to the range by the SubDocument.AppendText, SubDocument.InsertText or SubDocument.InsertSingleLineText methods. Adding new text elements increases the DocumentRange.End and DocumentRange.Length property values.
To delete the characters from the range, use the SubDocument.Delete method. After deletion, the DocumentRange.End and DocumentRange.Length property values will lessen.
If the text preceding the range is removed, the DocumentRange.Length value remains the same, whereas the start and end range positions move backward, hence the DocumentRange.Start and DocumentRange.End values decrease.
Each document entity has the Range property, so you can get the range occupied by the specific unit. And vice versa, you can retrieve the document entity from the specified range. The following table shows what method should be used to retrieve any document entity.
Object | Method |
---|---|
Text | |
Paragraph | |
Section | |
Image | |
Shape (floating image or text box) | |
Table | |
Hyperlink | |
Bookmark | |
Comment | |
Field |
Note
If you operate with a selection range, any of the above-listed methods should be enclosed within DocumentRange.BeginUpdateDocument - DocumentRange.EndUpdateDocument methods pair. Otherwise, an incorrect document model might be selected, resulting in an exception “Error: specified document position or range belongs to other document or subdocument” being thrown.
Since there is no exact correspondence between the logical and physical document structures, it is hard to determine a line or a page where a certain range or position is located. But there are a few methods that can help you obtain an approximate location of the required position.
- To obtain coordinates of the rectangle surrounding the specified document position, use the RichEditControl.GetBoundsFromPosition method.
- The RichEditControl.GetPositionFromPoint method enables you to locate the position, which is situated close to the specified point.
- The RichEditView.GetCursorBounds provides coordinates of a rectangle encompassing the cursor.