Skip to main content

Hyphenation

  • 3 minutes to read

RichEditDocumentServer can enable or suppress word hyphenation at line breaks.

When you load a document, the following scenarios are possible:

  • If the document contains soft hyphens, RichEditDocumentServer preserves them.
  • If the document has automatic hyphenation enabled, the Word Processing Document API uses the currently linked hyphenation dictionaries to hyphenate the text. Note that the document layout may change.

Add a Hyphenation Dictionary

Link a dictionary that specifies hyphenation rules. The RichEditDocumentServer.HyphenationDictionaries property provides access to the dictionary collection. You can use two dictionary types:

  1. OpenOfficeHyphenationDictionary

    The OpenOffice hyphenation dictionary is a .dic file containing hyphenation patterns with specific encoding.

    The file can contain the following information:

    • LEFTHYPHENMIN - The minimum number of characters that must appear before the first hyphen.
    • RIGHTHYPHENMIN - The minimum number of characters that must appear after the last hyphen.

    Use the LeftHyphenMin and RightHyphenMin properties to change these parameters.

    You can download dictionaries from the OpenOffice Extensions Page. Note that the OpenOffice license cannot be used for commercial projects.

  2. CustomHyphenationDictionary

    A file with hyphenation patterns. It can contain a list of words divided into syllables (e.g., hy-phe-na-tion).

    The code sample below shows how to add hyphenation dictionaries:

    View Example: Enable Automatic Hyphenation

    //Load embedded dictionaries
    var openOfficePatternStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("ConsoleApp1.hyphen.dic");
    var customDictionaryStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("ConsoleApp1.hyphen_exc.dic");
    
    //Create dictionary objects
    CustomHyphenationDictionary exceptionsDictionary = new CustomHyphenationDictionary(customDictionaryStream, new System.Globalization.CultureInfo("EN-US"));
    OpenOfficeHyphenationDictionary hyphenationDictionary = new OpenOfficeHyphenationDictionary(openOfficePatternStream, new System.Globalization.CultureInfo("EN-US"));
    
    //Add them to the word processor's collection
    wordProcessor.HyphenationDictionaries.Add(exceptionsDictionary);
    wordProcessor.HyphenationDictionaries.Add(hyphenationDictionary);
    

Dictionary Culture

The RichEditDocumentServer uses dictionaries that match the document’s culture. You can specify the dictionary’s culture in the object constructor. If the culture is not specified, the dictionary’s culture is set to the machine’s current culture.

Hyphenate Text

Use the following properties to hyphenate document text:

Property Description
Document.Hyphenation Specifies whether to hyphenate text automatically. If dictionaries are not provided, the property has no effect.
Document.HyphenateCaps Gets or sets whether to hyphenate words in CAPS.
Paragraph.SuppressHyphenation Specifies whether to limit hyphenation to a specific paragraph.

The code sample below shows how to enable hyphenation and export a document to the PDF format.

//Load a document
wordProcessor.LoadDocument("Grimm.docx");

//Specify hyphenation settings
wordProcessor.Document.Hyphenation = true;
wordProcessor.Document.HyphenateCaps = true;

//Export the result to the PDF format
wordProcessor.ExportToPdf("Result.pdf");

//Open the result
Process.Start(new ProcessStartInfo("Result.pdf") { UseShellExecute = true });