PDF 的属性面板里记着标题、作者、主题、关键字这几个字段,知识库、归档系统和全文检索都拿它们做归类依据。实际收到的文件往往相反:标题还是上一个模板留下的名字,作者一栏空着,关键字干脆没有。补全要在阅读器里逐字段手填,核对一批文档的作者或主题也只能一份份点开属性对话框——桌面软件做不了批量,把文件传到服务端又意味着内容离开了用户的设备。
本文介绍用 Spire.PDF for JavaScript 设置和获取 PDF 文档属性。它基于 WebAssembly 在浏览器端直接加载、修改与保存 PDF 文档,全过程在本地完成,通过虚拟文件系统(VFS)读写文件,无需后端配合。
本文介绍两个核心功能点:
有关安装和项目配置,请参考 React 项目中集成 Spire.PDF for JavaScript。以下示例默认已安装 Spire.PDF 并完成 WebAssembly 模块初始化。
设置 PDF 文档属性
Spire.PDF for JavaScript 提供 doc.DocumentInformation,用于写入标准文档属性——标题、作者、主题、关键字各占一个字段,Creator 与 Producer 记录生成方,取值都是字符串。
function App() {
const setPdfProperties = async () => {
// 获取 Spire.PDF WASM 模块
const pdfModule = window.wasmModule?.spirepdf;
// 检查模块是否就绪
if (!pdfModule) {
alert('Spire.PDF is not ready yet');
return;
}
// 将待处理的 PDF 文件载入 VFS
const inputFileName = '产品介绍.pdf';
await window.spire.FetchFileToVFS(inputFileName, '', `${process.env.PUBLIC_URL}/data/`);
// 创建 PdfDocument 对象并加载 PDF 文档
const doc = new pdfModule.PdfDocument();
doc.LoadFromFile(inputFileName);
// 写入标准文档属性
doc.DocumentInformation.Title = '2026 年度产品介绍';
doc.DocumentInformation.Author = '市场部';
doc.DocumentInformation.Subject = '产品线与报价';
doc.DocumentInformation.Keywords = '产品介绍, 报价, 2026';
doc.DocumentInformation.Creator = '内容中心';
doc.DocumentInformation.Producer = 'Spire.PDF for JavaScript';
const outputFileName = '已设置属性的文档.pdf';
doc.SaveToFile(outputFileName);
doc.Close();
// 从 VFS 读取生成的文件,触发下载
const fileArray = window.dotnetRuntime.Module.FS.readFile(outputFileName);
const blob = new Blob([fileArray], { type: 'application/pdf' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = outputFileName;
a.click();
URL.revokeObjectURL(url);
};
return (
<div style={{ textAlign: 'center', height: '300px' }}>
<h1>设置 PDF 文档属性</h1>
<button onClick={setPdfProperties}>
开始设置
</button>
</div>
);
}
export default App;
设置完成后在阅读器文档属性面板中看到的标准字段:

获取 PDF 文档属性
读取走的是同一个 DocumentInformation:标准字段取回字符串。把取到的值拼成文本写出,就能在批量流程里直接比对或入库,不必依赖阅读器的属性面板。
function App() {
const getPdfProperties = async () => {
// 获取 Spire.PDF WASM 模块
const pdfModule = window.wasmModule?.spirepdf;
// 检查模块是否就绪
if (!pdfModule) {
alert('Spire.PDF is not ready yet');
return;
}
// 将待读取的 PDF 文件载入 VFS
const inputFileName = '已设置属性的文档.pdf';
await window.spire.FetchFileToVFS(inputFileName, '', `${process.env.PUBLIC_URL}/data/`);
// 创建 PdfDocument 对象并加载 PDF 文档
const doc = new pdfModule.PdfDocument();
doc.LoadFromFile(inputFileName);
// 逐个取回标准属性与创建/修改时间
const info = doc.DocumentInformation;
const lines = [
`Title: ${info.Title}`,
`Author: ${info.Author}`,
`Subject: ${info.Subject}`,
`Keywords: ${info.Keywords}`,
`Creator: ${info.Creator}`,
`Producer: ${info.Producer}`,
`CreationDate: ${info.CreationDate.toString()}`,
`ModificationDate: ${info.ModificationDate.toString()}`,
];
// 把结果写成文本文件
const outputFileName = '文档属性.txt';
window.dotnetRuntime.Module.FS.writeFile(outputFileName, lines.join('\n'));
doc.Close();
// 从 VFS 读取生成的文件,触发下载
const fileArray = window.dotnetRuntime.Module.FS.readFile(outputFileName);
const blob = new Blob([fileArray], { type: 'text/plain' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = outputFileName;
a.click();
URL.revokeObjectURL(url);
};
return (
<div style={{ textAlign: 'center', height: '300px' }}>
<h1>获取 PDF 文档属性</h1>
<button onClick={getPdfProperties}>
开始获取
</button>
</div>
);
}
export default App;
导出的文本文档,逐行列出读到的标准属性:

常见问题
设置的属性重新打开文件后没有变化
原因:DocumentInformation 上的字段改的是内存中的文档对象,只有调用 SaveToFile 才会写进文件。赋完值直接 Close(),或者打开的还是原来那份输入文件,看到的自然还是旧值。
解决:赋值之后另存为新的输出文件,再打开该产物查看:
doc.DocumentInformation.Title = '2026 年度产品介绍';
doc.DocumentInformation.Author = '市场部';
// 保存后属性才会落进文件
doc.SaveToFile('已设置属性的文档.pdf');
获取免费许可证
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。









