How to select and retrieve the word and paragraph in Document Editor

2 Aug 20232 minutes to read

You can get the current word or paragraph content from the Blazor Document Editor component as plain text and SFDT (rich text).

Select and get the word in current cursor position

You can use SelectCurrentWordAsync API in selection module to select the current word at cursor position and use GetTextAsync API to get the selected content as plain text from Blazor Document Editor component.

The following example code illustrates how to select and get the current word as plain text.

@using Syncfusion.Blazor.DocumentEditor

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

@code {
    SfDocumentEditorContainer container;

    public async void OnCreated(object args)
    {
        // To insert text in cursor position
        await container.DocumentEditor.Editor.InsertTextAsync("Document editor");
        // To select the current word in document
        await container.DocumentEditor.Selection.SelectCurrentWordAsync();
        // To get the selected content as text
        string selectedContent = await container.DocumentEditor.Selection.GetTextAsync();
    }
}

To get the bookmark content as SFDT (rich text), check this link.

Select and get the paragraph in current cursor position

You can use SelectParagraphAsync API in selection module to select the current paragraph at cursor position and use GetTextAsync API or GetSfdtAsync API to get the selected content as plain text or SFDT from Blazor Document Editor component.

The following example code illustrates how to select and get the current paragraph as SFDT.

@using Syncfusion.Blazor.DocumentEditor

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

@code {
    SfDocumentEditorContainer container;

    public async void OnCreated(object args)
    {
        // To insert text in cursor position
        await container.DocumentEditor.Editor.InsertTextAsync("Document editor");
        // To select the current paragraph in document
        await container.DocumentEditor.Selection.SelectParagraphAsync();
        // To get the selected content as SFDT
        string selectedContent = await container.DocumentEditor.Selection.GetSfdtAsync();
    }
}