Hyphenation with TX Spell .NET

TX Spell .NET provides different methods to hyphenate words in multilingual documents. It returns the hyphenation point before a specific divide position or an array of all hyphenation points for a defined word.

The source code for this sample is contained in the following directories:

  • Samples\WPF\CSharp\Hyphenation
  • Samples\WPF\VB.NET\Hyphenation

Relevant API Links

This sample uses the text of a TextBox as the input for TX Spell .NET to hyphenate a word. Additionally, it shows how to adjust typical settings such as the minimum number of characters at a word begin and word end.

Start the application and type in a word into the first TextBox. The bottom RichTextBox shows the hyphenated word immediately including the hyphenation positions.

Image

The required code is straightforward. The text from the TextBox is used in the Proofing.TXSpell.GetHyphenationPoints method of TX Spell that returns the possible hyphenation points. For demo purposes, these points are used to split the word into separate syllables, which are concatenated with a hyphen to illustrate the potential hyphenation points.

string sWordToHyphenate = textBox1.Text;
int[] iHyphenationPoints = txSpellChecker1.GetHyphenationPoints(sWordToHyphenate);

var parts = new List<string>(iHyphenationPoints.Length);

int index = 0;
foreach (int length in iHyphenationPoints)
{
    parts.Add(sWordToHyphenate.Substring(index, length - index));
    index = length;
}

string sHyphenatedString = "";

foreach (string part in parts)
    sHyphenatedString += part + "-";

richTextBox1.Selection.Text = sHyphenatedString;
Dim sWordToHyphenate As String = textBox1.Text
Dim iHyphenationPoints As Integer() = txSpellChecker1.GetHyphenationPoints(sWordToHyphenate)

Dim parts = New List(Of String)(iHyphenationPoints.Length)

Dim index As Integer = 0
For Each length As Integer In iHyphenationPoints
    parts.Add(sWordToHyphenate.Substring(index, length - index))
    index = length
Next

Dim sHyphenatedString As String = ""

For Each part As String In parts
    sHyphenatedString += part & "-"
Next

richTextBox1.Selection.Text = sHyphenatedString