How to Get Current Word in TypeScript DOCX Editor
28 Aug 20263 minutes to read
You can get the current word or paragraph content from the TypeScript DOCX Editor (Document Editor) component as plain text and SFDT (rich text).
Select and get the word at the current cursor position
You can use the selectCurrentWord API in the selection module to select the current word at the cursor position and use the text API to get the selected content as plain text from the JavaScript DOCX Editor component.
The following example code illustrates how to select and get the current word as plain text.
import { DocumentEditorContainer, Toolbar } from '@syncfusion/ej2-documenteditor';
DocumentEditorContainer.Inject(Toolbar);
let container: DocumentEditorContainer = new DocumentEditorContainer({ enableToolbar: true, height: '590px' });
container.serviceUrl = 'https://document.syncfusion.com/web-services/docx-editor/api/documenteditor/';
container.appendTo('#container');
// To insert text at the cursor position
container.documentEditor.editor.insertText('Document editor');
// To select the current word in the document
container.documentEditor.selection.selectCurrentWord();
// To get the selected content as text
let selectedContentText: string = container.documentEditor.selection.text;
// To get the selected content as SFDT (rich text)
let selectedContentSFDT: string = container.documentEditor.selection.sfdt;The Web API hosted link
https://document.syncfusion.com/web-services/docx-editor/api/documenteditor/utilized in the DOCX Editor’s serviceUrl property is intended solely for demonstration and evaluation purposes. For production deployment, please host your own web service with your required server configurations. You can refer and reuse the GitHub Web Service example or Docker image for hosting your own web service and use for the serviceUrl property.
To get the bookmark content as SFDT (rich text), please check this link.
Select and get the paragraph at the current cursor position
You can use the selectParagraph API in the selection module to select the current paragraph at the cursor position and use the text API or the sfdt API to get the selected content as plain text or SFDT from the JavaScript DOCX Editor component.
The following example code illustrates how to select and get the current paragraph as SFDT.
import { DocumentEditorContainer, Toolbar } from '@syncfusion/ej2-documenteditor';
DocumentEditorContainer.Inject(Toolbar);
let container: DocumentEditorContainer = new DocumentEditorContainer({ enableToolbar: true, height: '590px' });
container.serviceUrl = 'https://document.syncfusion.com/web-services/docx-editor/api/documenteditor/';
container.appendTo('#container');
// To insert text at the cursor position
container.documentEditor.editor.insertText('Document editor');
// To select the current paragraph in the document
container.documentEditor.selection.selectParagraph();
// To get the selected content as text
let selectedContentText: string = container.documentEditor.selection.text;
// To get the selected content as SFDT (rich text)
let selectedContentSFDT: string = container.documentEditor.selection.sfdt;The Web API hosted link
https://document.syncfusion.com/web-services/docx-editor/api/documenteditor/utilized in the DOCX Editor’s serviceUrl property is intended solely for demonstration and evaluation purposes. For production deployment, please host your own web service with your required server configurations. You can refer and reuse the GitHub Web Service example or Docker image for hosting your own web service and use for the serviceUrl property.