Skip to main content

How To: Obtain a Specific Document Part

  • 3 minutes to read

This example demonstrates how to retrieve the DocumentRange occupied by a specific document part.

Obtain Section Content

Use the Section.Range property to access the required section’s range. Refer to the How to obtain a specific section content repository for a code example.

Obtain Page Content

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.

int pageCount = richEditControl1.DocumentLayout.GetPageCount();
for (int i = 0; i < pageCount; i++)
    LayoutPage layoutPage = richEditControl.DocumentLayout.GetPage(i);
    FixedRange contentRange = layoutPage.MainContentRange;
    DocumentRange mainBodyRange =
         richEditControl.Document.CreateRange(contentRange.Start, contentRange.Length);

Obtain Paragraphs with 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).

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());
    range = document.CreateRange(startPos, paragraphs[i + 1].Range.Start.ToInt() - startPos.ToInt());

Retrieve 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:




Plain Text


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.AppendRtfText - Appends formatted text.

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



SubDocument.AppendHtmlText - Appends HTML formatted text.

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





OpenXML array