How to select and retrieve the word and paragraph in Document Editor
22 Jun 20262 minutes to read
The Blazor DOCX Editor (Document Editor) component allows for getting the current word or paragraph content as plain text and SFDT (rich text).
Select and get the word in current cursor position
Use the SelectCurrentWordAsync API in the selection module to select the current word at the cursor position, and use the 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();
}
}