Spell Check

8 Apr 20225 minutes to read

Document editor supports performing spell checking for any input text. You can perform spell checking for the text in Document Editor and it will provide suggestions for the mis-spelled words through dialog and in context menu.

<ejs-documenteditor isReadOnly=false enableEditor=true enableSelection=true enableSpellChecker=true id="container"></ejs-documenteditor>

<script>
    var documenteditor;
    document.addEventListener('DOMContentLoaded', function () {
        documenteditor = document.getElementById("container").ej2_instances[0];
        documenteditor.spellChecker.languageID = 1033 //LCID of "en-us";
        documenteditor.spellChecker.removeUnderline = false;
        documenteditor.spellChecker.allowSpellCheckAndSuggestion = true;
    });
</script>

Features

  • Supports context menu suggestions.
  • Provides build-in options to Ignore, Ignore All, Change, Change All for error words in spell checker dialog.

Enable SpellCheck

To enable spell check in DocumentEditor, set enableSpellCheck property as true and then configure SpellCheckSettings.

Disable SpellCheck

To disable spell check in DocumentEditor, set enableSpellCheck property as false or remove enableSpellCheck property initialization code. The default value of this property is false.

Spell check settings

Remove Underline

By default, mis-spelled words are marked with squiggly line. You can also disable this behavior by enabling the removeUnderline API and now, the squiggly lines will never be rendered for mis-spelled words.

this.container.documentEditor.spellChecker.removeUnderline = false;

AllowSpellCheckAndSuggestion

By default, on performing spell check in Document Editor, both spelling and suggestions of the mis-spelled words will be retrieved, and this mis-spelled words can be corrected through context menu suggestions. You can modify this behavior using the allowSpellCheckAndSuggestion API, which will perform only spell check.

this.container.documentEditor.spellChecker.allowSpellCheckAndSuggestion = false;

LanguageID

Document Editor provides multi-language spell check support. You can add as many languages (dictionaries) in the server-side and to use that language for spell checking in Document Editor, it must be matched with languageID you pass in the Document Editor.

this.container.documentEditor.spellChecker.languageID = 1033; //LCID of "en-us";

EnableOptimizedSpellCheck

Document editor provides option to spellcheck page by page when loading the documents. The default value of this property is false, so when opening the document spellcheck web API will be called for each word in the document. To optimize the frequency of spellcheck web API calls, you can enable this property.

this.container.documentEditor.spellChecker.enableOptimizedSpellCheck = true;

Spell check dictionary cache

Starting from v20.1.0.xx, the performance and memory usage of spell checker has been optimized by adding a static method to initialize the dictionaries with specified cache count.

By default, the spell checker holds only one language dictionary in memory. If you want to hold multiple dictionaries in memory, you need to set the cache limit by using InitializeDictionaries method as in the below example.

 List<DictionaryData> spellDictCollection = new List<DictionaryData>();
 string personalDictPath = string.Empty;
 int cacheCount = 2;
 // Initialize dictionaries
 SpellChecker.InitializeDictionaries(spellDictCollection, personalDictPath, cacheCount);

If dictionaries are initialized using InitializeDictionaries method, then default constructor of the SpellChecker should be used to check spelling and get suggestion as in the below example code, it will prevent reinitialization of already loaded dictionaries.

public string SpellCheck([FromBody] SpellCheckJsonData spellChecker)
{
            try
      {
            SpellChecker spellCheck = new SpellChecker();
            spellCheck.GetSuggestions(spellChecker.LanguageID, spellChecker.TexttoCheck, spellChecker.CheckSpelling, spellChecker.CheckSuggestion, spellChecker.AddWord);
            return Newtonsoft.Json.JsonConvert.SerializeObject(spellCheck);
      }
      catch
      {
            return "{\"SpellCollection\":[],\"HasSpellingError\":false,\"Suggestions\":null}";
      }
}

Previously on every SpellChecker.GetSuggestion() method call, the .aff and dictionary data will be parsed to generate suggestion for miss spelled word. But, starting from v20.1.0.xx, the .aff and dictionary data will be parsed only for the first time alone while calling SpellChecker.GetSuggestion() method.

Context menu

Right click on error word to open the context menu with spell check options.

Spell check option in context menu

Suggestions

Context menu shows the suggestions for mis-spelled words. By clicking on the required word from suggestion, the error word gets replaced automatically.

Add To Dictionary

Using this option, you can add the current word to the dictionary. So that the spell checker does not consider that word as error in future.

Ignore Once and Ignore All

If you do not wish to add the word to dictionary and do not want to show error, use Ignore Once or Ignore All options.

Ignore: ignore only the current occurrence of a word from error.

Ignore All: ignore all occurrence of a word from error in the entire document.

Spelling

Using this option, you can open spell check dialog.

Spell check dialog

  • Refer to the Spell checker link for configuring spell checker in server-side.