使用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 天的试用许可证。

相关推荐
1024奇点7 小时前
【仓颉语言入门 · 第二篇 第 22 课】
开发语言·ide·开源
Warren2Lynch8 小时前
C4 模型详解:利用 Visual Paradigm 可视化软件架构的综合指南
开发语言·uml
Knight_AL8 小时前
Java 文件下载时文件名中的 `#` 被转换成 `%23`?一次 URL 编码与解码问题排查
java·开发语言
FRing039 小时前
vue2干货总结2(v-bind、v-for、v-model、指令修饰符、v-bind操作class和style)
开发语言·前端·vue.js·vue2
吃一碗粉只付一碗钱9 小时前
16-SqlSugar多库ConfigId与按实体选连接
sqlserver·c#
by209999 小时前
C++ 智能指针的所有权与使用
开发语言·c++·经验分享
砚底藏山河9 小时前
python量化自定义周期 K 线合成
java·开发语言·数据库·python·金融
IT召唤狮10 小时前
开源企业级零信任项目Pangolin,零信任能挡住PHP核弹漏洞吗?
开发语言·开源·php
知已难寻10 小时前
03 · OpenStack的计算管理 Nova
运维·服务器·开发语言·云原生·云计算·openstack·开源软件
Rover Ramble10 小时前
C++ 用互斥锁实现读写锁
开发语言·c++