Back to Devexpress

How To: Obtain Specific Document Part

wpf-120503-controls-and-libraries-rich-text-editor-examples-text-how-to-obtain-specific-document-part.md

latest6.2 KB
Original Source

How To: Obtain Specific Document Part

  • Sep 25, 2025
  • 3 minutes to read

You can use the following approaches to get a DocumentRange occupied by a specific document part:

Get the Section Range

Use the Section.Range property to access the required section’s range.

Get the Page Range

Call the DocumentLayout.GetPage method to iterate through the document pages. The LayoutPage.MainContentRange property returns the obtained page’s range as a FixedRange object. Call the SubDocument.CreateRange method to convert the retrieved FixedRange to DocumentRange.

csharp
int pageCount = richEditControl1.DocumentLayout.GetPageCount();
for (int i = 0; i < pageCount; i++)
{
    LayoutPage layoutPage = richEditControl1.DocumentLayout.GetPage(i);
    DocumentRange mainBodyRange = richEditControl1.Document.CreateRange(layoutPage.MainContentRange.Start, layoutPage.MainContentRange.Length);                                
}
vb
Private Sub SurroundingSub()
    Dim pageCount As Integer = richEditControl1.DocumentLayout.GetPageCount()

    For i As Integer = 0 To pageCount - 1
        Dim layoutPage As LayoutPage = richEditControl1.DocumentLayout.GetPage(i)
        Dim mainBodyRange As DocumentRange = richEditControl1.Document.CreateRange(layoutPage.MainContentRange.Start, layoutPage.MainContentRange.Length)
    Next
End Sub

Get the Range of Paragraphs with Headings (if Heading1, Heading2, etc. style is applied to the headings)

Applying the Heading style to the paragraph changes its outline level. Find all paragraphs with the required Paragraph.OutlineLevel property value. Then obtain the range located between these paragraphs (including the paragraph with the heading).

csharp
Document document = richEditControl1.Document;
List<Paragraph> paragraphs = document.Paragraphs.Where(paragraph => paragraph.OutlineLevel == 1).ToList();
for (int i = 0; i < paragraphs.Count; i++)
    {     
        DocumentRange range;
        DocumentPosition startPos = paragraphs[i].Range.Start;
        if (i == paragraphs.Count - 1)
            range = document.CreateRange(startPos, document.Range.End.ToInt() - startPos.ToInt());
        else
            range = document.CreateRange(startPos, paragraphs[i + 1].Range.Start.ToInt() - startPos.ToInt());        
    }
vb
Dim document As Document = richEditControl1.Document
    Dim paragraphs As List(Of Paragraph) = document.Paragraphs.Where(Function(paragraph) paragraph.OutlineLevel = 1).ToList()

    For i As Integer = 0 To paragraphs.Count - 1
        Dim range As DocumentRange
        Dim startPos As DocumentPosition = paragraphs(i).Range.Start

        If i = paragraphs.Count - 1 Then
            range = document.CreateRange(startPos, document.Range.[End].ToInt() - startPos.ToInt())
        Else
            range = document.CreateRange(startPos, paragraphs(i + 1).Range.Start.ToInt() - startPos.ToInt())
        End If
    Next

Get Text from a Range

You can get text from the DocumentRange instance. The table below shows API used to retrieve and insert format-specific content:

|

Format

|

Retrieve

|

Insert

| | --- | --- | --- | |

Plain Text

|

SubDocument.GetText

|

SubDocument.AppendText - Appends the specified text.

SubDocument.AppendSingleLineText - Appends text as a single line.

SubDocument.InsertText - Inserts the specified text at the specified position.

SubDocument.InsertSingleLineText - Inserts a single line of text (text without line breaks) at the specified position.

| |

Rich Text Format

|

SubDocument.GetRtfText

|

SubDocument.AppendRtfText - Appends formatted text.

SubDocument.InsertRtfText - Inserts the specified formatted text into the specified position.

| |

HTML

|

SubDocument.GetHtmlText

|

SubDocument.AppendHtmlText - Appends HTML formatted text.

SubDocument.InsertHtmlText - Inserts the specified HTML text into the specified position.

| |

MHT

|

SubDocument.GetMhtText

| | |

WordML

|

SubDocument.GetWordMLText

| | |

OpenXML array

|

SubDocument.GetDocxBytes

| |