search/mag_sel search/close
Aspose::Words::Hyphenation Class Reference

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

Examples

Shows how to open and register a dictionary from a file.

{
// Set up a callback that tracks warnings that occur during hyphenation dictionary registration.
auto warningInfoCollection = MakeObject<WarningInfoCollection>();
Hyphenation::set_WarningCallback(warningInfoCollection);
// Register an English (US) hyphenation dictionary by stream.
SharedPtr<System::IO::Stream> dictionaryStream = MakeObject<System::IO::FileStream>(MyDir + u"hyph_en_US.dic", System::IO::FileMode::Open);
Hyphenation::RegisterDictionary(u"en-US", dictionaryStream);
ASSERT_EQ(0, warningInfoCollection->get_Count());
// Open a document with a locale that Microsoft Word may not hyphenate on an English machine, such as German.
auto doc = MakeObject<Document>(MyDir + u"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::set_Callback(MakeObject<ExHyphenation::CustomHyphenationDictionaryRegister>());
// When we save the document, German hyphenation will take effect.
doc->Save(ArtifactsDir + u"Hyphenation.RegisterDictionary.pdf");
// This dictionary contains two identical patterns, which will trigger a warning.
ASSERT_EQ(1, warningInfoCollection->get_Count());
ASSERT_EQ(WarningType::MinorFormattingLoss, warningInfoCollection->idx_get(0)->get_WarningType());
ASSERT_EQ(WarningSource::Layout, warningInfoCollection->idx_get(0)->get_Source());
ASSERT_EQ(String(u"Hyphenation dictionary contains duplicate patterns. The only first found pattern will be used. ") +
u"Content can be wrapped differently.",
warningInfoCollection->idx_get(0)->get_Description());
}
class CustomHyphenationDictionaryRegister : public IHyphenationCallback
{
public:
CustomHyphenationDictionaryRegister()
{
mHyphenationDictionaryFiles = MakeObject<System::Collections::Generic::Dictionary<String, String>>();
mHyphenationDictionaryFiles->Add(u"en-US", MyDir + u"hyph_en_US.dic");
mHyphenationDictionaryFiles->Add(u"de-CH", MyDir + u"hyph_de_CH.dic");
}
void RequestDictionary(String language) override
{
std::cout << (String(u"Hyphenation dictionary requested: ") + language);
{
std::cout << ", is already registered." << std::endl;
return;
}
if (mHyphenationDictionaryFiles->ContainsKey(language))
{
Hyphenation::RegisterDictionary(language, mHyphenationDictionaryFiles->idx_get(language));
std::cout << ", successfully registered." << std::endl;
return;
}
std::cout << ", no respective dictionary file known by this Callback." << std::endl;
}
private:
SharedPtr<System::Collections::Generic::Dictionary<String, String>> mHyphenationDictionaryFiles;
};

#include <Aspose.Words.Cpp/Hyphenation.h>

Public Member Functions

 Hyphenation ()=delete
 

Static Public Member Functions

static SharedPtr< IHyphenationCallbackget_Callback ()
 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. More...
 
static SharedPtr< IWarningCallbackget_WarningCallback ()
 Called during a load hyphenation patterns, when an issue is detected that might result in formatting fidelity loss. More...
 
static bool IsDictionaryRegistered (String language)
 Returns False if for the specified language there is no dictionary registered or if registered is Null dictionary, True otherwise. More...
 
static void RegisterDictionary (String language, SharedPtr< Stream > stream)
 Registers and loads a hyphenation dictionary for the specified language from a stream. Throws if dictionary cannot be read or has invalid format. More...
 
static void RegisterDictionary (String language, 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. More...
 
static void set_Callback (SharedPtr< IHyphenationCallback > value)
 Setter for get_Callback. More...
 
static void set_WarningCallback (SharedPtr< IWarningCallback > value)
 Setter for get_WarningCallback. More...
 
static void UnregisterDictionary (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. More...
 

Constructor & Destructor Documentation

◆ Hyphenation()

Aspose::Words::Hyphenation::Hyphenation ( )
delete

Member Function Documentation

◆ get_Callback()

static System::SharedPtr<Aspose::Words::IHyphenationCallback> Aspose::Words::Hyphenation::get_Callback ( )
static

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.

Examples

Shows how to open and register a dictionary from a file.

{
// Set up a callback that tracks warnings that occur during hyphenation dictionary registration.
auto warningInfoCollection = MakeObject<WarningInfoCollection>();
Hyphenation::set_WarningCallback(warningInfoCollection);
// Register an English (US) hyphenation dictionary by stream.
SharedPtr<System::IO::Stream> dictionaryStream = MakeObject<System::IO::FileStream>(MyDir + u"hyph_en_US.dic", System::IO::FileMode::Open);
Hyphenation::RegisterDictionary(u"en-US", dictionaryStream);
ASSERT_EQ(0, warningInfoCollection->get_Count());
// Open a document with a locale that Microsoft Word may not hyphenate on an English machine, such as German.
auto doc = MakeObject<Document>(MyDir + u"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::set_Callback(MakeObject<ExHyphenation::CustomHyphenationDictionaryRegister>());
// When we save the document, German hyphenation will take effect.
doc->Save(ArtifactsDir + u"Hyphenation.RegisterDictionary.pdf");
// This dictionary contains two identical patterns, which will trigger a warning.
ASSERT_EQ(1, warningInfoCollection->get_Count());
ASSERT_EQ(WarningType::MinorFormattingLoss, warningInfoCollection->idx_get(0)->get_WarningType());
ASSERT_EQ(WarningSource::Layout, warningInfoCollection->idx_get(0)->get_Source());
ASSERT_EQ(String(u"Hyphenation dictionary contains duplicate patterns. The only first found pattern will be used. ") +
u"Content can be wrapped differently.",
warningInfoCollection->idx_get(0)->get_Description());
}
class CustomHyphenationDictionaryRegister : public IHyphenationCallback
{
public:
CustomHyphenationDictionaryRegister()
{
mHyphenationDictionaryFiles = MakeObject<System::Collections::Generic::Dictionary<String, String>>();
mHyphenationDictionaryFiles->Add(u"en-US", MyDir + u"hyph_en_US.dic");
mHyphenationDictionaryFiles->Add(u"de-CH", MyDir + u"hyph_de_CH.dic");
}
void RequestDictionary(String language) override
{
std::cout << (String(u"Hyphenation dictionary requested: ") + language);
{
std::cout << ", is already registered." << std::endl;
return;
}
if (mHyphenationDictionaryFiles->ContainsKey(language))
{
Hyphenation::RegisterDictionary(language, mHyphenationDictionaryFiles->idx_get(language));
std::cout << ", successfully registered." << std::endl;
return;
}
std::cout << ", no respective dictionary file known by this Callback." << std::endl;
}
private:
SharedPtr<System::Collections::Generic::Dictionary<String, String>> mHyphenationDictionaryFiles;
};

◆ get_WarningCallback()

static System::SharedPtr<Aspose::Words::IWarningCallback> Aspose::Words::Hyphenation::get_WarningCallback ( )
static

Called during a load hyphenation patterns, when an issue is detected that might result in formatting fidelity loss.

Examples

Shows how to open and register a dictionary from a file.

{
// Set up a callback that tracks warnings that occur during hyphenation dictionary registration.
auto warningInfoCollection = MakeObject<WarningInfoCollection>();
Hyphenation::set_WarningCallback(warningInfoCollection);
// Register an English (US) hyphenation dictionary by stream.
SharedPtr<System::IO::Stream> dictionaryStream = MakeObject<System::IO::FileStream>(MyDir + u"hyph_en_US.dic", System::IO::FileMode::Open);
Hyphenation::RegisterDictionary(u"en-US", dictionaryStream);
ASSERT_EQ(0, warningInfoCollection->get_Count());
// Open a document with a locale that Microsoft Word may not hyphenate on an English machine, such as German.
auto doc = MakeObject<Document>(MyDir + u"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::set_Callback(MakeObject<ExHyphenation::CustomHyphenationDictionaryRegister>());
// When we save the document, German hyphenation will take effect.
doc->Save(ArtifactsDir + u"Hyphenation.RegisterDictionary.pdf");
// This dictionary contains two identical patterns, which will trigger a warning.
ASSERT_EQ(1, warningInfoCollection->get_Count());
ASSERT_EQ(WarningType::MinorFormattingLoss, warningInfoCollection->idx_get(0)->get_WarningType());
ASSERT_EQ(WarningSource::Layout, warningInfoCollection->idx_get(0)->get_Source());
ASSERT_EQ(String(u"Hyphenation dictionary contains duplicate patterns. The only first found pattern will be used. ") +
u"Content can be wrapped differently.",
warningInfoCollection->idx_get(0)->get_Description());
}
class CustomHyphenationDictionaryRegister : public IHyphenationCallback
{
public:
CustomHyphenationDictionaryRegister()
{
mHyphenationDictionaryFiles = MakeObject<System::Collections::Generic::Dictionary<String, String>>();
mHyphenationDictionaryFiles->Add(u"en-US", MyDir + u"hyph_en_US.dic");
mHyphenationDictionaryFiles->Add(u"de-CH", MyDir + u"hyph_de_CH.dic");
}
void RequestDictionary(String language) override
{
std::cout << (String(u"Hyphenation dictionary requested: ") + language);
{
std::cout << ", is already registered." << std::endl;
return;
}
if (mHyphenationDictionaryFiles->ContainsKey(language))
{
Hyphenation::RegisterDictionary(language, mHyphenationDictionaryFiles->idx_get(language));
std::cout << ", successfully registered." << std::endl;
return;
}
std::cout << ", no respective dictionary file known by this Callback." << std::endl;
}
private:
SharedPtr<System::Collections::Generic::Dictionary<String, String>> mHyphenationDictionaryFiles;
};

◆ IsDictionaryRegistered()

static bool Aspose::Words::Hyphenation::IsDictionaryRegistered ( System::String  language)
static

Returns False if for the specified language there is no dictionary registered or if registered is Null dictionary, True otherwise.

Examples

Shows how to register a hyphenation dictionary.

// A hyphenation dictionary contains a list of strings that define hyphenation rules for the dictionary's language.
// When a document contains lines of text in which a word could be split up and continued on the next line,
// hyphenation will look through the dictionary's list of strings for that word's substrings.
// If the dictionary contains a substring, then hyphenation will split the word across two lines
// by the substring and add a hyphen to the first half.
// Register a dictionary file from the local file system to the "de-CH" locale.
Hyphenation::RegisterDictionary(u"de-CH", MyDir + u"hyph_de_CH.dic");
ASSERT_TRUE(Hyphenation::IsDictionaryRegistered(u"de-CH"));
// Open a document containing text with a locale matching that of our dictionary,
// and save it to a fixed-page save format. The text in that document will be hyphenated.
auto doc = MakeObject<Document>(MyDir + u"German text.docx");
ASSERT_TRUE(doc->get_FirstSection()->get_Body()->get_FirstParagraph()->get_Runs()->LINQ_OfType<SharedPtr<Run>>()->LINQ_All(
[](SharedPtr<Run> r) { return r->get_Font()->get_LocaleId() == MakeObject<System::Globalization::CultureInfo>(u"de-CH")->get_LCID(); }));
doc->Save(ArtifactsDir + u"Hyphenation.Dictionary.Registered.pdf");
// Re-load the document after un-registering the dictionary,
// and save it to another PDF, which will not have hyphenated text.
ASSERT_FALSE(Hyphenation::IsDictionaryRegistered(u"de-CH"));
doc = MakeObject<Document>(MyDir + u"German text.docx");
doc->Save(ArtifactsDir + u"Hyphenation.Dictionary.Unregistered.pdf");

◆ RegisterDictionary() [1/2]

static void Aspose::Words::Hyphenation::RegisterDictionary ( System::String  language,
System::SharedPtr< System::IO::Stream stream 
)
static

Registers and loads a hyphenation dictionary for the specified language from a stream. Throws if dictionary cannot be read or has invalid format.

Parameters
languageA language name, e.g. "en-US". See .NET documentation for "culture name" and RFC 4646 for details.
streamA stream for the dictionary file in OpenOffice format.
Examples

Shows how to open and register a dictionary from a file.

{
// Set up a callback that tracks warnings that occur during hyphenation dictionary registration.
auto warningInfoCollection = MakeObject<WarningInfoCollection>();
Hyphenation::set_WarningCallback(warningInfoCollection);
// Register an English (US) hyphenation dictionary by stream.
SharedPtr<System::IO::Stream> dictionaryStream = MakeObject<System::IO::FileStream>(MyDir + u"hyph_en_US.dic", System::IO::FileMode::Open);
Hyphenation::RegisterDictionary(u"en-US", dictionaryStream);
ASSERT_EQ(0, warningInfoCollection->get_Count());
// Open a document with a locale that Microsoft Word may not hyphenate on an English machine, such as German.
auto doc = MakeObject<Document>(MyDir + u"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::set_Callback(MakeObject<ExHyphenation::CustomHyphenationDictionaryRegister>());
// When we save the document, German hyphenation will take effect.
doc->Save(ArtifactsDir + u"Hyphenation.RegisterDictionary.pdf");
// This dictionary contains two identical patterns, which will trigger a warning.
ASSERT_EQ(1, warningInfoCollection->get_Count());
ASSERT_EQ(WarningType::MinorFormattingLoss, warningInfoCollection->idx_get(0)->get_WarningType());
ASSERT_EQ(WarningSource::Layout, warningInfoCollection->idx_get(0)->get_Source());
ASSERT_EQ(String(u"Hyphenation dictionary contains duplicate patterns. The only first found pattern will be used. ") +
u"Content can be wrapped differently.",
warningInfoCollection->idx_get(0)->get_Description());
}
class CustomHyphenationDictionaryRegister : public IHyphenationCallback
{
public:
CustomHyphenationDictionaryRegister()
{
mHyphenationDictionaryFiles = MakeObject<System::Collections::Generic::Dictionary<String, String>>();
mHyphenationDictionaryFiles->Add(u"en-US", MyDir + u"hyph_en_US.dic");
mHyphenationDictionaryFiles->Add(u"de-CH", MyDir + u"hyph_de_CH.dic");
}
void RequestDictionary(String language) override
{
std::cout << (String(u"Hyphenation dictionary requested: ") + language);
{
std::cout << ", is already registered." << std::endl;
return;
}
if (mHyphenationDictionaryFiles->ContainsKey(language))
{
Hyphenation::RegisterDictionary(language, mHyphenationDictionaryFiles->idx_get(language));
std::cout << ", successfully registered." << std::endl;
return;
}
std::cout << ", no respective dictionary file known by this Callback." << std::endl;
}
private:
SharedPtr<System::Collections::Generic::Dictionary<String, String>> mHyphenationDictionaryFiles;
};

◆ RegisterDictionary() [2/2]

static void Aspose::Words::Hyphenation::RegisterDictionary ( System::String  language,
System::String  fileName 
)
static

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
languageA language name, e.g. "en-US". See .NET documentation for "culture name" and RFC 4646 for details.
fileNameA 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() method.
Examples

Shows how to register a hyphenation dictionary.

// A hyphenation dictionary contains a list of strings that define hyphenation rules for the dictionary's language.
// When a document contains lines of text in which a word could be split up and continued on the next line,
// hyphenation will look through the dictionary's list of strings for that word's substrings.
// If the dictionary contains a substring, then hyphenation will split the word across two lines
// by the substring and add a hyphen to the first half.
// Register a dictionary file from the local file system to the "de-CH" locale.
Hyphenation::RegisterDictionary(u"de-CH", MyDir + u"hyph_de_CH.dic");
ASSERT_TRUE(Hyphenation::IsDictionaryRegistered(u"de-CH"));
// Open a document containing text with a locale matching that of our dictionary,
// and save it to a fixed-page save format. The text in that document will be hyphenated.
auto doc = MakeObject<Document>(MyDir + u"German text.docx");
ASSERT_TRUE(doc->get_FirstSection()->get_Body()->get_FirstParagraph()->get_Runs()->LINQ_OfType<SharedPtr<Run>>()->LINQ_All(
[](SharedPtr<Run> r) { return r->get_Font()->get_LocaleId() == MakeObject<System::Globalization::CultureInfo>(u"de-CH")->get_LCID(); }));
doc->Save(ArtifactsDir + u"Hyphenation.Dictionary.Registered.pdf");
// Re-load the document after un-registering the dictionary,
// and save it to another PDF, which will not have hyphenated text.
ASSERT_FALSE(Hyphenation::IsDictionaryRegistered(u"de-CH"));
doc = MakeObject<Document>(MyDir + u"German text.docx");
doc->Save(ArtifactsDir + u"Hyphenation.Dictionary.Unregistered.pdf");

Shows how to open and register a dictionary from a file.

{
// Set up a callback that tracks warnings that occur during hyphenation dictionary registration.
auto warningInfoCollection = MakeObject<WarningInfoCollection>();
Hyphenation::set_WarningCallback(warningInfoCollection);
// Register an English (US) hyphenation dictionary by stream.
SharedPtr<System::IO::Stream> dictionaryStream = MakeObject<System::IO::FileStream>(MyDir + u"hyph_en_US.dic", System::IO::FileMode::Open);
Hyphenation::RegisterDictionary(u"en-US", dictionaryStream);
ASSERT_EQ(0, warningInfoCollection->get_Count());
// Open a document with a locale that Microsoft Word may not hyphenate on an English machine, such as German.
auto doc = MakeObject<Document>(MyDir + u"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::set_Callback(MakeObject<ExHyphenation::CustomHyphenationDictionaryRegister>());
// When we save the document, German hyphenation will take effect.
doc->Save(ArtifactsDir + u"Hyphenation.RegisterDictionary.pdf");
// This dictionary contains two identical patterns, which will trigger a warning.
ASSERT_EQ(1, warningInfoCollection->get_Count());
ASSERT_EQ(WarningType::MinorFormattingLoss, warningInfoCollection->idx_get(0)->get_WarningType());
ASSERT_EQ(WarningSource::Layout, warningInfoCollection->idx_get(0)->get_Source());
ASSERT_EQ(String(u"Hyphenation dictionary contains duplicate patterns. The only first found pattern will be used. ") +
u"Content can be wrapped differently.",
warningInfoCollection->idx_get(0)->get_Description());
}
class CustomHyphenationDictionaryRegister : public IHyphenationCallback
{
public:
CustomHyphenationDictionaryRegister()
{
mHyphenationDictionaryFiles = MakeObject<System::Collections::Generic::Dictionary<String, String>>();
mHyphenationDictionaryFiles->Add(u"en-US", MyDir + u"hyph_en_US.dic");
mHyphenationDictionaryFiles->Add(u"de-CH", MyDir + u"hyph_de_CH.dic");
}
void RequestDictionary(String language) override
{
std::cout << (String(u"Hyphenation dictionary requested: ") + language);
{
std::cout << ", is already registered." << std::endl;
return;
}
if (mHyphenationDictionaryFiles->ContainsKey(language))
{
Hyphenation::RegisterDictionary(language, mHyphenationDictionaryFiles->idx_get(language));
std::cout << ", successfully registered." << std::endl;
return;
}
std::cout << ", no respective dictionary file known by this Callback." << std::endl;
}
private:
SharedPtr<System::Collections::Generic::Dictionary<String, String>> mHyphenationDictionaryFiles;
};

◆ set_Callback()

static void Aspose::Words::Hyphenation::set_Callback ( System::SharedPtr< Aspose::Words::IHyphenationCallback value)
static

◆ set_WarningCallback()

static void Aspose::Words::Hyphenation::set_WarningCallback ( System::SharedPtr< Aspose::Words::IWarningCallback value)
static

◆ UnregisterDictionary()

static void Aspose::Words::Hyphenation::UnregisterDictionary ( System::String  language)
static

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
languageA 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.
Examples

Shows how to register a hyphenation dictionary.

// A hyphenation dictionary contains a list of strings that define hyphenation rules for the dictionary's language.
// When a document contains lines of text in which a word could be split up and continued on the next line,
// hyphenation will look through the dictionary's list of strings for that word's substrings.
// If the dictionary contains a substring, then hyphenation will split the word across two lines
// by the substring and add a hyphen to the first half.
// Register a dictionary file from the local file system to the "de-CH" locale.
Hyphenation::RegisterDictionary(u"de-CH", MyDir + u"hyph_de_CH.dic");
ASSERT_TRUE(Hyphenation::IsDictionaryRegistered(u"de-CH"));
// Open a document containing text with a locale matching that of our dictionary,
// and save it to a fixed-page save format. The text in that document will be hyphenated.
auto doc = MakeObject<Document>(MyDir + u"German text.docx");
ASSERT_TRUE(doc->get_FirstSection()->get_Body()->get_FirstParagraph()->get_Runs()->LINQ_OfType<SharedPtr<Run>>()->LINQ_All(
[](SharedPtr<Run> r) { return r->get_Font()->get_LocaleId() == MakeObject<System::Globalization::CultureInfo>(u"de-CH")->get_LCID(); }));
doc->Save(ArtifactsDir + u"Hyphenation.Dictionary.Registered.pdf");
// Re-load the document after un-registering the dictionary,
// and save it to another PDF, which will not have hyphenated text.
ASSERT_FALSE(Hyphenation::IsDictionaryRegistered(u"de-CH"));
doc = MakeObject<Document>(MyDir + u"German text.docx");
doc->Save(ArtifactsDir + u"Hyphenation.Dictionary.Unregistered.pdf");