Text format in EJ2 JavaScript Document editor control

19 Jul 202314 minutes to read

Document Editor supports several formatting options for text like bold, italic, font color, highlight color, and more. This section describes how to modify the formatting for selected text in detail.

Bold

The bold formatting for selected text can be get or set by using the following sample code.

//Gets the value for bold formatting of selected text.
let bold : boolean = documenteditor.selection.characterFormat.bold;
//Sets bold formatting for selected text.
documenteditor.selection.characterFormat.bold = true;

You can toggle the bold formatting based on existing value at selection. Refer to the following sample code.

documenteditor.editor.toggleBold();

Italic

The Italic formatting for selected text can be get or set by using the following sample code.

//Gets the value for italic formatting of selected text.
let italic : boolean = documenteditor.selection.characterFormat.italic;
//Sets italic formatting for selected text.
documenteditor.selection.characterFormat.italic = true|false;

You can toggle the Italic formatting based on existing value at selection. Refer to the following sample code.

documenteditor.editor.toggleItalic();

Underline property

The underline style for selected text can be get or set by using the following sample code.

//Gets the value for underline formatting of selected text.
let underline : Underline = documenteditor.selection.characterFormat.underline;
//Sets underline formatting for selected text.
documenteditor.selection.characterFormat.underline = 'Single' | 'None';

You can toggle the underline style of selected text based on existing value at selection by specifying a value. Refer to the following sample code.

documenteditor.editor.toggleUnderline('Single');

Strikethrough property

The strikethrough style for selected text can be get or set by using the following sample code.

//Gets the value for strikethrough formatting of selected text.
let strikethrough : Strikethrough = documenteditor.selection.characterFormat.strikethrough;
//Sets strikethrough formatting for selected text.
documenteditor.selection.characterFormat.strikethrough = 'Single' | 'Normal';

You can toggle the strikethrough style of selected text based on existing value at selection by specifying a value. Refer to the following sample code.

documenteditor.editor.toggleStrikethrough();

Superscript property

The selected text can be made superscript by using the following sample code.

//Gets the value for baselineAlignment formatting of selected text.
let baselineAlignment : BaselineAlignment = documenteditor.selection.characterFormat.baselineAlignment;
//Sets baselineAlignment formatting for selected text.
documenteditor.selection.characterFormat.baselineAlignment = 'Superscript';

Toggle the selected text as superscript or normal using the following sample code.

documenteditor.editor.toggleSuperscript();

Subscript property

The selected text can be made subscript by using the following sample code.

//Gets the value for baselineAlignment formatting of selected text.
let baselineAlignment : BaselineAlignment = documenteditor.selection.characterFormat.baselineAlignment;
//Sets baselineAlignment formatting for selected text.
documenteditor.selection.characterFormat.baselineAlignment = 'Subscript';

Toggle the selected text as subscript or normal using the following sample code.

documenteditor.editor.toggleSubscript();

You can make a subscript or superscript text as normal using the following code.

documenteditor.selection.characterFormat.baselineAlignment = 'Normal';

Size

The size of selected text can be get or set using the following code.

//Gets the value for fontSize formatting of selected text.
let fontSize : number = documenteditor.selection.characterFormat.fontSize;
//Sets fontSize formatting for selected text.
documenteditor.selection.characterFormat.fontSize = 32;

Color

The color of selected text can be get or set using the following code.

//Gets the value for fontColor formatting of selected text.
let fontColor : string = documenteditor.selection.characterFormat.fontColor;
//Sets fontColor formatting for selected text.
documenteditor.selection.characterFormat.fontColor = 'Pink';
documenteditor.selection.characterFormat.fontColor = '#FFC0CB';

Font

The font style of selected text can be get or set using the following sample code.

//Gets the value for fontFamily formatting of selected text.
let baselineAlignment : string = documenteditor.selection.characterFormat.fontFamily;
//Sets fontFamily formatting for selected text.
documenteditor.selection.characterFormat.fontFamily = 'Arial';

Highlight color

The highlight color of the selected text can be get or set using the following sample code.

//Gets the value for highlightColor formatting of selected text.
let highlightColor : HighlightColor = documenteditor.selection.characterFormat.highlightColor;
//Sets highlightColor formatting for selected text.
documenteditor.selection.characterFormat.highlightColor = 'Pink';
documenteditor.selection.characterFormat.highlightColor = '#FFC0CB';

NOTE

  1. Character scaling and spacing present in the input Word document will be preserved in the exported Word document. N> 2. Scaling is implemented using the letterSpacing property, which may present compatibility problems. For more information, please refer to this link

Toolbar with options for text formatting

Refer to the following example.

var documenteditor = new ej.documenteditor.DocumentEditor({ isReadOnly: false, enableSelection: true, enableEditorHistory: true, enableEditor: true, enableSfdtExport: true });
function toolbarButtonClick(arg) {
  switch (arg.item.id) {
    case 'bold':
      //Toggles the bold of selected content
      documenteditor.editor.toggleBold();
      break;
    case 'italic':
      //Toggles the Italic of selected content
      documenteditor.editor.toggleItalic();
      break;
    case 'underline':
      //Toggles the underline of selected content
      documenteditor.editor.toggleUnderline('Single');
      break;
    case 'strikethrough':
      //Toggles the strikethrough of selected content
      documenteditor.editor.toggleStrikethrough();
      break;
    case 'subscript':
      //Toggles the subscript of selected content
      documenteditor.editor.toggleSubscript();
      break;
    case 'superscript':
      //Toggles the superscript of selected content
      documenteditor.editor.toggleSuperscript();
      break;
  }
}
//To change the font Style of selected content
function changeFontFamily(args) {
  documenteditor.selection.characterFormat.fontFamily = args.value;
  documenteditor.focusIn();
}
//To Change the font Size of selected content
function changeFontSize(args) {
  documenteditor.selection.characterFormat.fontSize = args.value;
  documenteditor.focusIn();
}
//To Change the font Color of selected content
function changeFontColor(args) {
  documenteditor.selection.characterFormat.fontColor = args.currentValue.hex;
  documenteditor.focusIn();
}
documenteditor.selectionChange = () => {
  setTimeout(() => { onSelectionChange(); }, 20);
};
//Selection change to retrieve formatting
function onSelectionChange() {
  if (documenteditor.selection) {
    enableDisableFontOptions();
    // #endregion
  }
}
function enableDisableFontOptions() {
  var characterformat = documenteditor.selection.characterFormat;
  var properties = [characterformat.bold, characterformat.italic, characterformat.underline, characterformat.strikeThrough];
  var toggleBtnId = ["bold", "italic", "underline", "strikethrough"];
  for (var i = 0; i < properties.length; i++) {
    changeActiveState(properties[i], toggleBtnId[i]);
  }
}
function changeActiveState(property, btnId) {
  var toggleBtn = document.getElementById(btnId);
  if ((typeof (property) == 'boolean' && property == true) || (typeof (property) == 'string' && property !== 'None'))
    toggleBtn.classList.add("e-btn-toggle");
  else {
    if (toggleBtn.classList.contains("e-btn-toggle"))
      toggleBtn.classList.remove("e-btn-toggle");
  }
}
var fontStyle = ['Algerian', 'Arial', 'Calibri', 'Cambria', 'Cambria Math', 'Candara', 'Courier New', 'Georgia', 'Impact', 'Segoe Print', 'Segoe Script', 'Segoe UI', 'Symbol', 'Times New Roman', 'Verdana', 'Windings'
];
var fontSize = ['8', '9', '10', '11', '12', '14', '16', '18',
  '20', '22', '24', '26', '28', '36', '48', '72', '96'];
var toolBar = new ej.navigations.Toolbar({
  clicked: toolbarButtonClick,
  items: [
    {
      prefixIcon: 'e-de-ctnr-bold e-icons',
      tooltipText: 'Bold',
      id: 'bold',
    },
    {
      prefixIcon: 'e-de-ctnr-italic e-icons',
      tooltipText: 'Italic',
      id: 'italic',
    },
    {
      prefixIcon: 'e-de-ctnr-underline e-icons',
      tooltipText: 'Underline',
      id: 'underline',
    },
    {
      prefixIcon: 'e-de-ctnr-strikethrough e-icons',
      tooltipText: 'Strikethrough',
      id: 'strikethrough',
    },
    {
      prefixIcon: 'e-de-ctnr-subscript e-icons',
      tooltipText: 'Subscript',
      id: 'subscript',
    },
    {
      prefixIcon: 'e-de-ctnr-superscript e-icons',
      tooltipText: 'Superscript',
      id: 'superscript',
    },
    { type: 'Separator' },
    {
      type: 'Input',
      template: new ej.inputs.ColorPicker({
        value: '#000000',
        showButtons: true,
        change: changeFontColor
      }),
    },
    { type: 'Separator' },
    {
      type: 'Input',
      template: new ej.dropdowns.ComboBox({
        dataSource: fontStyle,
        width: 120,
        index: 2,
        allowCustom: true,
        change: changeFontFamily,
        showClearButton: false,
      }),
    },
    {
      type: 'Input',
      template: new ej.dropdowns.ComboBox({
        dataSource: fontSize,
        width: 80,
        allowCustom: true,
        index: 2,
        change: changeFontSize,
        showClearButton: false,
      }),
    },
  ],
});
toolBar.appendTo('#toolbar');
documenteditor.appendTo('#DocumentEditor');
<!DOCTYPE html><html lang="en"><head>
    <title>EJ2 Animation</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="Typescript UI Controls">
    <meta name="author" content="Syncfusion">
    <link href="index.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/25.1.35/ej2-documenteditor/styles/fabric.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/25.1.35/ej2-buttons/styles/fabric.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/25.1.35/ej2-base/styles/fabric.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/25.1.35/ej2-dropdowns/styles/fabric.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/25.1.35/ej2-inputs/styles/fabric.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/25.1.35/ej2-lists/styles/fabric.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/25.1.35/ej2-navigations/styles/fabric.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/25.1.35/ej2-popups/styles/fabric.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/25.1.35/ej2-splitbuttons/styles/fabric.css" rel="stylesheet"> 
    
    
<script src="https://cdn.syncfusion.com/ej2/25.1.35/dist/ej2.min.js" type="text/javascript"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>

<body>
    
    <div id="container">
        <div id="toolbar">            
        </div>
        <div style="width:100%;height: 100%">
            <!--Element which will render as DocumentEditor -->
            <div id="DocumentEditor"></div>
        </div>
    </div>


<script>
var ele = document.getElementById('container');
if(ele) {
  ele.style.visibility = "visible";
}   
      </script>
<script src="index.js" type="text/javascript"></script>
</body></html>

See Also