Back to Devexpress

How to: Save words added via the Add to Dictionary option to a custom physical dictionary

aspnet-119369-components-rich-text-editor-examples-how-to-save-words-added-via-the-add-to-dictionary-option-to-a-custom-physical-dictionary.md

latest7.0 KB
Original Source

How to: Save words added via the Add to Dictionary option to a custom physical dictionary

  • Dec 17, 2020
  • 3 minutes to read

Currently, ASPxRichEdit doesn’t save words added by a user via the “Add to Dictionary” option in the physical dictionary file and stores these changes in a Session variable.If it’s necessary to save changes from the temporary dictionary stored in Session to a physical dictionary file on demand, you can accomplish this task programmatically, for example, on a button click. Get the temporary dictionary that has the SpellCheckerCachedCustomDictionary type from Session in the following way:

csharp
SpellCheckerCachedCustomDictionary dic = Session[ASPxRichEdit1.Settings.SpellChecker.Dictionaries[0].CacheKey] as SpellCheckerCachedCustomDictionary;

Note that you need to pass an index of your custom dictionary to the collection of other dictionaries used in your project as the Dictionaries collection index in this code.Then, pass words from that temporary dictionary to a new instance of your custom dictionary class derived from SpellCheckerCustomDictionary and save this custom dictionary to the appropriate physical path obtained from the temporary dictionary’s DictionaryPath property:

csharp
MyCustomDictionary dictionary = new MyCustomDictionary();
for (int i = 0; i < dic.WordCount; i++)
    dictionary.AddWord(dic[i]);
dictionary.SaveAs(dic.DictionaryPath);
csharp
using DevExpress.XtraSpellChecker;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e) {

    }
    protected void ASPxButton1_Click(object sender, EventArgs e) {
        SpellCheckerCachedCustomDictionary dic = Session[ASPxRichEdit1.Settings.SpellChecker.Dictionaries[0].CacheKey] as SpellCheckerCachedCustomDictionary;
        MyCustomDictionary dictionary = new MyCustomDictionary();
        for (int i = 0; i < dic.WordCount; i++)
            dictionary.AddWord(dic[i]);
        dictionary.SaveAs(dic.DictionaryPath);
    }
}

public class MyCustomDictionary : SpellCheckerCustomDictionary
{
    public MyCustomDictionary() : base() { }
    public MyCustomDictionary(string dictionaryPath, CultureInfo culture) : base(dictionaryPath, culture) { }
    public override bool Loaded { get { return true; } }
}
aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<%@ Register assembly="DevExpress.Web.ASPxRichEdit.v __, Version=__ , Culture=neutral, PublicKeyToken=b88d1754d700e49a" namespace="DevExpress.Web.ASPxRichEdit" tagprefix="dx" %>

<%@ Register assembly="DevExpress.Web.v __, Version=__ , Culture=neutral, PublicKeyToken=b88d1754d700e49a" namespace="DevExpress.Web" tagprefix="dx" %>
<%@ Register assembly="DevExpress.Web.ASPxSpellChecker.v __, Version=__ , Culture=neutral, PublicKeyToken=b88d1754d700e49a" namespace="DevExpress.Web.ASPxSpellChecker" tagprefix="dx" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
     <dx:ASPxRichEdit ID="ASPxRichEdit1" runat="server" Height="700px" Width="100%">
      <SettingsDocumentSelector>
       <UploadSettings Enabled="true" />
      </SettingsDocumentSelector>
      <Settings>
       <SpellChecker Enabled="True">
        <Dictionaries>
         <dx:ASPxSpellCheckerCustomDictionary AlphabetPath="~/EnglishAlphabet.txt" DictionaryPath="~/CustomEnglish.dic" CacheKey="Key1" />
        </Dictionaries>
        <OptionsSpelling IgnoreUpperCaseWords="true" />
       </SpellChecker>
      </Settings>
     </dx:ASPxRichEdit>
     <dx:ASPxButton ID="ASPxButton1" runat="server" Text="Save new added words to the dictionary" OnClick="ASPxButton1_Click">
     </dx:ASPxButton>
    </form>
</body>
</html>
aspx
<%@ Page Language="vb" AutoEventWireup="true" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<%@ Register assembly="DevExpress.Web.ASPxRichEdit.v __, Version=__ , Culture=neutral, PublicKeyToken=b88d1754d700e49a" namespace="DevExpress.Web.ASPxRichEdit" tagprefix="dx" %>

<%@ Register assembly="DevExpress.Web.v __, Version=__ , Culture=neutral, PublicKeyToken=b88d1754d700e49a" namespace="DevExpress.Web" tagprefix="dx" %>
<%@ Register assembly="DevExpress.Web.ASPxSpellChecker.v __, Version=__ , Culture=neutral, PublicKeyToken=b88d1754d700e49a" namespace="DevExpress.Web.ASPxSpellChecker" tagprefix="dx" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
     <dx:ASPxRichEdit ID="ASPxRichEdit1" runat="server" Height="700px" Width="100%">
      <SettingsDocumentSelector>
       <UploadSettings Enabled="true" />
      </SettingsDocumentSelector>
      <Settings>
       <SpellChecker Enabled="True">
        <Dictionaries>
         <dx:ASPxSpellCheckerCustomDictionary AlphabetPath="~/EnglishAlphabet.txt" DictionaryPath="~/CustomEnglish.dic" CacheKey="Key1" />
        </Dictionaries>
        <OptionsSpelling IgnoreUpperCaseWords="true" />
       </SpellChecker>
      </Settings>
     </dx:ASPxRichEdit>
     <dx:ASPxButton ID="ASPxButton1" runat="server" Text="Save new added words to the dictionary" OnClick="ASPxButton1_Click">
     </dx:ASPxButton>
    </form>
</body>
</html>
vb
Imports Microsoft.VisualBasic
Imports DevExpress.XtraSpellChecker
Imports System
Imports System.Collections.Generic
Imports System.Globalization
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls

Partial Public Class _Default
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

    End Sub
    Protected Sub ASPxButton1_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim dic As SpellCheckerCachedCustomDictionary = TryCast(Session(ASPxRichEdit1.Settings.SpellChecker.Dictionaries(0).CacheKey), SpellCheckerCachedCustomDictionary)
        Dim dictionary As New MyCustomDictionary()
        For i As Integer = 0 To dic.WordCount - 1
            dictionary.AddWord(dic(i))
        Next i
        dictionary.SaveAs(dic.DictionaryPath)
    End Sub
End Class

Public Class MyCustomDictionary
    Inherits SpellCheckerCustomDictionary
    Public Sub New()
        MyBase.New()
    End Sub
    Public Sub New(ByVal dictionaryPath As String, ByVal culture As CultureInfo)
        MyBase.New(dictionaryPath, culture)
    End Sub
    Public Overrides ReadOnly Property Loaded() As Boolean
        Get
            Return True
        End Get
    End Property
End Class