Click here to Skip to main content
15,860,972 members
Articles / Programming Languages / C#
Article

Tapping into the OpenOffice Spellchecker

Rate me:
Please Sign up or sign in to vote.
4.08/5 (5 votes)
25 Jan 2008CPOL2 min read 67.7K   346   22   10
A simple and inexpensive way to get your hands on a working spellchecker

Introduction

There are a few articles, including some from Microsoft, explaining how to use Microsoft Office’s spellchecker in other applications through automation and .NET. However, the Microsoft Office suite is expensive, so here is a method using the spellchecker in OpenOffice 2.3.0. Microsoft Office provides a dialog box, but OpenOffice doesn't, so we have to make our own. Not a problem. This example is written with Visual C# 2008 Express.

Using the Code

The finished program won't start unless OpenOffice is installed on the target machine. The LoadSpellChecker() function does what’s necessary to load the modules into memory. mxSpell is a class member of type XSpellChecker.

C#
private bool LoadSpellChecker()
{
    try
    {
        XComponentContext context = uno.util.Bootstrap.bootstrap();
        XMultiComponentFactory factory = context.getServiceManager();
        XLinguServiceManager lsm = 
            (XLinguServiceManager)factory.createInstanceWithContext(
                "com.sun.star.linguistic2.LinguServiceManager",
                context);
        mxSpell = lsm.getSpellChecker();
    }
    catch(unoidl.com.sun.star.uno.Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
    return mxSpell != null;
} 

In the spellchecking function, the Regex breaks the text in the TextBox into words for checking, and each word is fed in turn into XSpellChecker.spell(). This returns null if there is no alternative spelling suggestion - we take that to mean a correctly spelt word. Otherwise it returns a list of possible alternatives.

C#
private void btnCheck_Click(object sender, EventArgs e)
{
    int nCorrections = 0;
    PropertyValue[] aEmptyProps = new PropertyValue[0];
//  Locale aLocale = new Locale("en", "US", "");
    Locale aLocale = new Locale("en", "GB", "");

    Regex reg = new Regex("\\w+");
    MatchCollection words = reg.Matches(txtToCheck.Text);
    foreach(Match word in words)
    {
        txtToCheck.Select(word.Index, word.Length);
        XSpellAlternatives xAlt = mxSpell.spell(word.Value, aLocale, aEmptyProps);
        if(xAlt == null)
            continue;

        SpellCheckDlg dlg = new SpellCheckDlg();
        dlg.Word = word.Value;
        foreach(string str in xAlt.getAlternatives())
            dlg.ListBox.Items.Add(str);

        if(dlg.ListBox.Items.Count > 0)
        {
            dlg.EnableButton = true;
            dlg.ListBox.SelectedIndex = 0;
        }

        switch(dlg.ShowDialog())
        {
            case DialogResult.Yes:
                txtToCheck.SelectedText = dlg.Word;
                nCorrections++;
                break;

            case DialogResult.No:
                break;

            case DialogResult.Cancel:
                goto Finish;
        }
    }
    Finish: 

    msg.Text = string.Format("{0} error{1} corrected.", nCorrections,
                    nCorrections == 1 ? "" : "s");
} 

These alternatives are loaded into the ListBox in SpellCheckDlg, so that the user can decide what to do: replace the word, ignore the correction or quit the spellchecker - according to what is returned by the dialog. nCorrections keeps a count of corrections made, and this is subsequently displayed in the Label msg.

Points of Interest

It is not necessary to use the OpenOffice SDK in order to compile, but if you want to read the DevelopersGuide.pdf, you'll probably have to download the SDK. You'll need references in Visual Studio to the following DLLs:

  • C:\Program Files\OpenOffice.org 2.3\program\cli_uno.dll
  • C:\Program Files\OpenOffice.org 2.3\program\assembly\cli_basetypes.dll
  • C:\Program Files\OpenOffice.org 2.3\program\assembly\cli_cppuhelper.dll
  • C:\Program Files\OpenOffice.org 2.3\program\assembly\cli_types.dll

Of course, OpenOffice may be installed at some other place on your computer.

It is quite easy and interesting to change the language of the spellcheck, by changing the Locale which is fed into spell().

History

  • 25th January, 2008: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Instructor/Trainer
Hong Kong Hong Kong
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralOpen Office Spell Checker for .NET - spell checking without Open Office and the Open Office SDK Pin
Thomas Maierhofer (Tom)9-Sep-09 8:31
Thomas Maierhofer (Tom)9-Sep-09 8:31 
GeneralRe: Open Office Spell Checker for .NET - spell checking without Open Office and the Open Office SDK Pin
jimmiea2-Oct-09 3:18
jimmiea2-Oct-09 3:18 
GeneralRe: Open Office Spell Checker for .NET - spell checking without Open Office and the Open Office SDK Pin
Thomas Maierhofer (Tom)10-Nov-09 5:32
Thomas Maierhofer (Tom)10-Nov-09 5:32 
Questionversions Pin
lahatfield4-Nov-08 1:07
lahatfield4-Nov-08 1:07 
AnswerRe: versions Pin
ShaggyTN15-Nov-08 9:57
ShaggyTN15-Nov-08 9:57 
GeneralProblem try Ajax Spell Checker ASP.NET Pin
Member 26665682-Apr-08 2:47
Member 26665682-Apr-08 2:47 
GeneralRe: (Assemblies not available) Pin
kazim bhai26-May-09 0:00
kazim bhai26-May-09 0:00 
Generalread data from open office calc Pin
Member 50187691-Apr-08 5:13
Member 50187691-Apr-08 5:13 
GeneralRe: read data from open office calc Pin
Member 50187691-Apr-08 22:46
Member 50187691-Apr-08 22:46 
GeneralGr8 Pin
Priyank Bolia25-Jan-08 3:53
Priyank Bolia25-Jan-08 3:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.