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:

  • %USERPROFILE%\My Documents\TX Spell 7.0.NET for Windows Forms\Samples\WinForms\CSharp\Hyphenation
  • %USERPROFILE%\My Documents\TX Spell 7.0.NET for Windows Forms\Samples\WinForms\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 TXTextControl.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;
}

foreach (string part in parts)
{
    richTextBox1.SelectionColor = Color.Blue;
    richTextBox1.SelectedText = part;
    richTextBox1.SelectedText = "-";
    richTextBox1.Select(richTextBox1.SelectionStart - 1, 1);
    richTextBox1.SelectionColor = Color.Red;
    richTextBox1.SelectionStart += 1;
}
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

For Each part As String In parts
    RichTextBox1.SelectionColor = Color.Blue
    RichTextBox1.SelectedText = part
    RichTextBox1.SelectedText = "-"
    RichTextBox1.[Select](RichTextBox1.SelectionStart - 1, 1)
    RichTextBox1.SelectionColor = Color.Red
    RichTextBox1.SelectionStart += 1
Next