How to Get Current Word in ASP.NET Core DOCX Editor

27 Aug 20263 minutes to read

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

Select and get the word in 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 DOCX Editor component.

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

<div class="control-section">
    <ejs-documenteditorcontainer id="container" serviceUrl="/api/DocumentEditor/" enableToolbar=true created="onCreated" height="590px"></ejs-documenteditorcontainer>
</div>

<script>
  var documenteditor;
  var container;
  function onCreated() {
    var documenteditorElement = document.getElementById("container");
    container = documenteditorElement.ej2_instances[0];
    // To insert text in cursor position
    container.documentEditor.editor.insertText('Document editor');
    // To select the current word in document
    container.documentEditor.selection.selectCurrentWord();

    // To get the selected content as text
    var selectedContentText = container.documentEditor.selection.text;
    // To get the selected content as SFDT (rich text)
    var selectedContentSFDT = container.documentEditor.selection.sfdt;
  }
</script>

Select and get the paragraph in 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 DOCX Editor component.

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

<div class="control-section">
    <ejs-documenteditorcontainer id="container" serviceUrl="/api/DocumentEditor/" enableToolbar=true created="onCreated" height="590px"></ejs-documenteditorcontainer>
</div>

<script>
  var documenteditor;
  var container;
  function onCreated() {
    var documenteditorElement = document.getElementById("container");
    container = documenteditorElement.ej2_instances[0];
    // To insert text in cursor position
    container.documentEditor.editor.insertText('Document editor');
    // To select the current paragraph in document
    container.documentEditor.selection.selectParagraph();

    // To get the selected content as text
    var selectedContentText = container.documentEditor.selection.text;
    // To get the selected content as SFDT (rich text)
    var selectedContentSFDT = container.documentEditor.selection.sfdt;
  }
</script>