一份演示文稿往往要经过几轮审阅,页面上那些「这里补一组数据」「标题再短一点」的批注,就是评审过程留下的记录。定稿前要把意见归档,或者把过时的批注清理掉,手工逐条处理费时,多人协作的文件里还容易漏。放到服务端批处理,文件同样要先离开用户的设备。
本文用 Spire.Presentation for JavaScript 在浏览器中直接添加、替换和删除 PowerPoint 批注。整个过程基于 WebAssembly 在浏览器端完成,无需安装 Office,不依赖后端,文件通过虚拟文件系统(VFS)在本地读写。
本文介绍三个核心功能点:
有关安装和项目配置,请参考 React 项目中集成 Spire.Presentation for JavaScript。下面的示例假定 Spire.Presentation 已安装、WebAssembly 模块已初始化。
添加 PowerPoint 批注
添加批注用 Slide.AddComment():
| 写法 | 添加的内容 |
|---|---|
AddComment({ author, text, position, dateTime }) |
以指定作者、内容、位置和时间新建一条批注 |
AddComment({ comment }) |
把已有的批注对象添加到当前页 |
position 是 PointF,坐标以磅为单位,从幻灯片左上角量起。
具体操作步骤如下:
- 获取 Spire.Presentation 的 WASM 模块,确认已就绪。
- 读入示例演示文稿与字体,创建
Presentation对象并加载该文件。 - 使用
Presentation.CommentAuthors.AddAuthor()方法注册作者,再用Presentation.Slides.get_Item().AddComment()方法添加批注。 - 使用
Presentation.SaveToFile()方法保存结果文件。 - 从 VFS 读出结果文件并触发浏览器下载。
添加 PowerPoint 批注的代码如下:
function App() {
const addComment = async () => {
// 获取 Spire.Presentation WASM 模块
const pptModule = window.wasmModule?.spirepresentation;
// 检查模块是否就绪
if (!pptModule) {
alert('Spire.Presentation is not ready yet');
return;
}
// 载入示例演示文稿与字体
await window.spire.FetchFileToVFS('示例演示文稿.pptx', "", `${process.env.PUBLIC_URL}/data/`);
await window.spire.FetchFileToVFS('ARIALUNI.TTF', "/Library/Fonts/", `${process.env.PUBLIC_URL}/font/`);
// 创建 Presentation 实例并加载演示文稿
const ppt = new pptModule.Presentation();
ppt.LoadFromFile('示例演示文稿.pptx');
// 注册批注作者
const author = ppt.CommentAuthors.AddAuthor('Eric', 'ER');
// 在第一张幻灯片上添加批注,位置取左上角附近的 (60, 80) 磅
ppt.Slides.get_Item(0).AddComment({
author: author,
text: 'Please add a one-line product intro.',
position: new pptModule.PointF(60, 80),
dateTime: new Date(),
});
// 保存结果文件
const outputFileName = '添加批注后.pptx';
ppt.SaveToFile({ file: outputFileName, fileFormat: pptModule.FileFormat.Pptx2013 });
ppt.Dispose();
// 从 VFS 读取生成的文件,触发下载
const fileArray = window.dotnetRuntime.Module.FS.readFile(outputFileName);
const blob = new Blob([fileArray], { type: 'application/vnd.openxmlformats-officedocument.presentationml.presentation' });
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>添加 PowerPoint 批注</h1>
<button onClick={addComment}>
添加批注
</button>
</div>
);
}
export default App;
第一张幻灯片上多了一条 Eric 批注:

替换 PowerPoint 批注
替换即改写批注的 Text,示例遍历 Slide.Comments 找到 Nina 那条改掉。
具体操作步骤如下:
- 获取 Spire.Presentation 的 WASM 模块,确认已就绪。
- 读入示例演示文稿与字体,创建
Presentation对象并加载该文件。 - 使用
Presentation.Slides.get_Item().Comments取得批注集合,遍历找到目标批注并写入Text属性。 - 使用
Presentation.SaveToFile()方法保存结果文件。 - 从 VFS 读出结果文件并触发浏览器下载。
替换 PowerPoint 批注的代码如下:
function App() {
const replaceComment = async () => {
// 获取 Spire.Presentation WASM 模块
const pptModule = window.wasmModule?.spirepresentation;
// 检查模块是否就绪
if (!pptModule) {
alert('Spire.Presentation is not ready yet');
return;
}
// 载入示例演示文稿与字体
await window.spire.FetchFileToVFS('示例演示文稿.pptx', "", `${process.env.PUBLIC_URL}/data/`);
await window.spire.FetchFileToVFS('ARIALUNI.TTF', "/Library/Fonts/", `${process.env.PUBLIC_URL}/font/`);
// 创建 Presentation 实例并加载演示文稿
const ppt = new pptModule.Presentation();
ppt.LoadFromFile('示例演示文稿.pptx');
// 取第一张幻灯片的批注集合
const comments = ppt.Slides.get_Item(0).Comments;
// 按作者找到目标批注,替换文本
for (let i = 0; i < comments.length; i++) {
if (comments[i].AuthorName === 'Nina') {
comments[i].Text = 'Title updated, please review.';
break;
}
}
// 保存结果文件
const outputFileName = '替换批注后.pptx';
ppt.SaveToFile({ file: outputFileName, fileFormat: pptModule.FileFormat.Pptx2013 });
ppt.Dispose();
// 从 VFS 读取生成的文件,触发下载
const fileArray = window.dotnetRuntime.Module.FS.readFile(outputFileName);
const blob = new Blob([fileArray], { type: 'application/vnd.openxmlformats-officedocument.presentationml.presentation' });
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>替换 PowerPoint 批注</h1>
<button onClick={replaceComment}>
替换批注
</button>
</div>
);
}
export default App;
Nina 的批注内容换成了 “Title updated, please review.”:

删除 PowerPoint 批注
删除用 Slide.DeleteComment(),四个重载范围由小到大:
| 写法 | 删除的批注 |
|---|---|
DeleteComment({ comment }) |
指定的那一条批注对象 |
DeleteComment({ author, text }) |
作者与文本同时匹配的批注 |
DeleteComment({ author }) |
该作者在该页的全部批注 |
DeleteComment({ text }) |
文本与该内容相同的全部批注 |
示例用 { comment } 重载删掉 Cral 那条。
具体操作步骤如下:
- 获取 Spire.Presentation 的 WASM 模块,确认已就绪。
- 读入示例演示文稿与字体,创建
Presentation对象并加载该文件。 - 使用
Presentation.Slides.get_Item().Comments遍历找到目标批注,再调用Slide.DeleteComment()方法删除。 - 使用
Presentation.SaveToFile()方法保存结果文件。 - 从 VFS 读出结果文件并触发浏览器下载。
删除 PowerPoint 批注的代码如下:
function App() {
const deleteComment = async () => {
// 获取 Spire.Presentation WASM 模块
const pptModule = window.wasmModule?.spirepresentation;
// 检查模块是否就绪
if (!pptModule) {
alert('Spire.Presentation is not ready yet');
return;
}
// 载入示例演示文稿与字体
await window.spire.FetchFileToVFS('示例演示文稿.pptx', "", `${process.env.PUBLIC_URL}/data/`);
await window.spire.FetchFileToVFS('ARIALUNI.TTF', "/Library/Fonts/", `${process.env.PUBLIC_URL}/font/`);
// 创建 Presentation 实例并加载演示文稿
const ppt = new pptModule.Presentation();
ppt.LoadFromFile('示例演示文稿.pptx');
// 取第一张幻灯片的批注集合
const slide = ppt.Slides.get_Item(0);
const comments = slide.Comments;
// 按作者找到目标批注,按对象删除
for (let i = 0; i < comments.length; i++) {
if (comments[i].AuthorName === 'Cral') {
slide.DeleteComment({ comment: comments[i] });
break;
}
}
// 保存结果文件
const outputFileName = '删除批注后.pptx';
ppt.SaveToFile({ file: outputFileName, fileFormat: pptModule.FileFormat.Pptx2013 });
ppt.Dispose();
// 从 VFS 读取生成的文件,触发下载
const fileArray = window.dotnetRuntime.Module.FS.readFile(outputFileName);
const blob = new Blob([fileArray], { type: 'application/vnd.openxmlformats-officedocument.presentationml.presentation' });
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>删除 PowerPoint 批注</h1>
<button onClick={deleteComment}>
删除批注
</button>
</div>
);
}
export default App;
Cral 的批注已从第一张幻灯片移除,Nina 的批注保留:

按作者与文本删除批注
不想先遍历 Comments 时,可以直接传作者与文本。先用 CommentAuthors.FindByName() 取出作者对象,再交给 DeleteComment({ author, text }),两个条件同时成立才会删:
// 取出作者对象,按作者与文本删除
const author = ppt.CommentAuthors.FindByName('Cral')[0];
ppt.Slides.get_Item(0).DeleteComment({ author: author, text: 'Please add a data chart.' });
常见问题
添加批注前为什么要先注册作者
原因:AddComment({ author, ... }) 的 author 要求是 ICommentAuthor 对象,库调用时取它的句柄交给底层,直接传作者名字符串,库无法识别。
解决:先用 Presentation.CommentAuthors.AddAuthor(name, initials) 注册,拿返回的对象去添加批注。文件里已有的作者不必重复注册,用 CommentAuthors.FindByName(name) 取出来即可。
批注的 position 用什么单位
原因:position 是 PointF,坐标以磅为单位,从幻灯片左上角往右、往下量。
解决:按幻灯片尺寸取落点,默认 16:9 的页面是 960 × 540 磅,示例里的 (60, 80) 就在左上角附近。想让批注贴着某个元素,用该元素的 Left、Top 做参考即可。
按作者或文本删除时,会删掉几条批注
原因:只给作者或只给文本时条件比较宽,该页上所有匹配的批注都会删掉。给 { author, text } 时两个条件要同时成立,命中范围小得多。
解决:拿不准范围就先遍历 Slide.Comments,打印 AuthorName 与 Text 核对,再挑合适的重载。只想删一条,用 { comment } 传具体对象最稳。
批注的回复能单独删除吗
原因:Comment.Reply() 追加的回复也作为批注保存在同一页,Comments 里能取到,IsReply 为 true,删除时按作者与文本匹配。
解决:用 DeleteComment({ author, text }) 指定回复的作者和内容就能删掉这条回复,原批注不受影响。
一次能替换或删除多页的批注吗
原因:AddComment、DeleteComment 都挂在 Slide 上,一次只作用于当前页。
解决:遍历 Presentation.Slides,对每一页取 Comments 做同样的处理。批量删时先按条件找齐再删,避免边遍历边改集合。
获取免费许可证
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。







