在 Aspose.Words for Java 中使用加载选项

在 Aspose.Words for Java 中使用加载选项简介

在本教程中,我们将探讨如何在 Aspose.Words for Java 中使用加载选项。加载选项允许您自定义文档的加载和处理方式。我们将介绍各种场景,包括更新脏字段、加载加密文档、将形状转换为 Office Math、设置 MS Word 版本、指定临时文件夹、处理警告以及将图元文件转换为 PNG。让我们一步步深入。

更新脏字段

LoadOptions loadOptions = new LoadOptions();
loadOptions.setUpdateDirtyFields(true);

Document doc = new Document("Your Directory Path" + "Dirty field.docx", loadOptions);
doc.save("Your Directory Path" + "WorkingWithLoadOptions.UpdateDirtyFields.docx");

此代码片段演示了如何更新文档中的脏字段。这setUpdateDirtyFields(true)方法用于确保在文档加载期间更新脏字段。

加载加密文档

@Test
public void loadEncryptedDocument() throws Exception {
    Document doc = new Document("Your Directory Path" + "Encrypted.docx", new LoadOptions("docPassword"));
    doc.save("Your Directory Path" + "WorkingWithLoadOptions.LoadAndSaveEncryptedOdt.odt", new OdtSaveOptions("newPassword"));
}

在这里,我们使用密码加载加密文档。这LoadOptions构造函数接受文档密码,也可以在保存文档时指定新密码,使用OdtSaveOptions.

将形状转换为 Office Math

LoadOptions loadOptions = new LoadOptions();
loadOptions.setConvertShapeToOfficeMath(true);

Document doc = new Document("Your Directory Path" + "Office math.docx", loadOptions);
doc.save("Your Directory Path" + "WorkingWithLoadOptions.ConvertShapeToOfficeMath.docx", SaveFormat.DOCX);

此代码演示了如何在文档加载期间将形状转换为 Office Math 对象。这setConvertShapeToOfficeMath(true)方法启用此转换。

设置 MS Word 版本

@Test
public void setMsWordVersion() throws Exception {
    LoadOptions loadOptions = new LoadOptions();
    loadOptions.setMswVersion(MsWordVersion.WORD_2010);

    Document doc = new Document("Your Directory Path" + "Document.docx", loadOptions);
    doc.save("Your Directory Path" + "WorkingWithLoadOptions.SetMsWordVersion.docx");
}

您可以指定用于文档加载的 MS Word 版本。在此示例中,我们使用以下命令将版本设置为 Microsoft Word 2010setMswVersion.

使用临时文件夹

@Test
public void useTempFolder() throws Exception {
    LoadOptions loadOptions = new LoadOptions();
    loadOptions.setTempFolder("Your Directory Path");

    Document doc = new Document("Your Directory Path" + "Document.docx", loadOptions);
}

通过使用设置临时文件夹setTempFolder,您可以控制文档处理过程中临时文件的存储位置。

警告回调

@Test
public void warningCallback() throws Exception {
    LoadOptions loadOptions = new LoadOptions();
    loadOptions.setWarningCallback(new DocumentLoadingWarningCallback());

    Document doc = new Document("Your Directory Path" + "Document.docx", loadOptions);
}

public static class DocumentLoadingWarningCallback implements IWarningCallback {
    public void warning(WarningInfo info) {
        //处理文档加载过程中出现的警告。
        System.out.println(MessageFormat.format("WARNING: {0}, source: {1}", info.getWarningType(), info.getSource()));
        System.out.println(MessageFormat.format("\tDescription: {0}", info.getDescription()));
    }
}

此代码演示了如何设置警告回调来处理文档加载期间的警告。您可以自定义发生警告时应用程序的行为。

将图元文件转换为 PNG

@Test
public void convertMetafilesToPng() throws Exception {
    LoadOptions loadOptions = new LoadOptions();
    loadOptions.setConvertMetafilesToPng(true);

    Document doc = new Document("Your Directory Path" + "WMF with image.docx", loadOptions);
}

要在文档加载期间将图元文件(例如 WMF)转换为 PNG 图像,您可以使用setConvertMetafilesToPng(true)方法。

在 Aspose.Words for Java 中使用加载选项的完整源代码

public void updateDirtyFields() throws Exception {
	LoadOptions loadOptions = new LoadOptions();
	{
		loadOptions.setUpdateDirtyFields(true);
	}
	Document doc = new Document("Your Directory Path" + "Dirty field.docx", loadOptions);
	doc.save("Your Directory Path" + "WorkingWithLoadOptions.UpdateDirtyFields.docx");
}
@Test
public void loadEncryptedDocument() throws Exception {
	Document doc = new Document("Your Directory Path" + "Encrypted.docx", new LoadOptions("docPassword"));
	doc.save("Your Directory Path" + "WorkingWithLoadOptions.LoadAndSaveEncryptedOdt.odt", new OdtSaveOptions("newPassword"));
}
@Test
public void convertShapeToOfficeMath() throws Exception {
	LoadOptions loadOptions = new LoadOptions();
	{
		loadOptions.setConvertShapeToOfficeMath(true);
	}
	Document doc = new Document("Your Directory Path" + "Office math.docx", loadOptions);
	doc.save("Your Directory Path" + "WorkingWithLoadOptions.ConvertShapeToOfficeMath.docx", SaveFormat.DOCX);
}
@Test
public void setMsWordVersion() throws Exception {
	//创建一个新的LoadOptions对象,默认情况下将根据MS Word 2019规范加载文档
	//并将加载版本更改为Microsoft Word 2010。
	LoadOptions loadOptions = new LoadOptions();
	{
		loadOptions.setMswVersion(MsWordVersion.WORD_2010);
	}
	Document doc = new Document("Your Directory Path" + "Document.docx", loadOptions);
	doc.save("Your Directory Path" + "WorkingWithLoadOptions.SetMsWordVersion.docx");
}
@Test
public void useTempFolder() throws Exception {
	LoadOptions loadOptions = new LoadOptions();
	{
		loadOptions.setTempFolder("Your Directory Path");
	}
	Document doc = new Document("Your Directory Path" + "Document.docx", loadOptions);
}
@Test
public void warningCallback() throws Exception {
	LoadOptions loadOptions = new LoadOptions();
	{
		loadOptions.setWarningCallback(new DocumentLoadingWarningCallback());
	}
	Document doc = new Document("Your Directory Path" + "Document.docx", loadOptions);
}
public static class DocumentLoadingWarningCallback implements IWarningCallback {
	public void warning(WarningInfo info) {
		//打印文档加载过程中出现的警告及其详细信息。
		System.out.println(MessageFormat.format("WARNING: {0}, source: {1}", info.getWarningType(), info.getSource()));
		System.out.println(MessageFormat.format("\tDescription: {0}", info.getDescription()));
	}
}
@Test
public void convertMetafilesToPng() throws Exception {
	LoadOptions loadOptions = new LoadOptions();
	{
		loadOptions.setConvertMetafilesToPng(true);
	}
	Document doc = new Document("Your Directory Path" + "WMF with image.docx", loadOptions);
}
@Test
public void loadChm() throws Exception {
	LoadOptions loadOptions = new LoadOptions();
	{
		loadOptions.setEncoding(Charset.forName("windows-1251"));
	}
	Document doc = new Document("Your Directory Path" + "HTML help.chm", loadOptions);
}

结论

在本教程中,我们深入研究了在 Aspose.Words for Java 中使用加载选项的各个方面。加载选项在自定义文档的加载和处理方式方面发挥着至关重要的作用,使您可以根据自己的特定需求定制文档处理。让我们回顾一下本指南中涵盖的要点:

常见问题解答

如何处理文档加载过程中的警告?

您可以设置警告回调,如下所示warningCallback()方法同上。定制DocumentLoadingWarningCallback类来根据应用程序的要求处理警告。

加载文档时可以将形状转换为 Office Math 对象吗?

是的,您可以使用以下方法将形状转换为 Office Math 对象loadOptions.setConvertShapeToOfficeMath(true).

如何指定加载文档的 MS Word 版本?

使用loadOptions.setMswVersion(MsWordVersion.WORD_2010)指定用于文档加载的 MS Word 版本。

目的是什么setTempFolder method in Load Options?

setTempFolder方法允许您指定在文档处理过程中存储临时文件的文件夹。