在本简短指南中,您将简要了解如何在 Java 中编辑 Word 文档元数据。它解释了 IDE 中所需的配置、详细步骤的编程逻辑,以及用 Java 开发 DOCX 元数据编辑器的可运行示例代码。为了清楚地了解此功能,演示了不同类型的自定义和内置属性。
在 Java 中更改 Word 文档属性的步骤
- 设置环境以使用 Aspose.Words for Java 编辑文档属性
- 加载要修改属性的目标Word文件
- 访问 custom properties 的集合并检查所需的属性是否可用
- 设置自定义属性的新值
- 访问和更新 built-in properties
- 使用更新的属性保存生成的 Word 文件
通过遵循这些步骤,您可以在 Java 中编辑 Word 元数据。此过程首先加载源 Word 文件,访问自定义属性,然后根据要求修改它们。在接下来的步骤中,在保存生成的 Word 文件之前相应地访问和更新内置属性。
在 Java 中编辑 Word 文档属性的代码
import com.aspose.words.*; | |
public class Main { | |
public static void main(String[] args) throws Exception // Update Word Metadata in Java | |
{ | |
// Set the licenses | |
new com.aspose.words.License().setLicense("Aspose.Total.lic"); | |
// Load the document | |
Document doc = new Document("SampleProps.docx"); | |
// Access the properties | |
CustomDocumentProperties custProps = doc.getCustomDocumentProperties(); | |
// Check the desired property | |
if (custProps.get("Reviewed") != null) | |
{ | |
// Set new properties | |
custProps.get("Reviewed By").setValue("Mart"); | |
custProps.get("Reviewed Date").setValue(new java.util.Date()); | |
} | |
// Access the properties | |
BuiltInDocumentProperties documentProperties = doc.getBuiltInDocumentProperties(); | |
// Update the properties | |
documentProperties.get("Pages").setValue(doc.getPageCount()); | |
documentProperties.get("Comments").setValue("Document Comments"); | |
documentProperties.get("Title").setValue("Document Title"); | |
// Save the output file | |
doc.save("Output.docx"); | |
System.out.println("Done"); | |
} | |
} |
您已经通过此代码片段探索了在 Java 中编辑 Word 文档元数据的过程。 Document 类中的 getCustomDocumentProperties() 方法提供了用于编辑的自定义属性集合,其中可以使用 get() 方法访问各个属性。同样,getBuiltInDocumentProperties() 方法提供对内置属性的访问以进行修改。
本文指导我们访问和修改 Word 元数据。如果要将 Word 文件转换为 markdown 文件,请参阅 如何使用 Java 将 Word 转换为 markdown 上的文章。