演示文稿外发时,保护方式通常分两种:一种是不让无关的人打开,需要先输密码。另一种是允许查看,但不希望内容被随手改动。前者对应打开密码,后者对应只读设置。桌面软件里可以逐份手动设置,却接不进批量处理的 Web 流程。交给服务端处理,文件又得先传出去。
本文用 Spire.Presentation for JavaScript 在浏览器中实现这四种操作:Encrypt() 设置打开密码、Protect() 将文档设为只读,RemoveEncryption() 与 RemoveProtect() 分别取消这两类保护。它基于 WebAssembly 在浏览器端运行,无需安装 Office,不依赖后端,文件通过虚拟文件系统(VFS)在本地读写。
本文介绍四个核心功能点:
有关安装和项目配置,请参考 React 项目中集成 Spire.Presentation for JavaScript。下面的示例假定 Spire.Presentation 已安装、WebAssembly 模块已初始化。
为 PowerPoint 文档设置密码保护
Spire.Presentation for JavaScript 提供 Presentation.Encrypt() 方法,用于给演示文稿设置打开密码。
具体操作步骤如下:
- 获取 Spire.Presentation 的 WASM 模块,确认已就绪。
- 读入示例演示文稿与字体,创建
Presentation对象并加载该文件。 - 使用
Presentation.Encrypt()方法设置打开密码。 - 使用
Presentation.SaveToFile()方法保存结果文件。 - 从 VFS 读出结果文件并触发浏览器下载。
为 PowerPoint 文档设置密码保护的代码如下:
function App() {
const encryptPowerPoint = 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');
// 设置打开密码,没有密码将无法打开文档
ppt.Encrypt('e-iceblue');
// 保存结果文件
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={encryptPowerPoint}>
设置打开密码
</button>
</div>
);
}
export default App;
设置打开密码后的演示文稿,打开时需要先输入密码:

将 PowerPoint 文档设为只读
Spire.Presentation for JavaScript 还提供 Presentation.Protect() 方法,用于给演示文稿设置只读保护。它写入的是修改密码:文件可以正常打开和浏览,编辑内容时才要求输入密码。
具体操作步骤如下:
- 获取 Spire.Presentation 的 WASM 模块,确认已就绪。
- 读入示例演示文稿与字体,创建
Presentation对象并加载该文件。 - 使用
Presentation.Protect()方法设置修改密码。 - 使用
Presentation.SaveToFile()方法保存结果文件。 - 从 VFS 读出结果文件并触发浏览器下载。
将 PowerPoint 文档设为只读的代码如下:
function App() {
const setReadOnly = 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');
// 设置修改密码,未输入密码时文档以只读方式打开
ppt.Protect('e-iceblue');
// 保存结果文件
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={setReadOnly}>
设置只读
</button>
</div>
);
}
export default App;
设为只读后的演示文稿,打开时提示输入密码才能编辑:

取消 PowerPoint 文档的密码保护
Spire.Presentation for JavaScript 提供 Presentation.RemoveEncryption() 方法,用于移除打开密码。
具体操作步骤如下:
- 获取 Spire.Presentation 的 WASM 模块,确认已就绪。
- 读入加密的演示文稿与字体,创建
Presentation对象,加载时一并传入密码。 - 使用
Presentation.RemoveEncryption()方法取消密码保护。 - 使用
Presentation.SaveToFile()方法保存结果文件。 - 从 VFS 读出结果文件并触发浏览器下载。
取消 PowerPoint 文档密码保护的代码如下:
function App() {
const removePasswordProtection = 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({ file: '加密的演示文稿.pptx', password: 'e-iceblue' });
// 移除打开密码
ppt.RemoveEncryption();
// 保存结果文件
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={removePasswordProtection}>
取消密码保护
</button>
</div>
);
}
export default App;
取消打开密码后的演示文稿,无需密码即可打开:

取消 PowerPoint 文档的只读设置
Spire.Presentation for JavaScript 还提供 Presentation.RemoveProtect() 方法,用于移除只读设置。
具体操作步骤如下:
- 获取 Spire.Presentation 的 WASM 模块,确认已就绪。
- 读入只读的演示文稿与字体,创建
Presentation对象并加载该文件。 - 使用
Presentation.RemoveProtect()方法取消只读设置。 - 使用
Presentation.SaveToFile()方法保存结果文件。 - 从 VFS 读出结果文件并触发浏览器下载。
取消 PowerPoint 文档只读设置的代码如下:
function App() {
const removeReadOnly = 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');
// 移除只读设置
ppt.RemoveProtect();
// 保存结果文件
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={removeReadOnly}>
取消只读
</button>
</div>
);
}
export default App;
取消只读设置后的演示文稿,可以直接编辑:

常见问题
Encrypt() 与 Protect() 有什么区别
原因:两个方法写入的密码用途不同。Encrypt() 设置打开密码,没有密码无法加载文件。Protect() 设置修改密码,文件仍可打开和浏览,编辑内容时才要求输入密码。
解决:限制访问用 Encrypt(),只防止内容被改动用 Protect()。取消时也对应两个方法:RemoveEncryption() 去掉打开密码,RemoveProtect() 去掉只读设置。
只读的演示文稿能直接读取吗
可以。ppt.LoadFromFile('只读演示文稿.pptx') 不需要密码——Protect() 限制的是编辑,不是读取。加密过的文件则不同,加载时要一并传入打开密码:
// 只读文件:直接加载
ppt.LoadFromFile('只读演示文稿.pptx');
// 加密文件:加载时带上打开密码
ppt.LoadFromFile({ file: '加密的演示文稿.pptx', password: 'e-iceblue' });
忘记打开密码还能取消密码保护吗
不能。RemoveEncryption() 的前提是文件已经以正确密码成功加载,拿不到打开密码就无法读入文档。RemoveProtect() 是例外:只读设置不接收原密码,免密加载后直接调用即可。
取消只读设置需要原密码吗
不需要。RemoveProtect() 没有密码参数,加载只读文件后调用一次,保存出的副本就不再要求输入修改密码。
设置保护后还能保存成 PPTX 吗
可以。示例统一用 FileFormat.Pptx2013 保存,加密与只读设置都会写入结果文件中。加密后的文件不再是普通 ZIP 结构,需要按加密文档的方式加载。
获取免费许可证
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。







