指定加载选项
加载文档时,您可以设置一些高级属性。 Aspose.Words 为您提供了 LoadOptions 类,它允许更精确地控制加载过程。某些加载格式具有相应的类,用于保存该加载格式的加载选项,例如,有 PdfLoadOptions 用于加载到 PDF 格式或 TxtLoadOptions 用于加载到 TXT。本文提供了使用 LoadOptions 类选项的示例。
设置Microsoft Word版本以更改外观
不同版本的Microsoft Word应用程序可以以不同的方式显示文档。例如,使用 WPS Office 生成的 OOXML 文档(例如 DOCX 或 DOTX)存在一个众所周知的问题。在这种情况下,重要的文档标记元素可能会丢失,或者可能会被不同地解释,从而导致 Microsoft Word 2019 与 Microsoft Word 2010 相比以不同的方式显示此类文档。
默认情况下,Aspose.Words 使用 Microsoft Word 2019 规则打开文档。如果您需要使文档加载看起来像在以前的 Microsoft Word 应用程序版本之一中那样,则应使用 LoadOptions 类的 MswVersion 属性显式指定所需的版本。
以下代码示例显示如何使用加载选项设置 Microsoft Word 版本:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
// Create a new LoadOptions object, which will load documents according to MS Word 2019 specification by default | |
// and change the loading version to Microsoft Word 2010. | |
LoadOptions loadOptions = new LoadOptions { MswVersion = MsWordVersion.Word2010 }; | |
Document doc = new Document(MyDir + "Document.docx", loadOptions); | |
doc.Save(ArtifactsDir + "WorkingWithLoadOptions.SetMsWordVersion.docx"); |
设置语言首选项以更改外观
在 Microsoft Word 中显示文档的详细信息不仅取决于应用程序版本和 MswVersion 属性值,还取决于语言设置。 Microsoft Word 可能会根据"Office 语言首选项"对话框设置以不同方式显示文档,该对话框设置可在"文件 → 选项 → 语言"中找到。使用此对话框,用户可以选择例如主要语言、校对语言、显示语言等。 Aspose.Words 提供 LanguagePreferences 属性作为此对话框的等效属性。如果 Aspose.Words 输出与 Microsoft Word 输出不同,请为 EditingLanguage 设置适当的值 – 这可以改进输出文档。
以下代码示例显示如何将日语设置为 EditingLanguage:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
LoadOptions loadOptions = new LoadOptions(); | |
// Set language preferences that will be used when document is loading. | |
loadOptions.LanguagePreferences.AddEditingLanguage(EditingLanguage.Japanese); | |
Document doc = new Document(MyDir + "No default editing language.docx", loadOptions); | |
int localeIdFarEast = doc.Styles.DefaultFont.LocaleIdFarEast; | |
Console.WriteLine( | |
localeIdFarEast == (int)EditingLanguage.Japanese | |
? "The document either has no any FarEast language set in defaults or it was set to Japanese originally." | |
: "The document default FarEast language was set to another than Japanese language originally, so it is not overridden."); |
使用 WarningCallback 控制加载文档时的问题
某些文档可能已损坏、包含无效条目或具有 Aspose.Words 目前不支持的功能。如果您想了解加载文档时出现的问题,Aspose.Words 提供了 IWarningCallback 接口。
以下代码示例显示了 IWarningCallback 接口的实现:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
public class DocumentLoadingWarningCallback : IWarningCallback | |
{ | |
public void Warning(WarningInfo info) | |
{ | |
// Prints warnings and their details as they arise during document loading. | |
Console.WriteLine($"WARNING: {info.WarningType}, source: {info.Source}"); | |
Console.WriteLine($"\tDescription: {info.Description}"); | |
} | |
} |
要获取有关整个加载期间所有问题的信息,请使用 WarningCallback 属性。
以下代码示例展示了如何使用此属性:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
LoadOptions loadOptions = new LoadOptions { WarningCallback = new DocumentLoadingWarningCallback() }; | |
Document doc = new Document(MyDir + "Document.docx", loadOptions); |
使用ResourceLoadingCallback控制外部资源加载
文档可能包含指向位于本地磁盘、网络或 Internet 上某处的图像的外部链接。 Aspose.Words 自动将此类图像加载到文档中,但在某些情况下需要控制此过程。例如,决定我们是否真的需要加载某个图像或者跳过它。 ResourceLoadingCallback 加载选项允许您对此进行控制。
以下代码示例显示了 IResourceLoadingCallback 接口的实现:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
private class HtmlLinkedResourceLoadingCallback : IResourceLoadingCallback | |
{ | |
public ResourceLoadingAction ResourceLoading(ResourceLoadingArgs args) | |
{ | |
switch (args.ResourceType) | |
{ | |
case ResourceType.CssStyleSheet: | |
{ | |
Console.WriteLine($"External CSS Stylesheet found upon loading: {args.OriginalUri}"); | |
// CSS file will don't used in the document. | |
return ResourceLoadingAction.Skip; | |
} | |
case ResourceType.Image: | |
{ | |
// Replaces all images with a substitute. | |
Image newImage = Image.FromFile(ImagesDir + "Logo.jpg"); | |
ImageConverter converter = new ImageConverter(); | |
byte[] imageBytes = (byte[])converter.ConvertTo(newImage, typeof(byte[])); | |
args.SetData(imageBytes); | |
// New images will be used instead of presented in the document. | |
return ResourceLoadingAction.UserProvided; | |
} | |
case ResourceType.Document: | |
{ | |
Console.WriteLine($"External document found upon loading: {args.OriginalUri}"); | |
// Will be used as usual. | |
return ResourceLoadingAction.Default; | |
} | |
default: | |
throw new InvalidOperationException("Unexpected ResourceType value."); | |
} | |
} | |
} |
以下代码示例显示如何使用 ResourceLoadingCallback 属性:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
LoadOptions loadOptions = new LoadOptions { ResourceLoadingCallback = new HtmlLinkedResourceLoadingCallback() }; | |
// When we open an Html document, external resources such as references to CSS stylesheet files | |
// and external images will be handled customarily by the loading callback as the document is loaded. | |
Document doc = new Document(MyDir + "Images.html", loadOptions); | |
doc.Save(ArtifactsDir + "WorkingWithLoadOptions.ResourceLoadingCallback.pdf"); |
使用 TempFolder 避免内存异常
Aspose.Words 支持包含数千页且内容丰富的超大文档。加载此类文档可能需要大量 RAM。在加载过程中,Aspose.Words 需要更多内存来保存用于解析文档的临时结构。
如果在加载文档时遇到内存不足异常问题,请尝试使用 TempFolder 属性。在这种情况下,Aspose.Words会将一些数据存储在临时文件中而不是内存中,这可以帮助避免此类异常。
以下代码示例展示了如何设置 TempFolder:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
LoadOptions loadOptions = new LoadOptions { TempFolder = ArtifactsDir }; | |
Document doc = new Document(MyDir + "Document.docx", loadOptions); |
显式设置编码
大多数现代文档格式以 Unicode 存储其内容,不需要特殊处理。另一方面,仍然有许多文档使用某些 Unicode 之前的编码,有时会丢失编码信息,或者甚至本质上不支持编码信息。默认情况下,Aspose.Words 会尝试自动检测适当的编码,但在极少数情况下,您可能需要使用与我们的编码识别算法检测到的编码不同的编码。在这种情况下,请使用 Encoding 属性来获取或设置编码。
以下代码示例演示如何设置编码以覆盖自动选择的编码:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
LoadOptions loadOptions = new LoadOptions { Encoding = Encoding.UTF7 }; | |
Document doc = new Document(MyDir + "Encoded in UTF-7.txt", loadOptions); |
加载加密文档
您可以加载使用密码加密的Word文档。为此,请使用特殊的构造函数重载,它接受 LoadOptions 对象。该对象包含 Password 属性,该属性指定密码字符串。
以下代码示例显示如何加载使用密码加密的文档:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
Document doc = new Document(MyDir + "Encrypted.docx", new LoadOptions("docPassword")); |
如果您事先不知道文件是否已加密,则可以使用 FileFormatUtil 类,该类提供了处理文件格式的实用方法,例如检测文件格式或将文件扩展名与文件格式枚举相互转换。要检测文档是否已加密以及是否需要密码才能打开它,请使用 IsEncrypted 属性。
以下代码示例显示如何验证 OpenDocument 是否已加密:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
FileFormatInfo info = FileFormatUtil.DetectFileFormat(MyDir + "Encrypted.odt"); | |
Console.WriteLine(info.IsEncrypted); |