com.aspose.words

Class Hyphenation

  • java.lang.Object
    • com.aspose.words.Hyphenation
public abstract class Hyphenation 
extends java.lang.Object

Provides methods for working with hyphenation dictionaries. These dictionaries prescribe where words of a specific language can be hyphenated.

Example:

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

    // Register an English (US) hyphenation dictionary by stream
    InputStream dictionaryStream = new FileInputStream(getMyDir() + "hyph_en_US.dic");
    Hyphenation.registerDictionary("en-US", dictionaryStream);

    // No warnings detectedß
    Assert.assertEquals(warningInfoCollection.getCount(), 0);

    // Open a document with a German locale that might not get automatically hyphenated by Microsoft Word on an English machine
    Document doc = new Document(getMyDir() + "German text.docx");

    // 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.setCallback(new CustomHyphenationDictionaryRegister());

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

    // This dictionary contains two identical patterns, which will trigger a warning
    Assert.assertEquals(warningInfoCollection.getCount(), 1);
    Assert.assertEquals(warningInfoCollection.get(0).getWarningType(), WarningType.MINOR_FORMATTING_LOSS);
    Assert.assertEquals(warningInfoCollection.get(0).getSource(), WarningSource.LAYOUT);
    Assert.assertEquals(warningInfoCollection.get(0).getDescription(), "Hyphenation dictionary contains duplicate patterns. " +
            "The only first found pattern will be used. Content can be wrapped differently.");
}

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

    public void requestDictionary(String language) throws Exception {
        System.out.print("Hyphenation dictionary requested: " + language);

        if (Hyphenation.isDictionaryRegistered(language)) {
            System.out.println(", is already registered.");
            return;
        }

        if (mHyphenationDictionaryFiles.containsKey(language)) {
            Hyphenation.registerDictionary(language, mHyphenationDictionaryFiles.get(language));
            System.out.println(", successfully registered.");
            return;
        }

        System.out.println(", no respective dictionary file known by this Callback.");
    }

    private HashMap<String, String> mHyphenationDictionaryFiles;
}

Property Getters/Setters Summary
static IHyphenationCallbackgetCallback()
staticvoid
           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.
static IWarningCallbackgetWarningCallback()
staticvoid
           Called during a load hyphenation patterns, when an issue is detected that might result in formatting fidelity loss.
 
Method Summary
static booleanisDictionaryRegistered(java.lang.String language)
Returns False if for the specified language there is no dictionary registered or if registered is Null dictionary, True otherwise.
static voidregisterDictionary(java.lang.String language, java.io.InputStream stream)
Registers and loads a hyphenation dictionary for the specified language from a stream. Throws if dictionary cannot be read or has invalid format.
static voidregisterDictionary(java.lang.String language, java.lang.String fileName)
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.
static voidunregisterDictionary(java.lang.String language)
Unregisters a hyphenation dictionary for the specified language.

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

    • Property Getters/Setters Detail

      • getCallback/setCallback

        public static IHyphenationCallback getCallback() / public static void setCallback(IHyphenationCallback value)
        
        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.

        Example:

        Shows how to open and register a dictionary from a file.
        public void registerDictionary() throws Exception {
            // Set up a callback that tracks warnings that occur during hyphenation dictionary registration
            WarningInfoCollection warningInfoCollection = new WarningInfoCollection();
            Hyphenation.setWarningCallback(warningInfoCollection);
        
            // Register an English (US) hyphenation dictionary by stream
            InputStream dictionaryStream = new FileInputStream(getMyDir() + "hyph_en_US.dic");
            Hyphenation.registerDictionary("en-US", dictionaryStream);
        
            // No warnings detectedß
            Assert.assertEquals(warningInfoCollection.getCount(), 0);
        
            // Open a document with a German locale that might not get automatically hyphenated by Microsoft Word on an English machine
            Document doc = new Document(getMyDir() + "German text.docx");
        
            // 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.setCallback(new CustomHyphenationDictionaryRegister());
        
            // When we save the document, it will be hyphenated according to rules defined by the dictionary known by our callback
            doc.save(getArtifactsDir() + "Hyphenation.RegisterDictionary.pdf");
        
            // This dictionary contains two identical patterns, which will trigger a warning
            Assert.assertEquals(warningInfoCollection.getCount(), 1);
            Assert.assertEquals(warningInfoCollection.get(0).getWarningType(), WarningType.MINOR_FORMATTING_LOSS);
            Assert.assertEquals(warningInfoCollection.get(0).getSource(), WarningSource.LAYOUT);
            Assert.assertEquals(warningInfoCollection.get(0).getDescription(), "Hyphenation dictionary contains duplicate patterns. " +
                    "The only first found pattern will be used. Content can be wrapped differently.");
        }
        
        /// <summary>
        /// Associates ISO language codes with custom local system dictionary files for their respective languages.
        /// </summary>
        private static class CustomHyphenationDictionaryRegister implements IHyphenationCallback {
            public CustomHyphenationDictionaryRegister() {
                mHyphenationDictionaryFiles = new HashMap<>();
                {
                    mHyphenationDictionaryFiles.put("en-US", getMyDir() + "hyph_en_US.dic");
                    mHyphenationDictionaryFiles.put("de-CH", getMyDir() + "hyph_de_CH.dic");
                }
            }
        
            public void requestDictionary(String language) throws Exception {
                System.out.print("Hyphenation dictionary requested: " + language);
        
                if (Hyphenation.isDictionaryRegistered(language)) {
                    System.out.println(", is already registered.");
                    return;
                }
        
                if (mHyphenationDictionaryFiles.containsKey(language)) {
                    Hyphenation.registerDictionary(language, mHyphenationDictionaryFiles.get(language));
                    System.out.println(", successfully registered.");
                    return;
                }
        
                System.out.println(", no respective dictionary file known by this Callback.");
            }
        
            private HashMap<String, String> mHyphenationDictionaryFiles;
        }
      • getWarningCallback/setWarningCallback

        public static IWarningCallback getWarningCallback() / public static void setWarningCallback(IWarningCallback value)
        
        Called during a load hyphenation patterns, when an issue is detected that might result in formatting fidelity loss.

        Example:

        Shows how to open and register a dictionary from a file.
        public void registerDictionary() throws Exception {
            // Set up a callback that tracks warnings that occur during hyphenation dictionary registration
            WarningInfoCollection warningInfoCollection = new WarningInfoCollection();
            Hyphenation.setWarningCallback(warningInfoCollection);
        
            // Register an English (US) hyphenation dictionary by stream
            InputStream dictionaryStream = new FileInputStream(getMyDir() + "hyph_en_US.dic");
            Hyphenation.registerDictionary("en-US", dictionaryStream);
        
            // No warnings detectedß
            Assert.assertEquals(warningInfoCollection.getCount(), 0);
        
            // Open a document with a German locale that might not get automatically hyphenated by Microsoft Word on an English machine
            Document doc = new Document(getMyDir() + "German text.docx");
        
            // 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.setCallback(new CustomHyphenationDictionaryRegister());
        
            // When we save the document, it will be hyphenated according to rules defined by the dictionary known by our callback
            doc.save(getArtifactsDir() + "Hyphenation.RegisterDictionary.pdf");
        
            // This dictionary contains two identical patterns, which will trigger a warning
            Assert.assertEquals(warningInfoCollection.getCount(), 1);
            Assert.assertEquals(warningInfoCollection.get(0).getWarningType(), WarningType.MINOR_FORMATTING_LOSS);
            Assert.assertEquals(warningInfoCollection.get(0).getSource(), WarningSource.LAYOUT);
            Assert.assertEquals(warningInfoCollection.get(0).getDescription(), "Hyphenation dictionary contains duplicate patterns. " +
                    "The only first found pattern will be used. Content can be wrapped differently.");
        }
        
        /// <summary>
        /// Associates ISO language codes with custom local system dictionary files for their respective languages.
        /// </summary>
        private static class CustomHyphenationDictionaryRegister implements IHyphenationCallback {
            public CustomHyphenationDictionaryRegister() {
                mHyphenationDictionaryFiles = new HashMap<>();
                {
                    mHyphenationDictionaryFiles.put("en-US", getMyDir() + "hyph_en_US.dic");
                    mHyphenationDictionaryFiles.put("de-CH", getMyDir() + "hyph_de_CH.dic");
                }
            }
        
            public void requestDictionary(String language) throws Exception {
                System.out.print("Hyphenation dictionary requested: " + language);
        
                if (Hyphenation.isDictionaryRegistered(language)) {
                    System.out.println(", is already registered.");
                    return;
                }
        
                if (mHyphenationDictionaryFiles.containsKey(language)) {
                    Hyphenation.registerDictionary(language, mHyphenationDictionaryFiles.get(language));
                    System.out.println(", successfully registered.");
                    return;
                }
        
                System.out.println(", no respective dictionary file known by this Callback.");
            }
        
            private HashMap<String, String> mHyphenationDictionaryFiles;
        }
    • Method Detail

      • isDictionaryRegistered

        public static boolean isDictionaryRegistered(java.lang.String language)
        Returns False if for the specified language there is no dictionary registered or if registered is Null dictionary, True otherwise.

        Example:

        Shows how to perform and verify hyphenation dictionary registration.
        // Register a dictionary file from the local file system to the "de-CH" locale
        Hyphenation.registerDictionary("de-CH", getMyDir() + "hyph_de_CH.dic");
        
        // This method can be used to verify that a language has a matching registered hyphenation dictionary
        Assert.assertTrue(Hyphenation.isDictionaryRegistered("de-CH"));
        
        // The dictionary file contains a long list of words in a specified language, and in this case it is German
        // These words define a set of rules for hyphenating text (splitting words across lines)
        // If we open a document with text of a language matching that of a registered dictionary,
        // that dictionary's hyphenation rules will be applied and visible upon saving
        Document doc = new Document(getMyDir() + "German text.docx");
        doc.save(getArtifactsDir() + "Hyphenation.Dictionary.Registered.pdf");
        
        // We can also un-register a dictionary to disable these effects on any documents opened after the operation
        Hyphenation.unregisterDictionary("de-CH");
        
        Assert.assertFalse(Hyphenation.isDictionaryRegistered("de-CH"));
        
        doc = new Document(getMyDir() + "German text.docx");
        doc.save(getArtifactsDir() + "Hyphenation.Dictionary.Unregistered.pdf");
      • registerDictionary

        public static void registerDictionary(java.lang.String language, java.io.InputStream stream)
                                           throws java.lang.Exception
        Registers and loads a hyphenation dictionary for the specified language from a stream. Throws if dictionary cannot be read or has invalid format.
        Parameters:
        language - A language name, e.g. "en-US". See .NET documentation for "culture name" and RFC 4646 for details.
        stream - A stream for the dictionary file in OpenOffice format.

        Example:

        Shows how to open and register a dictionary from a file.
        public void registerDictionary() throws Exception {
            // Set up a callback that tracks warnings that occur during hyphenation dictionary registration
            WarningInfoCollection warningInfoCollection = new WarningInfoCollection();
            Hyphenation.setWarningCallback(warningInfoCollection);
        
            // Register an English (US) hyphenation dictionary by stream
            InputStream dictionaryStream = new FileInputStream(getMyDir() + "hyph_en_US.dic");
            Hyphenation.registerDictionary("en-US", dictionaryStream);
        
            // No warnings detectedß
            Assert.assertEquals(warningInfoCollection.getCount(), 0);
        
            // Open a document with a German locale that might not get automatically hyphenated by Microsoft Word on an English machine
            Document doc = new Document(getMyDir() + "German text.docx");
        
            // 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.setCallback(new CustomHyphenationDictionaryRegister());
        
            // When we save the document, it will be hyphenated according to rules defined by the dictionary known by our callback
            doc.save(getArtifactsDir() + "Hyphenation.RegisterDictionary.pdf");
        
            // This dictionary contains two identical patterns, which will trigger a warning
            Assert.assertEquals(warningInfoCollection.getCount(), 1);
            Assert.assertEquals(warningInfoCollection.get(0).getWarningType(), WarningType.MINOR_FORMATTING_LOSS);
            Assert.assertEquals(warningInfoCollection.get(0).getSource(), WarningSource.LAYOUT);
            Assert.assertEquals(warningInfoCollection.get(0).getDescription(), "Hyphenation dictionary contains duplicate patterns. " +
                    "The only first found pattern will be used. Content can be wrapped differently.");
        }
        
        /// <summary>
        /// Associates ISO language codes with custom local system dictionary files for their respective languages.
        /// </summary>
        private static class CustomHyphenationDictionaryRegister implements IHyphenationCallback {
            public CustomHyphenationDictionaryRegister() {
                mHyphenationDictionaryFiles = new HashMap<>();
                {
                    mHyphenationDictionaryFiles.put("en-US", getMyDir() + "hyph_en_US.dic");
                    mHyphenationDictionaryFiles.put("de-CH", getMyDir() + "hyph_de_CH.dic");
                }
            }
        
            public void requestDictionary(String language) throws Exception {
                System.out.print("Hyphenation dictionary requested: " + language);
        
                if (Hyphenation.isDictionaryRegistered(language)) {
                    System.out.println(", is already registered.");
                    return;
                }
        
                if (mHyphenationDictionaryFiles.containsKey(language)) {
                    Hyphenation.registerDictionary(language, mHyphenationDictionaryFiles.get(language));
                    System.out.println(", successfully registered.");
                    return;
                }
        
                System.out.println(", no respective dictionary file known by this Callback.");
            }
        
            private HashMap<String, String> mHyphenationDictionaryFiles;
        }
      • registerDictionary

        public static void registerDictionary(java.lang.String language, java.lang.String fileName)
                                           throws java.lang.Exception
        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.
        Parameters:
        language - A language name, e.g. "en-US". See .NET documentation for "culture name" and RFC 4646 for details.
        fileName - A path to the dictionary file in Open Office format.

        If this parameter is null or empty string then registered is Null dictionary and callback is not called anymore for this language.

        To enable callback again use unregisterDictionary(java.lang.String) method.

        Example:

        Shows how to open and register a dictionary from a file.
        public void registerDictionary() throws Exception {
            // Set up a callback that tracks warnings that occur during hyphenation dictionary registration
            WarningInfoCollection warningInfoCollection = new WarningInfoCollection();
            Hyphenation.setWarningCallback(warningInfoCollection);
        
            // Register an English (US) hyphenation dictionary by stream
            InputStream dictionaryStream = new FileInputStream(getMyDir() + "hyph_en_US.dic");
            Hyphenation.registerDictionary("en-US", dictionaryStream);
        
            // No warnings detectedß
            Assert.assertEquals(warningInfoCollection.getCount(), 0);
        
            // Open a document with a German locale that might not get automatically hyphenated by Microsoft Word on an English machine
            Document doc = new Document(getMyDir() + "German text.docx");
        
            // 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.setCallback(new CustomHyphenationDictionaryRegister());
        
            // When we save the document, it will be hyphenated according to rules defined by the dictionary known by our callback
            doc.save(getArtifactsDir() + "Hyphenation.RegisterDictionary.pdf");
        
            // This dictionary contains two identical patterns, which will trigger a warning
            Assert.assertEquals(warningInfoCollection.getCount(), 1);
            Assert.assertEquals(warningInfoCollection.get(0).getWarningType(), WarningType.MINOR_FORMATTING_LOSS);
            Assert.assertEquals(warningInfoCollection.get(0).getSource(), WarningSource.LAYOUT);
            Assert.assertEquals(warningInfoCollection.get(0).getDescription(), "Hyphenation dictionary contains duplicate patterns. " +
                    "The only first found pattern will be used. Content can be wrapped differently.");
        }
        
        /// <summary>
        /// Associates ISO language codes with custom local system dictionary files for their respective languages.
        /// </summary>
        private static class CustomHyphenationDictionaryRegister implements IHyphenationCallback {
            public CustomHyphenationDictionaryRegister() {
                mHyphenationDictionaryFiles = new HashMap<>();
                {
                    mHyphenationDictionaryFiles.put("en-US", getMyDir() + "hyph_en_US.dic");
                    mHyphenationDictionaryFiles.put("de-CH", getMyDir() + "hyph_de_CH.dic");
                }
            }
        
            public void requestDictionary(String language) throws Exception {
                System.out.print("Hyphenation dictionary requested: " + language);
        
                if (Hyphenation.isDictionaryRegistered(language)) {
                    System.out.println(", is already registered.");
                    return;
                }
        
                if (mHyphenationDictionaryFiles.containsKey(language)) {
                    Hyphenation.registerDictionary(language, mHyphenationDictionaryFiles.get(language));
                    System.out.println(", successfully registered.");
                    return;
                }
        
                System.out.println(", no respective dictionary file known by this Callback.");
            }
        
            private HashMap<String, String> mHyphenationDictionaryFiles;
        }

        Example:

        Shows how to perform and verify hyphenation dictionary registration.
        // Register a dictionary file from the local file system to the "de-CH" locale
        Hyphenation.registerDictionary("de-CH", getMyDir() + "hyph_de_CH.dic");
        
        // This method can be used to verify that a language has a matching registered hyphenation dictionary
        Assert.assertTrue(Hyphenation.isDictionaryRegistered("de-CH"));
        
        // The dictionary file contains a long list of words in a specified language, and in this case it is German
        // These words define a set of rules for hyphenating text (splitting words across lines)
        // If we open a document with text of a language matching that of a registered dictionary,
        // that dictionary's hyphenation rules will be applied and visible upon saving
        Document doc = new Document(getMyDir() + "German text.docx");
        doc.save(getArtifactsDir() + "Hyphenation.Dictionary.Registered.pdf");
        
        // We can also un-register a dictionary to disable these effects on any documents opened after the operation
        Hyphenation.unregisterDictionary("de-CH");
        
        Assert.assertFalse(Hyphenation.isDictionaryRegistered("de-CH"));
        
        doc = new Document(getMyDir() + "German text.docx");
        doc.save(getArtifactsDir() + "Hyphenation.Dictionary.Unregistered.pdf");
      • unregisterDictionary

        public static void unregisterDictionary(java.lang.String language)
        Unregisters a hyphenation dictionary for the specified language.

        This is different from registering Null dictionary. Unregistering a dictionary enables callback for the specified language.
        Parameters:
        language - A language name, e.g. "en-US". See .NET documentation for "culture name" and RFC 4646 for details.

        If null or empty string then all dictionaries are unregistered.

        Example:

        Shows how to perform and verify hyphenation dictionary registration.
        // Register a dictionary file from the local file system to the "de-CH" locale
        Hyphenation.registerDictionary("de-CH", getMyDir() + "hyph_de_CH.dic");
        
        // This method can be used to verify that a language has a matching registered hyphenation dictionary
        Assert.assertTrue(Hyphenation.isDictionaryRegistered("de-CH"));
        
        // The dictionary file contains a long list of words in a specified language, and in this case it is German
        // These words define a set of rules for hyphenating text (splitting words across lines)
        // If we open a document with text of a language matching that of a registered dictionary,
        // that dictionary's hyphenation rules will be applied and visible upon saving
        Document doc = new Document(getMyDir() + "German text.docx");
        doc.save(getArtifactsDir() + "Hyphenation.Dictionary.Registered.pdf");
        
        // We can also un-register a dictionary to disable these effects on any documents opened after the operation
        Hyphenation.unregisterDictionary("de-CH");
        
        Assert.assertFalse(Hyphenation.isDictionaryRegistered("de-CH"));
        
        doc = new Document(getMyDir() + "German text.docx");
        doc.save(getArtifactsDir() + "Hyphenation.Dictionary.Unregistered.pdf");