指定加载选项
加载文档时,可以设置一些高级属性。 Aspose.Words为您提供LoadOptions类,它允许更精确地控制加载过程。 一些加载格式有一个相应的类,用于保存此加载格式的加载选项,例如,有PdfLoadOptions用于加载到PDF格式或TxtLoadOptions用于加载到TXT。 本文提供了使用LoadOptions类的选项的示例。
设置Microsoft Word版本以更改外观
不同版本的Microsoft Word应用程序可以无动于衷地显示文档。 例如,使用WPSOffice生成的DOCX或DOTX等OOXML文档存在一个众所周知的问题。 在这种情况下,基本文档标记元素可能丢失或可能被不同地解释,导致Microsoft Word2019与Microsoft Word2010相比以不同的方式显示此类文档。
默认情况下,Aspose.Words使用Microsoft Word2019规则打开文档。 如果您需要使文档加载显示为以前的Microsoft Word应用程序版本之一,则应使用LoadOptions类的MswVersion属性显式指定所需的版本。
下面的代码示例演示如何使用load选项设置Microsoft Word版本:
For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C | |
System::SharedPtr<LoadOptions> loadOptions = System::MakeObject<LoadOptions>(); | |
// Change the loading version to Microsoft Word 2010. | |
loadOptions->set_MswVersion(MsWordVersion::Word2010); | |
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"Document.docx", loadOptions); | |
System::String outputPath = outputDataDir + u"Load_Options.SetMSWordVersion.docx"; | |
doc->Save(outputPath); |
设置语言首选项以更改外观
在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-C | |
// Create a new LoadOptions object. | |
System::SharedPtr<LoadOptions> loadOptions = System::MakeObject<LoadOptions>(); | |
// Set language preferences that will be used when document is loading. | |
loadOptions->get_LanguagePreferences()->AddEditingLanguage(EditingLanguage::Japanese); | |
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"languagepreferences.docx", loadOptions); |
在加载文档时使用WarningCallback来控制问题
某些文档可能已损坏、包含无效条目或具有Aspose.Words当前不支持的功能。 如果您想了解加载文档时发生的问题,Aspose.Words提供IWarningCallback接口。
下面的代码示例演示IWarningCallback接口的实现:
For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C | |
class DocumentLoadingWarningCallback : public IWarningCallback | |
{ | |
public: | |
System::SharedPtr<WarningInfoCollection> mWarnings; | |
void Warning(System::SharedPtr<WarningInfo> info) override; | |
DocumentLoadingWarningCallback(); | |
}; | |
void DocumentLoadingWarningCallback::Warning(System::SharedPtr<WarningInfo> info) | |
{ | |
// Prints warnings and their details as they arise during document loading. | |
std::cout << "WARNING: {info->get_WarningType} " << std::endl; | |
std::cout << "Source: {info->get_Source} " << std::endl; | |
std::cout << "\tDescription: {info->get_Description} " << std::endl; | |
} | |
DocumentLoadingWarningCallback::DocumentLoadingWarningCallback() : mWarnings(System::MakeObject<WarningInfoCollection>()) | |
{ | |
} |
要获取有关整个加载时间内所有问题的信息,请使用WarningCallback
属性。
下面的代码示例演示如何使用此属性:
For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C | |
// Create a new LoadOptions object and set its WarningCallback property. | |
System::SharedPtr<LoadOptions> loadOptions = System::MakeObject<LoadOptions>(); | |
System::SharedPtr<DocumentLoadingWarningCallback> callback = System::MakeObject<DocumentLoadingWarningCallback>(); | |
loadOptions->set_WarningCallback(callback); | |
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"Document.docx", loadOptions); |
使用ResourceLoadingCallback控制外部资源加载
文档可能包含指向本地磁盘、网络或Internet上某处的图像的外部链接。 Aspose.Words自动将此类图像加载到文档中,但有些情况下需要控制此过程。 例如,决定我们是否真的需要加载某个图像,或者跳过它。 ResourceLoadingCallbackload选项允许您对此进行控制。
下面的代码示例演示IResourceLoadingCallback接口的实现:
For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C | |
class HtmlLinkedResourceLoadingCallback : public IResourceLoadingCallback | |
{ | |
public: | |
ResourceLoadingAction ResourceLoading(System::SharedPtr<ResourceLoadingArgs> args) override; | |
HtmlLinkedResourceLoadingCallback(); | |
}; | |
HtmlLinkedResourceLoadingCallback::HtmlLinkedResourceLoadingCallback() | |
{ | |
} | |
ResourceLoadingAction HtmlLinkedResourceLoadingCallback::ResourceLoading(System::SharedPtr<ResourceLoadingArgs> args) | |
{ | |
switch (args->get_ResourceType()) | |
{ | |
case ResourceType::CssStyleSheet: | |
{ | |
std::cout << "External CSS Stylesheet found upon loading: " << args->get_OriginalUri().ToUtf8String() << std::endl; | |
// CSS file will don't used in the document | |
return ResourceLoadingAction::Skip; | |
} | |
case ResourceType::Image: | |
{ | |
// Replaces all images with a substitute | |
System::String newImageFilename = u"Logo.jpg"; | |
std::cout << "\tImage will be substituted with: " << newImageFilename.ToUtf8String() << std::endl; | |
System::SharedPtr<System::Drawing::Image> newImage = System::Drawing::Image::FromFile(GetInputDataDir_LoadingAndSaving() + newImageFilename); | |
System::SharedPtr<System::Drawing::ImageConverter> converter = System::MakeObject<System::Drawing::ImageConverter>(); | |
auto imageBytes = System::DynamicCast<System::Array<uint8_t>>(converter->ConvertTo(nullptr, nullptr, newImage, System::ObjectExt::GetType<System::Array<uint8_t>>())); | |
//System::ArrayPtr<uint8_t> imageBytes = System::IO::File::ReadAllBytes(GetInputDataDir_LoadingAndSaving() + newImageFilename); | |
args->SetData(imageBytes); | |
// New images will be used instead of presented in the document | |
return ResourceLoadingAction::UserProvided; | |
} | |
case ResourceType::Document: | |
{ | |
std::cout << "External document found upon loading: " << args->get_OriginalUri().ToUtf8String() << std::endl; | |
// Will be used as usual | |
return ResourceLoadingAction::Default; | |
} | |
default: | |
throw System::InvalidOperationException(u"Unexpected ResourceType value."); | |
} | |
} | |
下面的代码示例演示如何使用ResourceLoadingCallback属性:
For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C | |
// Create a new LoadOptions object and set its ResourceLoadingCallback attribute as an instance of our IResourceLoadingCallback implementation | |
System::SharedPtr<LoadOptions> loadOptions = System::MakeObject<LoadOptions>(); | |
loadOptions->set_ResourceLoadingCallback(System::MakeObject<HtmlLinkedResourceLoadingCallback>()); | |
// When we open an Html document, external resources such as references to CSS stylesheet files and external images | |
// will be handled in a custom manner by the loading callback as the document is loaded | |
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"Images.html", loadOptions); | |
doc->Save(outputDataDir + u"Load_Options.LoadOptionsResourceLoadingCallback.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-C | |
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C | |
System::SharedPtr<LoadOptions> loadOptions = System::MakeObject<LoadOptions>(); | |
loadOptions->set_TempFolder(u"C:/TempFolder/"); | |
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"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-C | |
// Set the Encoding attribute in a LoadOptions object to override the automatically chosen encoding with the one we know to be correct | |
System::SharedPtr<LoadOptions> loadOptions = System::MakeObject<LoadOptions>(); | |
loadOptions->set_Encoding(System::Text::Encoding::get_UTF7()); | |
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"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-C | |
// The path to the documents directory. | |
System::String dataDir = GetInputDataDir_LoadingAndSaving(); | |
// Loads encrypted document. | |
System::SharedPtr<Document> doc = System::MakeObject<Document>(dataDir + u"LoadEncrypted.docx", System::MakeObject<LoadOptions>(u"aspose")); | |
如果您事先不知道文件是否已加密,则可以使用FileFormatUtil类,该类提供用于处理文件格式的实用方法,例如检测文件格式或将文件扩展名转换为/从文件格式枚举。 要检测文档是否已加密并需要密码才能打开,请使用IsEncrypted
属性。
下面的代码示例演示如何验证OpenDocument是否已加密:
For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C | |
System::SharedPtr<FileFormatInfo> info = FileFormatUtil::DetectFileFormat(inputDataDir + u"encrypted.odt"); | |
std::cout << info->get_IsEncrypted() << std::endl; |