Skip to main content
A newer version of this page is available. .

SubDocument Class

A sub-document in a document.

Namespace: DevExpress.Blazor.RichEdit

Assembly: DevExpress.Blazor.RichEdit.v21.2.dll

NuGet Package: DevExpress.Blazor.RichEdit

Declaration

public class SubDocument :
    IBatchUpdateHandler

The following members return SubDocument objects:

Remarks

Document content is divided into parts — sub-documents.

Document Model

The DxRichEdit control supports the following sub-document types:

Main

Every document contains one main sub-document. Use the DocumentAPI property to access this sub-document.

<DxRichEdit @ref="richEdit" />

@code {
    DxRichEdit richEdit;
    Document documentAPI;
    @* ... *@
    /* Surround the code that contains an asynchronous operation with a try-catch block to handle
    the OperationCanceledException. This exception is thrown when an asynchronous operation is cancelled. */
        try {
            documentAPI = richEdit.DocumentAPI;
            @* ... *@
            Section firstSection = await documentAPI.Sections.GetAsync(0);
            @* ... *@
        }
        catch (OperationCanceledException e) {
            Console.WriteLine($"{nameof(OperationCanceledException)} thrown with message: {e.Message}");
        }
}
Header and Footer

A document can contain multiple headers and footers for every section: different variants for odd/even pages and a specific version for the first page. To obtain a section’s header or footer, call the GetHeaderAsync or GetFooterAsync method, respectively.

<DxRichEdit @ref="richEdit" />

@code {
    DxRichEdit richEdit;
    Document documentAPI;
    @* ... *@
    /* Surround the code that contains an asynchronous operation with a try-catch block to handle
    the OperationCanceledException. This exception is thrown when an asynchronous operation is cancelled. */
        try {
            documentAPI = richEdit.DocumentAPI;
            @* ... *@
            Section section = await documentAPI.Sections.GetAsync(0);
            var footer = await section.GetFooterAsync(HeaderFooterType.Primary, true);
            await footer.AddTextAsync(DateTime.Now.Year.ToString());
            @* ... *@
        }
        catch (OperationCanceledException e) {
            Console.WriteLine($"{nameof(OperationCanceledException)} thrown with message: {e.Message}");
        }
}
TextBox
Each text box is an individual sub-document that can be placed in other sub-documents.

Use the Type property to identify a sub-document’s type.

Active Sub-Document

The sub-document to which the caret position belongs (or intervals of the current selection belong) is the active sub-document. Use the ActiveSubDocument property to get the active sub-document.

<DxRichEdit @bind-Selection="@selection" @ref="@richEdit" />

@code {
    DxRichEdit richEdit;
    Selection selection;
    @* ... *@
    /* Surround the code that contains an asynchronous operation with a try-catch block to handle
    the OperationCanceledException. This exception is thrown when an asynchronous operation is cancelled. */
        try {
        @* ... *@
            // Checks whether the main sub-document is active
            if (selection.ActiveSubDocument.Type == SubDocumentType.Main) {/*...*/}
            @* ... *@
        }
        catch (OperationCanceledException e) {
            Console.WriteLine($"{nameof(OperationCanceledException)} thrown with message: {e.Message}");
        }
}

Structural Elements

A SubDocument object allows you to get information about structural elements that it contains. Use the following properties to access element collections:

Work with Sub-Document Text

The SubDocument class implements methods that allow you to work with its text.

Add Text

Call a AddTextAsync method overload to add text to the sub-document.

await richEdit.DocumentAPI.AddTextAsync(0, "Ayn Rand was a Russian-American novelist...");

Find Text

Call the FindAllAsync(String, Boolean) method to find all matches of the specified text in the sub-document.

var DX_Intervals = await richEdit.DocumentAPI.FindAllAsync("DevExpress", true);

Format Text

Call a GetTextSpanAsync method overload to get the text span placed within the specified interval. Then call the ChangePropertiesAsync(Action<CharacterProperties>) method to change properties of the text span’s characters.

// Applies bold formatting to characters in the selected intervals.
IReadOnlyList<Interval> intervals = selection.Intervals;
foreach (Interval interval in intervals) {
  TextSpan boldTextSpan = await richEdit.DocumentAPI.GetTextSpanAsync(interval);
  await boldTextSpan.ChangePropertiesAsync(properties => {
      properties.FontBold = true;
  });
}

Remove Text

Call a RemoveAsync method overload to remove the specified text interval.

firstParagraph = await documentAPI.Paragraphs.GetAsync(0);
await documentAPI.RemoveAsync(firstParagraph.Interval);

Implements

DevExpress.Utils.IBatchUpdateHandler

Inheritance

Object
SubDocument
See Also