Back to Devexpress

SubDocument.GetText(DocumentRange, TextFragmentOptions) Method

officefileapi-devexpress-dot-xtrarichedit-dot-api-dot-native-dot-subdocument-dot-gettext-x28-devexpress-dot-xtrarichedit-dot-api-dot-native-dot-documentrange-devexpress-dot-xtrarichedit-dot-api-dot-native-dot-implementation-dot-textfragmentoptions-x29.md

latest7.2 KB
Original Source

SubDocument.GetText(DocumentRange, TextFragmentOptions) Method

Returns the plain text of the specified document range.

Namespace : DevExpress.XtraRichEdit.API.Native

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

NuGet Package : DevExpress.RichEdit.Core

Declaration

csharp
string GetText(
    DocumentRange range,
    TextFragmentOptions getTextOptions
)
vb
Function GetText(
    range As DocumentRange,
    getTextOptions As TextFragmentOptions
) As String

Parameters

NameTypeDescription
rangeDocumentRange

A DocumentRange object, specifying the range in the document for which the plain text representation is obtained.

| | getTextOptions | TextFragmentOptions |

A TextFragmentOptions object.

|

Returns

TypeDescription
String

A String value, containing the text of the specified range.

|

Remarks

Use the GetText method to get a plain text representation of any part of the document. This method overload triggers export-related events as described in the following table:

EventDescription
RichEditControl.BeforeExportDoes not occur.
RichEditControl.AfterExportDoes not occur.

Note

If you operate with a selection range, the GetText method should be combined with a DocumentRange.BeginUpdateDocument - DocumentRange.EndUpdateDocument method 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.

Example

This code snippet finds the first table of contents which is built upon Heading styles using the TOC field and converts it to plain text paragraph by paragraph. The SubDocument.GetText method overload is used for this purpose. It allows you to set the TextFragmentOptions.AllowExtendingDocumentRange option to false to not include the entire TOC when exporting the first paragraph.

View Example

csharp
using DevExpress.XtraRichEdit.API.Native.Implementation;
using DevExpress.XtraEditors;

document.LoadDocument("SampleTOC.docx");
string plainText = String.Empty;

foreach (Field item in document.Fields) {
    string fieldCode = document.GetText(item.CodeRange);
    string[] fieldParts = fieldCode.Split(' ');
    if (fieldParts[0].Trim() == "TOC" && fieldParts[1].Trim() == "\\h") {
        TextFragmentOptions options = 
            new TextFragmentOptions();
        options.AllowExtendingDocumentRange = false;
        foreach (Paragraph par in document.Paragraphs.Get(item.ResultRange)) {
            plainText += document.GetText(par.Range, options);
            plainText += Environment.NewLine;
        }
    }
}

XtraMessageBox.SmartTextWrap = false;
XtraMessageBox.Show(plainText);
vb
Imports DevExpress.XtraRichEdit.API.Native.Implementation
Imports DevExpress.XtraEditors

document.LoadDocument("SampleTOC.docx", DocumentFormat.Docx)
Dim plainText As String = String.Empty

For Each item As Field In document.Fields
    Dim fieldCode As String = document.GetText(item.CodeRange)
    Dim fieldParts() As String = fieldCode.Split(" "c)
    If fieldParts(0).Trim() = "TOC" AndAlso fieldParts(1).Trim() = "\h" Then
        Dim options As New TextFragmentOptions()
        options.AllowExtendingDocumentRange = False
        For Each par As Paragraph In document.Paragraphs.Get(item.ResultRange)
            plainText &= document.GetText(par.Range, options)
            plainText &= Environment.NewLine
        Next par
    End If
Next item

XtraMessageBox.SmartTextWrap = False
XtraMessageBox.Show(plainText)

The following code snippet (auto-collected from DevExpress Examples) contains a reference to the GetText(DocumentRange, TextFragmentOptions) method.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

winforms-richedit-document-api/CS/RichEditAPISample/CodeExamples/Export.cs#L110

csharp
foreach (Paragraph par in document.Paragraphs.Get(item.ResultRange)) {
    plainText += document.GetText(par.Range, options);
    plainText += Environment.NewLine;

winforms-richedit-document-api/VB/RichEditAPISample/CodeExamples/Export.vb#L98

vb
For Each par As DevExpress.XtraRichEdit.API.Native.Paragraph In document.Paragraphs.[Get](item.ResultRange)
    plainText += document.GetText(par.Range, options)
    plainText += System.Environment.NewLine

See Also

SubDocument Interface

SubDocument Members

DevExpress.XtraRichEdit.API.Native Namespace