Hyphenation
内容
[
隐藏
]Hyphenation class
提供使用连字符词典的方法。这些词典规定了特定语言的单词在何处可以使用连字符。
要了解更多信息,请访问使用连字符文档文章。
public static class Hyphenation
特性
姓名 | 描述 |
---|---|
static Callback { get; set; } | 获取或设置在构建文档页面布局时用于请求词典的回调接口。 这允许延迟加载词典,这在处理多种语言的文档时可能很有用。 |
static WarningCallback { get; set; } | 在加载连字符模式期间调用,当检测到可能导致格式保真度损失的问题时。 |
方法
姓名 | 描述 |
---|---|
static IsDictionaryRegistered(string) | 返回错误的 如果对于指定的语言没有注册词典,或者注册的词典为空,真的 否则。 |
static RegisterDictionary(string, Stream) | 从流中注册并加载指定语言的连字词典。如果词典无法读取或格式无效,则抛出异常。 |
static RegisterDictionary(string, string) | 从文件注册并加载指定语言的连字词典。如果词典无法读取或格式无效,则抛出异常。 |
static UnregisterDictionary(string) | 取消注册指定语言的连字符词典。 |
例子
展示如何从文件打开和注册字典。
public void RegisterDictionary()
{
// 设置一个回调来跟踪连字字典注册期间发生的警告。
WarningInfoCollection warningInfoCollection = new WarningInfoCollection();
Hyphenation.WarningCallback = warningInfoCollection;
// 通过流注册英语(美国)连字符词典。
Stream dictionaryStream = new FileStream(MyDir + "hyph_en_US.dic", FileMode.Open);
Hyphenation.RegisterDictionary("en-US", dictionaryStream);
Assert.AreEqual(0, warningInfoCollection.Count);
// 打开一个 Microsoft Word 可能无法在英语机器上使用连字符的语言环境的文档,例如德语。
Document doc = new Document(MyDir + "German text.docx");
// 为了在保存时对该文档进行连字,我们需要一个“de-CH”语言代码的连字词典。
// 此回调将处理该字典的自动请求。
Hyphenation.Callback = new CustomHyphenationDictionaryRegister();
// 当我们保存文档时,德语连字符将会生效。
doc.Save(ArtifactsDir + "Hyphenation.RegisterDictionary.pdf");
// 此字典包含两个相同的模式,这将触发警告。
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>
/// 将 ISO 语言代码与连字词典文件的本地系统文件名关联。
/// </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;
}
也可以看看
- 命名空间 Aspose.Words
- 部件 Aspose.Words