.NET Hyphenation with NHunspell

To get a hyphenation with List & Label you can use the open source project NHunspell. Just insert the conditional separator Chr$(173) at separable places of a word. This can be done via the AutoDefineField event.

Starting with version 25, this can be done in exactly the same way in the ProcessText event. In this case, static texts entered by the user are also captured and can be hyphenated.

First, add a dependency to the NuGet package “NHunspell” to your project (note the license terms!).

Sample code C# :

using NHunspell;

// use an OpenOffice hyphenation dictionary here
Hyphen hyphen = new Hyphen(@"...\hyph_en_US.dic");
ListLabel LL = new ListLabel()
LL.AutoDefineField += new AutoDefineElementHandler(LL_AutoDefineField);
void LL_AutoDefineField(object sender, AutoDefineElementEventArgs element)
{
        string word = element.Value.ToString();
        if ((word != "System.Byte[]") && !string.IsNullOrEmpty(word))
        {
               HyphenResult hyphenated = hyphen.Hyphenate(word);
               // Hunspell marks the separable characters with "=", change this to the correct character
               element.Value = hyphenated.HyphenatedWord.Replace('=', (char)173);
         }
}

Sample code VB.NET :

Imports NHunspell
Using LL As New ListLabel()
Using hyphen As New Hyphen("...\hyph_en_US.dic")
LL.AutoDefineField += New AutoDefineElementHandler(AddressOf LL_AutoDefineField)
Private Sub LL_AutoDefineField(sender As Object, element As AutoDefineElementEventArgs)
                Dim word As String = element.Value.ToString()
                If (word <> "System.Byte[]") AndAlso Not String.IsNullOrEmpty(word) Then
                               Dim hyphenated As HyphenResult = hyphen.Hyphenate(word)
                               element.Value = hyphenated.HyphenatedWord.Replace("="C, CChar(173))
                End If
End Sub

Links:

https://hunspell.github.io/
https://www.nuget.org/packages/NHunspell/