[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].NET Core 3 Support
 [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
      Paragraphs
      Inline Pictures
      Hyperlinks and Bookmarks
      Headers and Footers
      Tables
      Range Permissions
      Sections
      Lists
      Shapes
      Text Boxes
      Comments
      Checkboxes
      Document Properties
     Measure Units
   [Expand]Fields
    Text Formatting
    Import and Export
   [Expand]Page Layout
    Printing
    Hyphenation
    AutoCorrect
    Track Changes
    Mail Merge
    Restrictions and Protection
   [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]Gantt 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]eXpress Persistent Objects
[Expand]CodeRush
[Expand]CodeRush Classic
[Expand]Cross-Platform Core Libraries
[Expand]Tools and Utilities
 End-User Documentation
View this topic on docs.devexpress.com (Learn more)

Positions and Ranges

Position and range are key entities in the RichEditControl. They define the location of every object in a Document Model.

Expanded Position

Position (the DocumentPosition object) is a zero-based index of every symbol in a document. Each position identifies a possible placement for a text cursor. Position 0 is the beginning of a document and precedes the first character. When the caret moves to the next character, it moves to position 1 and so on.

Obtain a Position

The following code sample calls the SubDocument.CreatePosition method to obtain the document position at the given index and inserts a text in it:

You can use DocumentPosition objects to insert other elements into a document. The table below lists APIs that use a position as a parameter or returns a DocumentPosition object.

API Description
SubDocument.InsertText Inserts a given text string at the specified position.
SubDocument.InsertDocumentContent Inserts content from a range, stream or text string at the specified position.
ParagraphCollection.Insert Insert a new paragraph at the specified position.
Document.InsertSection Inserts a section break at the given position.
FieldCollection.Create Creates a new field at the specified position.
BookmarkCollection.Create Creates a new bookmark at the given position.
FormFieldCollection.InsertCheckBox Creates a new checkbox at the given position.
ShapeCollection.InsertPicture Inserts a new image at the given position.
ShapeCollection.InsertTextBox Inserts a new text box at the given position.
CommentCollection.Create Creates a comment anchored to the specified position
TableCollection.Create Inserts a new table with the given number of rows and columns in the specified position.

The code sample below appends an image at the end of the document:

Obtain the Position's Location

Use the following methods to obtain an approximate location of the required position.

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.

Expanded Range

The range (the DocumentRange (see DocumentRange) object) is an interval between two positions (DocumentRange.Start and DocumentRange.End). The difference between them is equal to DocumentRange.Length.

Obtain a Range

Use the SubDocument.CreateRange method to create a new DocumentRange object with the specified start and end positions.

The following API allows you to obtain a range associated with document elements or obtain elements from a specified range:

Object

Obtain a Range Related to an Element

Retrieve an Element from a Range

Text

SubDocument.Range

SubDocument.GetText

SubDocument.GetRtfText

SubDocument.GetHtmlText

SubDocument.GetMhtText

SubDocument.GetOpenXmlBytes

SubDocument.GetDocBytes

Paragraph

Paragraph.Range

ReadOnlyParagraphCollection.Get

Section

Section.Range

Document.GetSection

Image

DocumentImage.Range

ReadOnlyDocumentImageCollection.Get

Shape

Shape.Range

ReadOnlyShapeCollection.Get

Table

Table.Range

ReadOnlyTableCollection.Get

Hyperlink

Hyperlink.Range

ReadOnlyHyperlinkCollection.Get

Bookmark

Bookmark.Range

ReadOnlyBookmarkCollection.Get

Comment

Comment.Range

ReadOnlyCommentCollection.Get

Field

Field.Range

ReadOnlyFieldCollection.Get

The code sample below retrieves all images from the document and moves the first image to the header:

Expanded How Content Modifications Affect Ranges

When you modify content within a range, the range can expand or shrink automatically.

If you append a single line to a document and change its format options, the format is applied to the new content only.

Tip

Refer to the Text Formatting article for more information on how to format text.

If you sequentially append multiple fragments to a document, the DocumentRange object may expand with each new added fragment. The examples below demonstrate the possible results and two ways to resolve these issues.

Append two lines of text and then change the range's formatting, as shown below:

When you execute this code, it changes the format of all the text.

Solution #1

Change the format options before inserting new content to avoid this behavior. In this case, the new content remains unformatted.

Solution #2

Another way to format the initial content is to store the target range's position and length and create a new DocumentRange object.

The code sample below appends two lines to the document, but changes the format options only for the first line.

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