Hyphenation Class
Provides methods for working with hyphenation dictionaries. These dictionaries prescribe where words of a specific language can be hyphenated.
Inheritance Hierarchy
SystemObject
  Aspose.WordsHyphenation

Namespace: Aspose.Words
Assembly: Aspose.Words (in Aspose.Words.dll) Version: 19.11
Syntax
public static class Hyphenation

The Hyphenation type exposes the following members.

Properties
  NameDescription
Public propertyStatic memberCode exampleCallback
Gets or sets callback interface used to request dictionaries when page layout of the document is built. This allows delay loading of dictionaries which may be useful when processing documents in many languages.
Public propertyStatic memberCode exampleWarningCallback
Called during a load hyphenation patterns, when an issue is detected that might result in formatting fidelity loss.
Methods
  NameDescription
Public methodStatic memberCode exampleIsDictionaryRegistered
Returns False if for the specified language there is no dictionary registered or if registered is Null dictionary, True otherwise.
Public methodStatic memberCode exampleRegisterDictionary(String, Stream)
Registers and loads a hyphenation dictionary for the specified language from a stream. Throws if dictionary cannot be read or has invalid format.
Public methodStatic memberCode exampleRegisterDictionary(String, String)
Registers and loads a hyphenation dictionary for the specified language from file. Throws if dictionary cannot be read or has invalid format.

This method can also be used to register Null dictionary to prevent Callback from being called repeatedly for the same language.

Public methodStatic memberCode exampleUnregisterDictionary
Unregisters a hyphenation dictionary for the specified language.

This is different from registering Null dictionary. Unregistering a dictionary enables callback for the specified language.

Examples
Shows how to open and register a dictionary from a file.
public void RegisterDictionary()
{
    // Set up a callback that tracks warnings that occur during hyphenation dictionary registration
    WarningInfoCollection warningInfoCollection = new WarningInfoCollection();
    Hyphenation.WarningCallback = warningInfoCollection;

    // Register an English (US) hyphenation dictionary by stream
    Stream dictionaryStream = new FileStream(MyDir + "hyph_en_US.dic", FileMode.Open);
    Hyphenation.RegisterDictionary("en-US", dictionaryStream);

    // No warnings detected
    Assert.AreEqual(0, warningInfoCollection.Count);

    // Open a document with a German locale that might not get automatically hyphenated by Microsoft Word an english machine
    Document doc = new Document(MyDir + "RandomGermanWords.doc");

    // To hyphenate that document upon saving, we need a hyphenation dictionary for the "de-CH" language code
    // This callback will handle the automatic request for that dictionary 
    Hyphenation.Callback = new CustomHyphenationDictionaryRegister();

    // When we save the document, it will be hyphenated according to rules defined by the dictionary known by our callback
    doc.Save(ArtifactsDir + "Hyphenation.RegisterDictionary.pdf");

    // This dictionary contains two identical patterns, which will trigger a warning
    Assert.AreEqual(1, warningInfoCollection.Count);
    Assert.AreEqual(WarningType.MinorFormattingLoss, warningInfoCollection[0].WarningType);
    Assert.AreEqual(WarningSource.Layout, warningInfoCollection[0].Source);
    Assert.AreEqual("Hyphenation dictionary contains duplicate patterns. The only first found pattern will be used. " +
                    "Content can be wrapped differently.", warningInfoCollection[0].Description);
}

/// <summary>
/// Associates ISO language codes with custom local system dictionary files for their respective languages
/// </summary>
private class CustomHyphenationDictionaryRegister : IHyphenationCallback
{
    public CustomHyphenationDictionaryRegister()
    {
        mHyphenationDictionaryFiles = new Dictionary<string, string>
        {
            { "en-US", MyDir + "hyph_en_US.dic" },
            { "de-CH", MyDir + "hyph_de_CH.dic" }
        };
    }

    public void RequestDictionary(string language)
    {
        Console.Write("Hyphenation dictionary requested: " + language);

        if (Hyphenation.IsDictionaryRegistered(language))
        {
            Console.WriteLine(", is already registered.");
            return;
        }

        if (mHyphenationDictionaryFiles.ContainsKey(language))
        {
            Hyphenation.RegisterDictionary(language, mHyphenationDictionaryFiles[language]);
            Console.WriteLine(", successfully registered.");
            return;
        }

        Console.WriteLine(", no respective dictionary file known by this Callback.");
    }

    private readonly Dictionary<string, string> mHyphenationDictionaryFiles;
}
See Also