[Expand]General Information
[Expand]WinForms Controls
[Expand]ASP.NET Controls and MVC Extensions
[Expand]ASP.NET Bootstrap Controls
[Expand]ASP.NET Core Bootstrap Controls
[Collapse]WPF Controls
  Prerequisites
 [Expand]What's Installed
 [Expand]Common Concepts
 [Expand]MVVM Framework
 [Collapse]Controls and Libraries
   Reporting
  [Expand]Data Grid
  [Expand]Ribbon, Bars and Menu
  [Expand]Charts Suite
  [Expand]Pivot Grid
  [Expand]Scheduler
  [Expand]Spreadsheet
  [Collapse]Rich Text Editor
   [Expand]Product Information
    Product Class Structure
    Supported Formats
   [Expand]Getting Started
   [Collapse]RichEditControl Document
    [Expand]Document Structure
    [Collapse]Document Elements
      Positions and Ranges
      Characters
      Paragraphs
      Inline Pictures
      Hyperlinks and Bookmarks
      Headers and Footers
      Tables
      Range Permissions
      Sections
      Styles
      Lists
      Floating Objects
      Text Boxes
      Comments
      Checkboxes
      Document Properties
   [Expand]Fields
    Text Formatting
    Import and Export
   [Expand]Page Layout
    Printing
    AutoCorrect
    Mail Merge
    Restrictions and Protection
    Syntax Highlighting
    Traversing the Document
   [Expand]Visual Elements
    Services
    Events
    Commands
    HTML Tag Support
   [Expand]Examples
  [Expand]Tree List
  [Expand]Gauge Controls
  [Expand]Map Control
  [Expand]Layout Management
  [Expand]Windows Modern UI
  [Expand]Printing-Exporting
  [Expand]Data Editors
  [Expand]Navigation Controls
  [Expand]Spell Checker
  [Expand]Property Grid
  [Expand]PDF Viewer
  [Expand]TreeMap Control
  [Expand]Diagram Control
  [Expand]Windows and Utility Controls
   Dialogs, Notifications and Panels
  [Expand]Scheduler (legacy)
 [Expand]Scaffolding Wizard
 [Expand]Localization
  Redistribution and Deployment
  Get More Help
 [Expand]API Reference
[Expand]Xamarin Controls
[Expand]Windows 10 App Controls
[Expand]Office File API
[Expand]Reporting
[Expand]Report and Dashboard Server
[Expand]Dashboard
[Expand]eXpressApp Framework
[Expand]CodeRush
[Expand]CodeRush Classic
[Expand]Cross-Platform Core Libraries
[Expand]Tools and Utilities
 End-User Documentation

Positions and Ranges

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

SubDocument.GetText

SubDocument.GetRtfText

SubDocument.GetHtmlText

SubDocument.GetMhtText

SubDocument.GetOpenXmlBytes

Paragraph

ReadOnlyParagraphCollection.Get

Section

Document.GetSection

Image

ReadOnlyDocumentImageCollection.Get

Shape (floating image or text box)

ReadOnlyShapeCollection.Get

Table

ReadOnlyTableCollection.Get

Hyperlink

ReadOnlyHyperlinkCollection.Get

Bookmark

ReadOnlyBookmarkCollection.Get

Comment

ReadOnlyCommentCollection.Get

Field

ReadOnlyFieldCollection.Get

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.

Is this topic helpful?​​​​​​​