Back to Devexpress

DocumentRange Interface

officefileapi-devexpress-dot-xtrarichedit-dot-api-dot-native-1217010e.md

latest18.7 KB
Original Source

DocumentRange Interface

Defines a document range.

Namespace : DevExpress.XtraRichEdit.API.Native

Assembly : DevExpress.RichEdit.v25.2.Core.dll

NuGet Package : DevExpress.RichEdit.Core

Declaration

csharp
[ComVisible(true)]
public interface DocumentRange
vb
<ComVisible(True)>
Public Interface DocumentRange

The following members return DocumentRange objects:

Show 64 links

Remarks

The range is a distance between two positions (DocumentRange.Start and DocumentRange.End). The difference between the end and start position defines the DocumentRange.Length.

Insert and Delete Text

Text can be added to the range by the SubDocument.AppendText, SubDocument.InsertText or SubDocument.InsertSingleLineText methods. The DocumentRange.End) and DocumentRange.Length property values increase when new text elements are added.

Use the SubDocument.Delete method to delete characters from the range. After deletion, the DocumentRange.End) and DocumentRange.Length property values will lessen.

If the text preceding the range is removed, the range’s start and end positions move backward, i.e., the DocumentRange.Start and DocumentRange.End values decrease. The DocumentRange.Length value remains the same.

The code sample below obtains a range and calls the SubDocument.InsertText method to insert a text as follows:

ABNewTextCDEFGH

Range r1 starts at 1, ends at 11

Range r2 starts at 2, ends at 9

View Example

csharp
Document document = wordProcessor.Document;
document.AppendText("ABCDEFGH");

DocumentRange r1 = document.CreateRange(1, 3);
DocumentPosition pos1 = document.CreatePosition(2);

DocumentRange r2 = document.InsertText(pos1, ">>NewText<<");

string s1 = String.Format("Range r1 starts at {0}, ends at {1}", r1.Start, r1.End);
string s2 = String.Format("Range r2 starts at {0}, ends at {1}", r2.Start, r2.End);

document.Paragraphs.Append();
document.AppendText(s1);
document.Paragraphs.Append();
document.AppendText(s2);
vb
Dim document as Document = wordProcessor.Document
document.AppendText("ABCDEFGH")

Dim r1 As DocumentRange = document.CreateRange(1, 3)
Dim pos1 As DocumentPosition = document.CreatePosition(2)
Dim r2 As DocumentRange = document.InsertText(pos1, ">>NewText<<")

Dim s1 As String = String.Format("Range r1 starts at {0}, ends at {1}", r1.Start, r1.End)
Dim s2 As String = String.Format("Range r2 starts at {0}, ends at {1}", r2.Start, r2.End)

document.Paragraphs.Append()
document.AppendText(s1)
document.Paragraphs.Append()
document.AppendText(s2)

Fixed Range

The range returned by the SubDocument.AppendText method extends after each operation. This behavior may complicate certain situations.

You can implement an extension to the DocumentRange class to provide fixed ranges. Fixed ranges remain unchanged after they are obtained. The following code illustrates the CustomFixedRange implementation.

csharp
using System;
using System.Drawing;
using DevExpress.XtraRichEdit.API.Native;

public static class FixedRangeExtension
{
    public static CustomFixedRange GetFixedRange(this DocumentRange range)
    {
        return new CustomFixedRange(range);
    }
    public static CharacterProperties BeginUpdateCharacters
        (this Document document, CustomFixedRange range)
        {
        return document.BeginUpdateCharacters(range.CreateRange(document));
    }
}

public class CustomFixedRange
{
    int start;
    int length;
    public CustomFixedRange(DocumentRange range)
    {
        this.start = range.Start.ToInt();
        this.length = range.Length;
    }
    public DocumentRange CreateRange(Document document)
    {
        return document.CreateRange(start, length);
    }
}
vb
Imports System
Imports System.Drawing
Imports DevExpress.XtraRichEdit.API.Native

Public Module FixedRangeExtension
    Sub New()
    End Sub
     _
    Public Function GetFixedRange(ByVal range As DocumentRange) As CustomFixedRange
        Return New FixedRange(range)
    End Function
     _
    Public Function BeginUpdateCharacters(ByVal document As Document, ByVal range As CustomFixedRange) As CharacterProperties
        Return document.BeginUpdateCharacters(range.CreateRange(document))
    End Function
End Module

Public Class FixedRange
    Private start As Integer
    Private length As Integer
    Public Sub New(ByVal range As DocumentRange)
        Me.start = range.Start.ToInt()
        Me.length = range.Length
    End Sub
    Public Function CreateRange(ByVal document As Document) As DocumentRange
        Return document.CreateRange(start, length)
    End Function
End Class

This code snippet demonstrates how to format each appended text block using CustomFixedRange.

csharp
Document document = richEditControl1.Document;
document.AppendText("First text block without formatting. ");
var formattedRangeBold = document.AppendText("Second text block with bold font. ").GetFixedRange();
document.AppendText("Third text block without formatting. ");
var formattedRangeUnderlined = document.AppendText("Fourth text block with underlined font. ").GetFixedRange();

var charsBold = document.BeginUpdateCharacters(formattedRangeBold);
charsBold.Bold = true;
document.EndUpdateCharacters(charsBold);

var charsUnderline = document.BeginUpdateCharacters(formattedRangeUnderlined);
charsUnderline.Underline = UnderlineType.Single;
charsUnderline.UnderlineColor = Color.Brown;
document.EndUpdateCharacters(charsUnderline);
vb
Dim document As Document = richEditControl1.Document
document.AppendText("First text block without formatting. ")
Dim formattedRangeBold = document.AppendText("Second text block with bold font. ").GetFixedRange()
document.AppendText("Third text block without formatting. ")
Dim formattedRangeUnderlined = document.AppendText("Fourth text block with underlined font. ").GetFixedRange()

Dim charsBold = document.BeginUpdateCharacters(formattedRangeBold)
charsBold.Bold = True
document.EndUpdateCharacters(charsBold)

Dim charsUnderline = document.BeginUpdateCharacters(formattedRangeUnderlined)
charsUnderline.Underline = UnderlineType.Single
charsUnderline.UnderlineColor = Color.Brown
document.EndUpdateCharacters(charsUnderline)

Extension Methods

GetAsFrozen()

See Also

DocumentRange Members

Positions and Ranges

DevExpress.XtraRichEdit.API.Native Namespace