Insert text , paragraph and rich-text content in Blazor DocumentEditor

2 May 20222 minutes to read

You can insert the text, paragraph and rich-text content in Blazor Document Editor component.

Insert text in current cursor position

You can use InsertTextAsync API in editor module to insert the text in current cursor position.

The following example code illustrates how to add the text in current selection.

// It will insert the provided text in current selection

Insert paragraph in current cursor position

To insert new paragraph at current selection, you can can use InsertTextAsync API with parameter as \r\n or \n.

The following example code illustrates how to add the new paragraph in current selection.

// It will add the new paragraph in current selection

Insert the rich-text content

To insert the HTML content, you have to convert the HTML content to SFDT Format and then use PasteAsync API to insert the sfdt at current cursor position.

Note: HTML string should be well formatted HTML. DocIO support only well formatted XHTML.

The following example illustrates how to insert the HTML content at current cursor position.

  • Refer to the following example for Converting the HTML content to SFDT and then insert it in current position using Pasts API.
@using Syncfusion.Blazor.DocumentEditor

<SfDocumentEditorContainer @ref="container" EnableToolbar=true>
    <DocumentEditorContainerEvents Created="OnCreated"></DocumentEditorContainerEvents>

@code {

    SfDocumentEditorContainer container;

    public async void OnCreated(object args)
        string htmltags = "<?xml version='1.0' encoding='utf - 8'?><!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN'''><html xmlns ='' xml:lang='en' lang ='en'><body><h1>The img element</h1><img src='' alt ='Lamp Image' width='500' height='600'/></body></html>";
        // You can also load HTML file/string .
        Syncfusion.Blazor.DocumentEditor.WordDocument document = Syncfusion.Blazor.DocumentEditor.WordDocument.LoadString(htmltags, ImportFormatType.Html); // Convert the HTML to SFDT format.
        string sfdtString = Newtonsoft.Json.JsonConvert.SerializeObject(document);
        // Insert the sfdt content in cursor position using paste API
        await container.DocumentEditor.Editor.PasteAsync(sfdtString);

Note: The above example illustrates inserting HTML content. Similarly, you can insert any rich-text content by converting any of the supported file formats (DOCX, DOC, WordML, HTML, RTF) to SFDT.