指定加载选项
加载文档时,可以设置一些高级属性。 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-Java | |
// Specify load option to specify MS Word version | |
LoadOptions loadOptions = new LoadOptions(); | |
loadOptions.setMswVersion(MsWordVersion.WORD_2003); | |
Document doc = new Document(dataDir + "document.doc", loadOptions); | |
doc.save(dataDir + "Word2003_out.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-Java | |
// Specify LoadOptions to add Editing Language | |
LoadOptions loadOptions = new LoadOptions(); | |
loadOptions.getLanguagePreferences().addEditingLanguage(EditingLanguage.JAPANESE); | |
Document doc = new Document(dataDir + "languagepreferences.docx", loadOptions); | |
int localeIdFarEast = doc.getStyles().getDefaultFont().getLocaleIdFarEast(); | |
if (localeIdFarEast == (int) EditingLanguage.JAPANESE) | |
System.out.println("The document either has no any FarEast language set in defaults or it was set to Japanese originally."); | |
else | |
System.out.println("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-Java | |
private static class DocumentLoadingWarningCallback implements IWarningCallback { | |
public void warning(WarningInfo info) { | |
// Prints warnings and their details as they arise during document loading. | |
System.out.println("WARNING: " + info.getWarningType() + " source:" + info.getSource()); | |
System.out.println("\tDescription: " + info.getDescription()); | |
} | |
} |
要获取有关整个加载时间内所有问题的信息,请使用WarningCallback属性。
下面的代码示例演示如何使用此属性:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// Create a new LoadOptions object and set its WarningCallback property. | |
LoadOptions loadOptions = new LoadOptions(); | |
loadOptions.setWarningCallback(new DocumentLoadingWarningCallback()); | |
Document doc = new Document(dataDir + "input.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-Java | |
private static class HtmlLinkedResourceLoadingCallback implements IResourceLoadingCallback { | |
public int resourceLoading(ResourceLoadingArgs args) throws Exception { | |
switch (args.getResourceType()) { | |
case ResourceType.CSS_STYLE_SHEET: { | |
System.out.println("External CSS Stylesheet found upon loading: " + args.getOriginalUri()); | |
// CSS file will don't used in the document | |
return ResourceLoadingAction.SKIP; | |
} | |
case ResourceType.IMAGE: { | |
// Replaces all images with a substitute | |
String newImageFilename = "Logo.jpg"; | |
System.out.println("\tImage will be substituted with: " + newImageFilename); | |
BufferedImage newImage = ImageIO | |
.read(new File(Utils.getDataDir(LoadOptionsCallbacks.class) + newImageFilename)); | |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); | |
ImageIO.write(newImage, "jpg", baos); | |
baos.flush(); | |
byte[] imageBytes = baos.toByteArray(); | |
baos.close(); | |
args.setData(imageBytes); | |
// New images will be used instead of presented in the document | |
return ResourceLoadingAction.USER_PROVIDED; | |
} | |
case ResourceType.DOCUMENT: { | |
System.out.println("External document found upon loading: " + args.getOriginalUri()); | |
// Will be used as usual | |
return ResourceLoadingAction.DEFAULT; | |
} | |
default: | |
throw new Exception("Unexpected ResourceType value."); | |
} | |
} | |
} |
下面的代码示例演示如何使用ResourceLoadingCallback属性:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// Create a new LoadOptions object and set its ResourceLoadingCallback attribute | |
// as an instance of our IResourceLoadingCallback implementation | |
LoadOptions loadOptions = new LoadOptions(); | |
loadOptions.setResourceLoadingCallback(new 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 | |
Document doc = new Document(dataDir + "Images.html", loadOptions); | |
doc.save(dataDir + "Document.LoadOptionsCallback_out.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-Java | |
// Specify LoadOptions to set Temp Folder | |
LoadOptions lo = new LoadOptions(); | |
lo.setTempFolder("C:\\TempFolder\\"); | |
Document doc = new Document(dataDir + "document.doc", lo); |
显式设置编码
大多数现代文档格式将其内容存储在Unicode中,不需要特殊处理。 另一方面,仍然有许多文档使用一些预Unicode编码,有时要么错过编码信息,要么本质上甚至不支持编码信息。 Aspose.Words默认情况下尝试自动检测适当的编码,但在极少数情况下,您可能需要使用与我们的编码识别算法检测到的编码不同的编码。 在这种情况下,使用Encoding属性来获取或设置编码。
下面的代码示例演示如何设置编码以复盖自动选择的编码:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// Set the Encoding attribute in a LoadOptions object to override the | |
// automatically chosen encoding with the one we know to be correct | |
LoadOptions loadOptions = new LoadOptions(); | |
loadOptions.setEncoding(java.nio.charset.Charset.forName("UTF-8")); | |
Document doc = new Document(dataDir + "Encoded in UTF-8.txt", loadOptions); |
加载加密文档
您可以加载使用密码加密的Word文档。 为此,请使用特殊的构造函数重载,它接受LoadOptions对象。 此对象包含Password属性,该属性指定密码字符串。
下面的代码示例演示如何加载使用密码加密的文档:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// For complete examples and data files, please go to | |
// https://github.com/aspose-words/Aspose.Words-for-Java | |
// Load the encrypted document from the absolute path on disk. | |
Document doc = new Document(dataDir + "LoadEncrypted.docx", new LoadOptions("aspose")); |
如果您事先不知道文件是否已加密,则可以使用FileFormatUtil类,该类提供用于处理文件格式的实用程序方法,例如检测文件格式或将文件扩展名转换为/从文件格式枚举。 要检测文档是否已加密并需要密码才能打开,请使用IsEncrypted属性。
下面的代码示例演示如何验证OpenDocument是否已加密:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
FileFormatInfo info = FileFormatUtil.detectFileFormat(dataDir + "encrypted.odt"); | |
System.out.println(info.isEncrypted()); |