文档属性记录内容之外的信息。内置属性由 Word 规定名称与含义,涵盖标题、主题、作者、单位、类别、关键词与备注;自定义属性由业务方定义名称与值,常用于合同编号、审核状态、项目编号等字段。属性不参与正文排版,却为归档、检索与流程判断提供依据。
Spire.Doc for C++ 通过 Document->GetBuiltinDocumentProperties() 读写内置属性,通过 Document->GetCustomDocumentProperties() 管理自定义属性。自定义属性的值以 Object 传入,字符串使用 String 对象包装;读取时通过 DocumentProperty->ToString()、GetName() 与 GetValueType() 取得值、名称与类型。
本文介绍三个功能点:
两类属性的入口与用途如下:
| 类别 | 访问入口 | 典型字段 |
|---|---|---|
| 内置属性 | GetBuiltinDocumentProperties() |
标题、主题、作者、单位、类别、关键词、备注 |
| 自定义属性 | GetCustomDocumentProperties() |
合同编号、审核状态、归档日期等业务字段 |
内置属性在“文件 - 信息”与文件属性的“详细信息”中可见;自定义属性保存在文档包的 docProps/custom.xml 中,由读取属性的程序解析。
安装 Spire.Doc for C++
有两种方法可以将 Spire.Doc for C++ 集成到您的应用程序中。一种方法是通过 NuGet 安装,另一种方法是从我们的网站下载包并将库复制到您的程序中。通过 NuGet 安装更简单,也更推荐使用。
- 在 Visual Studio 中打开“NuGet 包管理器”,搜索并安装
Spire.Doc.Cpp。 - 或在项目所在目录执行
nuget install Spire.Doc.Cpp,再将包中的include与lib目录加入项目。
集成后的项目需要引用对应的导入库,并确保运行目录中存在 SpireDocCpp.dll、Spire.Doc.Base.dll 等依赖文件。更详细的配置说明请参考如何将 Spire.Doc for C++ 集成到 C++ 程序中。
设置内置文档属性
内置属性由 BuiltinDocumentProperties 提供,常用字段均有对应的 Set* 与 Get* 方法。示例加载项目进展报告,写入标题、主题、作者、管理者、单位、类别、关键词与备注,再读取其中三项确认写入结果。
| 操作 | 方法 | 说明 |
|---|---|---|
| 标题 | SetTitle(string) / GetTitle() |
文档标题 |
| 主题 | SetSubject(string) |
文档主题 |
| 作者 | SetAuthor(string) |
创建者 |
| 管理者 | SetManager(string) |
负责人或部门 |
| 单位 | SetCompany(string) |
所属公司 |
| 类别 | SetCategory(string) |
文档分类 |
| 关键词 | SetKeywords(string) |
多个关键词以逗号分隔 |
| 备注 | SetComments(string) |
补充说明 |
具体操作步骤如下。
- 创建 Document 对象并调用 LoadFromFile() 加载示例文档。
- 调用 GetBuiltinDocumentProperties() 取得内置属性对象。
- 依次调用 SetTitle()、SetSubject()、SetAuthor()、SetManager()、SetCompany()、SetCategory()、SetKeywords() 与 SetComments() 写入字段。
- 调用 GetTitle()、GetAuthor()、GetCompany() 读取并输出结果。
- 使用 SaveToFile() 保存文档。
设置内置文档属性的代码如下。
#include <iostream>
#include <string>
#include "Spire/Doc/pch.h"
#ifdef DocumentProperties
#undef DocumentProperties
#endif
#define DocumentProperties DocumentPropertiesA
#include "Spire/Doc/BuiltinDocumentProperties.h"
#include "Spire/Doc/Document.h"
#include "Spire/Doc/FileFormat.h"
#include "Spire/Doc/common/h/Stream.h"
using namespace std;
using namespace Spire::Doc;
using namespace Spire::Doc::Common;
int main()
{
SetConsoleOutputCP(CP_UTF8);
//加载项目进展报告
intrusive_ptr<Document> document = new Document();
document->LoadFromFile("Template.docx");
//写入内置的摘要属性
intrusive_ptr<BuiltinDocumentProperties> properties =
document->GetBuiltinDocumentProperties();
properties->SetTitle("智慧仓储管理平台项目进展报告");
properties->SetSubject("2026 年第四季度项目进展");
properties->SetAuthor("项目管理部 陈立");
properties->SetManager("仓储运营中心");
properties->SetCompany("北辰供应链有限公司");
properties->SetCategory("内部报告");
properties->SetKeywords("仓储, 项目进展, 2026Q4, 内部");
properties->SetComments("经项目管理委员会审议,作为季度归档材料。");
cout << "标题:" << properties->GetTitle() << endl;
cout << "作者:" << properties->GetAuthor() << endl;
cout << "单位:" << properties->GetCompany() << endl;
//保存并关闭文档
document->SaveToFile("Output/BuiltinProperties.docx", FileFormat::Docx);
document->Close();
return 0;
}
控制台输出显示三个字段已写入并可读回:
标题:智慧仓储管理平台项目进展报告
作者:项目管理部 陈立
单位:北辰供应链有限公司
保存后查看文件属性的“详细信息”,标题、主题、作者、类别、备注、单位与管理者均出现在属性列表中:

内置属性在保存时写入文件
SetTitle() 等方法修改内存中的属性对象,调用 SaveToFile() 后才写入文档包。若文档由模板生成,模板中已有的属性值会保留到保存时刻,随后被显式设置的值覆盖。示例对八个字段逐一赋值,未赋值的字段保持模板原值。
属性值不参与正文排版
内置属性属于元数据,不会出现在页面正文中,也不影响分页与打印。示例保存后的页面与加载前一致,变化只体现在文件属性面板。需要把标题或作者显示在页面上时,应在正文或页眉页脚中插入对应内容,属性本身不提供版面输出。
统计类字段为只读
WordCount、CharCount、PageCount、ParagraphCount 等统计字段由文档保存时计算,只有 Get* 方法,没有对应的 Set*。示例只设置摘要类字段。需要页数或字数时,读取这些字段即可,不要尝试写入。
添加自定义文档属性
自定义属性的键名与值由业务方决定。CustomDocumentProperties->Add(name, value) 的第二个参数是 intrusive_ptr<Object>,字符串需要先用 String 对象包装。示例添加合同编号、审核状态与归档日期三个属性,随后遍历集合并输出名称、值与值类型。
| 操作 | 方法 | 说明 |
|---|---|---|
| 添加属性 | Add(name, Object) |
值为 Object 派生对象 |
| 添加字符串 | Add(name, new String(value)) |
使用 Spire::Doc::Common::String |
| 统计数量 | GetCount() |
返回属性数量 |
| 按下标取值 | GetItem(index) |
返回 DocumentProperty |
| 读取名称 | DocumentProperty->GetName() |
属性键名 |
| 读取值 | DocumentProperty->ToString() |
转为字符串 |
| 读取类型 | GetValueType() |
返回 PropertyValueType |
具体操作步骤如下。
- 创建 Document 对象并调用 LoadFromFile() 加载示例文档。
- 调用 GetCustomDocumentProperties() 取得自定义属性集合。
- 调用 Add() 添加三个字符串属性,值分别用 String 对象包装。
- 使用 GetCount() 输出数量,循环调用 GetItem(index) 取得每个属性。
- 调用 GetName()、ToString() 与 GetValueType() 输出名称、值与类型。
- 使用 SaveToFile() 保存文档。
添加自定义文档属性的代码如下。
#include <iostream>
#include <string>
#include "Spire/Doc/pch.h"
#ifdef DocumentProperties
#undef DocumentProperties
#endif
#define DocumentProperties DocumentPropertiesA
#include "Spire/Doc/CustomDocumentProperties.h"
#include "Spire/Doc/Document.h"
#include "Spire/Doc/DocumentProperty.h"
#include "Spire/Doc/FileFormat.h"
#include "Spire/Doc/common/h/Stream.h"
#include "Spire/Doc/common/h/System_String.h"
using namespace std;
using namespace Spire::Doc;
using namespace Spire::Doc::Common;
//把属性值类型转换为可读名称
string GetTypeName(PropertyValueType type)
{
switch (type)
{
case PropertyValueType::String: return "String";
case PropertyValueType::Int:
case PropertyValueType::Int32: return "Integer";
case PropertyValueType::Date: return "Date";
case PropertyValueType::Boolean: return "Boolean";
case PropertyValueType::Double:
case PropertyValueType::Float: return "Number";
default: return "Other";
}
}
int main()
{
SetConsoleOutputCP(CP_UTF8);
//加载项目进展报告
intrusive_ptr<Document> document = new Document();
document->LoadFromFile("Template.docx");
//添加三个自定义属性,值以 String 对象传入
intrusive_ptr<CustomDocumentProperties> properties =
document->GetCustomDocumentProperties();
properties->Add("合同编号", new String("HT-2026-018"));
properties->Add("审核状态", new String("已审核"));
properties->Add("归档日期", new String("2026-12-31"));
//遍历自定义属性
cout << "自定义属性数量:" << properties->GetCount() << endl;
for (int i = 0; i < properties->GetCount(); i++)
{
intrusive_ptr<DocumentProperty> property = properties->GetItem(i);
cout << property->GetName() << " = " << property->ToString()
<< "(" << GetTypeName(property->GetValueType()) << ")" << endl;
}
//保存并关闭文档
document->SaveToFile("Output/CustomProperties.docx", FileFormat::Docx);
document->Close();
return 0;
}
控制台输出三个属性的名称、值与类型,值类型均为 String:
自定义属性数量:3
合同编号 = HT-2026-018(String)
审核状态 = 已审核(String)
归档日期 = 2026-12-31(String)
保存后的文档正文不变,自定义属性写入文档包。下图中右侧为程序输出的属性清单:

值必须包装为 Object
Add() 的第二个参数是 intrusive_ptr<Object>,不能直接传入 std::string。示例使用 new String("HT-2026-018") 创建 Spire::Doc::Common::String,该类型继承自 Object,可直接作为参数。属性值统一以 Object 传入;读取时可通过 DocumentProperty 的 ToInt()、ToDateTime() 或 ToBool() 按目标类型转换。
自定义属性保存在 custom.xml
保存后,文档包中新增 docProps/custom.xml,每个属性对应一个 <property> 元素。示例生成的内容如下(节选):
<Properties xmlns="http://schemas.openxmlformats.org/officeDocument/2006/custom-properties"
xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes">
<property fmtid="{D5CDD505-2E9C-101B-9397-08002B2CF9AE}" pid="2" name="合同编号">
<vt:lpwstr>HT-2026-018</vt:lpwstr>
</property>
<property fmtid="{D5CDD505-2E9C-101B-9397-08002B2CF9AE}" pid="3" name="审核状态">
<vt:lpwstr>已审核</vt:lpwstr>
</property>
<property fmtid="{D5CDD505-2E9C-101B-9397-08002B2CF9AE}" pid="4" name="归档日期">
<vt:lpwstr>2026-12-31</vt:lpwstr>
</property>
</Properties>
pid 从 2 开始递增,name 为属性键名,vt:lpwstr 表示 Unicode 字符串。
自定义属性不出现在详细信息面板
Windows 文件属性的“详细信息”面板只列出系统识别的标准字段,docx 的自定义属性不会显示在其中。核对自定义属性时,应通过程序读取 CustomDocumentProperties,或直接查看 docProps/custom.xml。示例因此在正文旁附上程序输出的属性清单。
读取与删除自定义文档属性
读取单个属性使用 GetItem(name) 或 GetItem(index),返回 DocumentProperty;删除使用 Remove(name)。示例加载含三个自定义属性的文档,先输出数量与合同编号,再删除“审核状态”,输出剩余属性的名称。
| 操作 | 方法 | 说明 |
|---|---|---|
| 按键名读取 | GetItem(name) |
返回 DocumentProperty |
| 按下标读取 | GetItem(index) |
返回 DocumentProperty |
| 读取值 | DocumentProperty->ToString() |
字符串形式 |
| 删除属性 | Remove(name) |
按键名删除 |
| 删除后统计 | GetCount() |
数量减一 |
具体操作步骤如下。
- 创建 Document 对象并调用 LoadFromFile() 加载含自定义属性的文档。
- 调用 GetCustomDocumentProperties() 取得属性集合。
- 调用 GetCount() 输出删除前的数量。
- 调用 GetItem("合同编号")->ToString() 按键名读取值。
- 调用 Remove("审核状态") 删除属性,再次输出数量并遍历剩余名称。
- 使用 SaveToFile() 保存结果文档。
读取与删除自定义文档属性的代码如下。
#include <iostream>
#include <string>
#include "Spire/Doc/pch.h"
#ifdef DocumentProperties
#undef DocumentProperties
#endif
#define DocumentProperties DocumentPropertiesA
#include "Spire/Doc/CustomDocumentProperties.h"
#include "Spire/Doc/Document.h"
#include "Spire/Doc/DocumentProperty.h"
#include "Spire/Doc/FileFormat.h"
#include "Spire/Doc/common/h/Stream.h"
using namespace std;
using namespace Spire::Doc;
using namespace Spire::Doc::Common;
int main()
{
SetConsoleOutputCP(CP_UTF8);
//加载含三个自定义属性的文档
intrusive_ptr<Document> document = new Document();
document->LoadFromFile("CustomProperties.docx");
intrusive_ptr<CustomDocumentProperties> properties =
document->GetCustomDocumentProperties();
cout << "删除前的属性数量:" << properties->GetCount() << endl;
//按名称读取单个属性
cout << "合同编号:" << properties->GetItem("合同编号")->ToString() << endl;
//删除审核状态属性,再输出剩余属性的名称
properties->Remove("审核状态");
cout << "删除后的属性数量:" << properties->GetCount() << endl;
for (int i = 0; i < properties->GetCount(); i++)
{
cout << properties->GetItem(i)->GetName() << endl;
}
//保存并关闭文档
document->SaveToFile("Output/CustomPropertiesCleaned.docx", FileFormat::Docx);
document->Close();
return 0;
}
控制台输出显示属性数量由 3 变为 2,剩余两项为合同编号与归档日期:
删除前的属性数量:3
合同编号:HT-2026-018
删除后的属性数量:2
合同编号
归档日期
保存后的文档中不再包含“审核状态”,docProps/custom.xml 只保留两个属性:

删除属性不影响正文
自定义属性属于文档元数据,删除后正文内容与排版均不改变。示例删除“审核状态”后,页面和段落保持原样,变化只体现在属性集合与 custom.xml 中。若业务需要在页面上显示审核状态,应通过域或文本引用属性值,删除属性后相应内容需要同步处理。
按键名读取区分大小写
GetItem("合同编号") 的名称必须与 Add() 时写入的键名完全一致。名称不一致时,读取结果不是目标属性。遍历时使用 GetItem(index)->GetName() 取得实际键名,再与业务字段比对,可避免因名称差异导致读取失败。
删除后保存才写入文件
Remove() 修改内存中的属性集合,调用 SaveToFile() 后才写入 custom.xml。示例在删除后立即保存,结果文档的属性数量为 2。若需要保留原文件,应在删除前复制文档,或另存为新的文件名。
申请临时 License
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。







