Skip to main content

Document Class

A document in the Rich Text Editor.

Namespace: DevExpress.Blazor.RichEdit

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

NuGet Package: DevExpress.Blazor.RichEdit

Declaration

public class Document :
    SubDocument

The following members return Document objects:

Remarks

The Document class combines the document’s common and the main sub-document‘s functionalities.

How to Process a Batch of Changes in the Document without Intermediate Updates

Enclose your code in the BeginUpdate(Boolean) - EndUpdate() method calls to suppress the RichEdit’s visual updates and improve its performance when you make multiple changes to a document.

Each call to BeginUpdate(Boolean) should be paired with the EndUpdate() call. The BeginUpdate(Boolean) method locks the component to prevent it from re-rendering after each modification. The EndUpdate() method unlocks the control to apply all changes.

<DxRichEdit @ref="richEdit" />
@code {
    DxRichEdit richEdit;
    protected override Task OnAfterRenderAsync(bool firstRender) {
        if (firstRender)
            InitializeDocument();
        return base.OnAfterRenderAsync(firstRender);
    }

    async void InitializeDocument() {
    /* 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 canceled. */
        try {
            richEdit.DocumentAPI.BeginUpdate();
            IReadOnlyList<Paragraph> paragraphs = await richEdit.DocumentAPI.Paragraphs.GetAllAsync();
            foreach (Paragraph p in paragraphs)
                await p.ChangePropertiesAsync(properties => {/*...*/});
            richEdit.DocumentAPI.EndUpdate();
            }
        catch (OperationCanceledException e) {
            Console.WriteLine($"{nameof(OperationCanceledException)} thrown with message: {e.Message}");
        }
    }
}

How to Send a Batch of Changes to the Client in a Single Transaction

The Rich Text Editor sends data from the server to the client every time you make changes to a document. Enclose your code in the BeginTransaction() - EndTransaction() method calls to lock the RichEdit’s data transmission to the client when you make multiple changes to a document. Such transactions allow you to send a batch of changes to the client and improve the component’s performance.

Each BeginTransaction() method call should be paired with the EndTransaction() method call. The BeginTransaction() method marks the starting point of a server-client transaction and locks the component to prevent constant data transmission. The EndTransaction() method unlocks the component and sends all accumulated changes to the client.

Run Demo: Document API

The following code snippet accumulates paragraph changes on the server and sends them to the client in a single transaction:

<DxRichEdit @ref="richEdit" CssClass="w-100 ch-720"/>
@code {
    DxRichEdit richEdit;
    protected override async Task OnAfterRenderAsync(bool firstRender) {
        if(firstRender) {
            try {
                await InitializeDocument();
            } catch(TaskCanceledException) { }
        }

        await base.OnAfterRenderAsync(firstRender);
    }
    async Task InitializeDocument() {
        Document documentAPI = richEdit.DocumentAPI;
        documentAPI.BeginTransaction();
        await CreateBodyParagraph(documentAPI);
        await CreateTitleParagraph(documentAPI);
        await documentAPI.EndTransaction();
    }
    async Task CreateTitleParagraph(Document documentAPI) {
        Paragraph titleParagraph = await documentAPI.Paragraphs.CreateAsync(0);
        await titleParagraph.ChangePropertiesAsync(properties => { properties.SpacingAfter = 400; });
        TextSpan titleParagraphTextSpan = await documentAPI.AddTextAsync(0, "Albert Einstein");
        await titleParagraphTextSpan.ChangePropertiesAsync(properties => {
            properties.FontName = "Arial";
            properties.FontSize = 24;
            properties.FontBold = true;
        });
    }

    async Task CreateBodyParagraph(Document documentAPI) {
        Paragraph bodyParagraph = await documentAPI.Paragraphs.CreateAsync(0);
        await bodyParagraph.ChangePropertiesAsync(properties => {
            properties.FirstLineIndentType = FirstLineIndentType.Indented;
            properties.FirstLineIndent = 500;
        });
        string bodyParagraphText = "Albert Einstein (14 March 1879 - 18 April 1955) was a German-born theoretical physicist, widely acknowledged to be one of the greatest physicists of all time. Einstein is known for developing the theory of relativity, but he also made important contributions to the development of the theory of quantum mechanics.";
        TextSpan bodyParagraphTextSpan = await documentAPI.AddTextAsync(0, bodyParagraphText);
        await bodyParagraphTextSpan.ChangePropertiesAsync(properties => {
            properties.FontName = "Segoe UI";
            properties.FontSize = 12;
        });
        TextSpan boldTextSpan = await documentAPI.GetTextSpanAsync(0, 15);
        await boldTextSpan.ChangePropertiesAsync(properties => { properties.FontBold = true; });
        TextSpan italicTextSpan = await documentAPI.GetTextSpanAsync(16, 31);
        await italicTextSpan.ChangePropertiesAsync(properties => { properties.FontItalic = true; });
        await documentAPI.Hyperlinks.CreateAsync(bodyParagraphText.IndexOf("quantum mechanics"), "quantum mechanics".Length, url: "https://en.wikipedia.org/wiki/Quantum_mechanics");
        await documentAPI.Hyperlinks.CreateAsync(bodyParagraphText.IndexOf("theory of relativity"), "theory of relativity".Length, url: "https://en.wikipedia.org/wiki/Theory_of_relativity");
        await documentAPI.Hyperlinks.CreateAsync(bodyParagraphText.IndexOf("theoretical physicist"), "theoretical physicist".Length, url: "https://en.wikipedia.org/wiki/Theoretical_physics");
    }

}

Structural Elements

A Document object provides access to structural elements contained in the main sub-document. Use the following properties to access a specific element’s collection:

Use the Lists property to access a collection of the document’s List objects.

Implements

DevExpress.Utils.IBatchUpdateHandler

Inheritance

See Also