Insert text in current position in Angular Document editor component

20 Dec 20226 minutes to read

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

Insert text in current cursor position

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

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

import { Component, ViewEncapsulation, ViewChild } from '@angular/core';
import {
    DocumentEditorContainerComponent, ToolbarService
} from '@syncfusion/ej2-angular-documenteditor';

      selector: 'app-container',
      // specifies the template string for the Document Editor container component
      template: `<div><button ejs-button (click)="insertText()" >Insert Text</button>
      <ejs-documenteditorcontainer #documenteditor_default serviceUrl="" height="600px" style="display:block" [enableToolbar]=true> </ejs-documenteditorcontainer></div>`,
      encapsulation: ViewEncapsulation.None,
      providers: [ToolbarService]
export class AppComponent {
    public container: DocumentEditorContainerComponent;

    public insertText(): void {
        // It will insert the provided text in current selection

Please check below gif which illustrates how to insert text in current cursor position on button click:

Insert text in current cursor position in Javascript document editor

Insert paragraph in current cursor position

To insert new paragraph at current selection, you can can use insertText 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 using web service. Then use paste API to insert the sfdt at current cursor position.

Note: Html string should be welformatted html. DocIO support only welformatted XHTML.

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

  • Send the HTML content to server side for SFDT conversion. Refer to the following example to send the HTML content to server side and inserting it in current cursor position.
import { Component, OnInit, ViewChild } from '@angular/core';
import {
} from '@syncfusion/ej2-angular-documenteditor';
      selector: 'app-root',
      // specifies the template string for the DocumentEditorContainer component
      template: `<ejs-documenteditorcontainer #documenteditor_default serviceUrl="" height="600px" style="display:block" [documentEditorSettings]= "searchHighlightColor" [enableToolbar]=true (created)="onCreated()"> </ejs-documenteditorcontainer>`,
      providers: [ToolbarService],
export class AppComponent implements OnInit {
  public container: DocumentEditorContainerComponent;
  ngOnInit(): void {}
  onCreated() {
    let proxy = this;
    let htmltags: string =
      "<?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>";
      document.getElementById('export').addEventListener('click', () => {
          let http: XMLHttpRequest = new XMLHttpRequest();
'POST', 'http://localhost:5000/api/documenteditor/LoadString');
          http.setRequestHeader('Content-Type', 'application/json;charset=UTF-8');
          http.responseType = 'json';
          http.onreadystatechange = function () {
            if (http.readyState === 4) {
              if (http.status === 200 || http.status === 304) {
                // Insert the sfdt content in cursor position using paste API
              } else {

          let htmlContent: any = { content: htmltags };
  • Please refer the following code example for server-side web implementation for HTML conversion using DocumentEditor.
//API controller for the conversion.
public string LoadString([FromBody]InputParameter data)
      // You can also load HTML file/string from server side.
      Syncfusion.EJ2.DocumentEditor.WordDocument document = Syncfusion.EJ2.DocumentEditor.WordDocument.LoadString(data.content, FormatType.Html); // Convert the HTML to SFDT format.
      string json = Newtonsoft.Json.JsonConvert.SerializeObject(document);
      return json;

public class InputParameter
      public string content {get; set; }

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.