这篇简短的文章快速浏览了如何使用 C# 编辑 Word 元数据。它分享了设置开发环境的详细说明、要执行的步骤列表以及使用 C#** 更改 Word 元数据的运行示例代码。您将学习访问自定义和内置属性,并根据应用程序要求编辑它们。
使用 C# 编辑 Word 文档元数据的步骤
- 将 Aspose.Words for .NET 库添加到项目以编辑元数据
- 将 Word 文件加载到 Document 对象中并访问其中的 custom properties
- 使用 Value 属性更新所需的元数据
- 访问内置文档属性
- 使用相应的属性名称更新所需的属性
- 保存生成的 Word 文件
这些步骤总结了使用 C#* 开发 *Word 元数据更改器的过程。该过程首先加载目标 Word 文件,然后访问自定义属性集合进行编辑。同样,您可以访问内置属性并通过提供所需的属性名称和使用值属性设置新数据来修改它们。
使用 C# 在 Word 中编辑文档属性的代码
using Aspose.Words; | |
using Aspose.Words.Properties; | |
class Program{ | |
static void Main(string[] args) // Modify document properties using C# | |
{ | |
// Set the license | |
new License().SetLicense("Aspose.Total.lic"); | |
// Load the document | |
Document doc = new Document("SampleProps.doc"); | |
// Access the custom document properties | |
CustomDocumentProperties custProps = doc.CustomDocumentProperties; | |
if (custProps["Authorized"] != null) | |
{ | |
// Set properties | |
custProps["Authorized By"].Value = "John"; | |
custProps["Authorized Date"].Value = new System.DateTime(1972,11,11); | |
custProps["Authorized Revision"].Value = 200; | |
custProps["Authorized Amount"].Value = 400; | |
} | |
// Access built-in document properties | |
BuiltInDocumentProperties documentProperties = doc.BuiltInDocumentProperties; | |
// Set properties | |
documentProperties["Subject"].Value = "Test Subject"; | |
documentProperties["Manager"].Value = "Test Manager"; | |
documentProperties["Company"].Value = "Test Company"; | |
// Save the Word file | |
doc.Save("Output.doc"); | |
System.Console.WriteLine("Done"); | |
} | |
} |
此代码演示了使用 C#* 编辑 Word 元数据的过程。 Document 类中的 CustomDocumentProperties 集合用于设置自定义属性,BuiltInDocumentProperties 集合用于设置内置属性。您可以设置不同的属性,例如 Author、Category、Comments、Company 和 CreatedTime 等等。
本文教我们使用 C# 开发 DOCX 元数据编辑器。如果您想了解创建新 Word 文件的过程,请参阅 如何在C#中创建Word文档 上的文章。