使用C#代码向 Word 文档添加文档属性

文档属性(也称为元数据) 是指描述文档的一组信息。所有 Word 文档都自带一组内置的文档属性,包括标题、作者、主题、关键词等。除了内置文档属性之外,Microsoft Word 还允许用户为 Word 文档添加自定义文档属性

在本文中,我们将介绍如何使用 Spire.Doc for .NET ,通过 C# 和 VB.NET 向 Word 文档添加这些文档属性。

安装 Spire.Doc for .NET

首先,你需要将 Spire.Doc for .NET 包中包含的 DLL 文件添加为 .NET 项目的引用。这些 DLL 文件可以通过此链接下载,或通过 NuGet 进行安装。

cs 复制代码
PM> Install-Package Spire.Doc

在 C# 和 VB.NET 中向 Word 文档添加内置文档属性

内置文档属性由名称和值组成。由于其名称是由 Microsoft Word 预先定义的,因此无法设置或更改内置文档属性的名称,但可以设置或修改其对应的值。

示例代码如下:

cs 复制代码
using Spire.Doc;

namespace BuiltinDocumentProperties
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建一个 Document 实例
            Document document = new Document();
            //加载 Word 文档
            document.LoadFromFile("Sample.docx");

            //向文档添加内置文档属性
            BuiltinDocumentProperties standardProperties = document.BuiltinDocumentProperties;
            standardProperties.Title = "添加文档属性";
            standardProperties.Subject = "C# 示例";
            standardProperties.Author = "张三";
            standardProperties.Company = "Eiceblue";
            standardProperties.Manager = "李四";
            standardProperties.Category = "文档操作";
            standardProperties.Keywords = "C#, Word, 文档属性";
            standardProperties.Comments = "本文演示了如何添加文档属性";

            //保存生成的文档
            document.SaveToFile("StandardDocumentProperties.docx", FileFormat.Docx2013);
        }
    }
}

在 C# 和 VB.NET 中向 Word 文档添加自定义文档属性

自定义文档属性可以由文档作者或用户自行定义。每个自定义文档属性都应包含名称、值和数据类型 。数据类型可以是以下四种之一:文本(Text)日期(Date)数字(Number)是/否(Yes or No)

示例代码如下:

cs 复制代码
using Spire.Doc;
using System;

namespace CustomDocumentProperties
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建一个 Document 实例
            Document document = new Document();
            //加载 Word 文档
            document.LoadFromFile("Sample.docx");

            //向文档添加自定义文档属性
            CustomDocumentProperties customProperties = document.CustomDocumentProperties;
            customProperties.Add("Document ID", 1);
            customProperties.Add("Authorized", true);
            customProperties.Add("Authorized By", "John Smith");
            customProperties.Add("Authorized Date", DateTime.Today);

            //保存生成的文档
            document.SaveToFile("CustomDocumentProperties.docx", FileFormat.Docx2013);
        }
    }
}

申请临时许可证

如果您希望移除生成文档中的评估提示信息,或解除功能限制,请为自己申请一个 30 天的试用许可证

相关推荐
唐青枫27 分钟前
对象到底住在哪里?C#.NET 托管堆从分配、GC 到内存排查
c#·.net
白远山31 分钟前
家政服务家政社区派单实战:调度系统架构设计与实现指南
java·开发语言·小程序·架构·需求分析
DevRay1 小时前
Java 21虚拟线程深度实战:告别线程池焦虑,百万并发吞吐量直接翻倍(原理+代码+避坑)
java·开发语言
free-elcmacom1 小时前
C++学习<1>程序分区
开发语言·c++
醉颜凉2 小时前
Java 必看:如何彻底避免 HashMap 多线程死循环问题?
java·开发语言
默 语2 小时前
Java新手入门:从零开始安装JDK并配置环境变量
java·开发语言·python·mysql·group by·1024程序员节·数据去重
Brilliantwxx2 小时前
【STM32】 I2C_EEPROM_test 工程(实战篇)
开发语言·stm32·单片机·嵌入式硬件·ecmascript
清水白石0082 小时前
Python 异步编程深度解析:Cancellation 到底是异常还是控制信号?从 asyncio 取消机制到企业级事务设计最佳实践
开发语言·python
fogota2 小时前
Emoji与Segoe MDL2 Assets的比较
c#·wpf
kolyle2 小时前
万级 QPS 下的 Token 分发系统架构:从 0 到 1 跑通 AI 时代的“水电煤“
开发语言·人工智能·系统架构·token·qps·极智词元·大模型私有化部署