Log In
Home
Support
Demos
Documentation
Blogs
Training
Webinars
[Expand]General Information
[Collapse]WinForms Controls
  Prerequisites
 [Expand]What's Installed
 [Expand]Build an Application
 [Collapse]Controls and Libraries
  [Expand]Forms and User Controls
  [Expand]Messages, Notifications and Dialogs
  [Expand]Editors and Simple Controls
  [Expand]Ribbon, Bars and Menu
  [Expand]Application UI Manager
  [Expand]Docking Library
  [Expand]Data Grid
  [Expand]Vertical Grid
  [Expand]Pivot Grid
  [Expand]Tree List
  [Expand]Chart Control
  [Expand]Diagrams
  [Expand]Gauges
  [Expand]Map Control
  [Expand]Scheduler
  [Expand]Spreadsheet
  [Collapse]Rich Text Editor
    Overview
   [Expand]Getting Started
   [Collapse]Fundamentals
    [Expand]RichEditControl
    [Collapse]Document
      Document Model
      Document Layout
      Positions and Ranges
      Characters
      Paragraphs
      Inline Pictures
      Document Fields
      Hyperlinks and Bookmarks
      Headers and Footers
      Tables
      Range Permissions
      Sections
      Styles
      Numbered and Bulleted Lists
      Floating Objects (Shapes)
      Text Boxes
      Comments
      Document Properties
     Supported Document Formats
    [Expand]Fields
     Commands
     Services
     Layout API
     Product Class Structure
     Default Keyboard Shortcuts
     Measure Units
     HTML Tag Interpretation
   [Expand]Concepts
   [Expand]Visual Elements
   [Expand]Examples
   [Expand]Member Tables
  [Expand]Spell Checker
  [Expand]Form Layout Managers
  [Expand]Navigation Controls
  [Expand]Printing-Exporting
  [Expand]PDF Viewer
   Reporting
  [Expand]Snap
  [Expand]TreeMap Control
 [Expand]Common Features
  Get More Help
 [Expand]API Reference
[Expand]ASP.NET Controls and MVC Extensions
[Expand]ASP.NET Bootstrap Controls
[Expand]WPF Controls
[Expand]Xamarin Controls
[Expand]Windows 10 App Controls
[Expand]Document Server
[Expand]Reporting
[Expand]Report Server
[Expand]Dashboard
[Expand]eXpressApp Framework
[Expand]CodeRush
[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.

Expanded See Also

How would you rate this topic?​​​​​​​