C# 设置PPT密码保护(加密、解密、编辑权限)
保护一些重要文档,我们可以通过设置文件的打开密码或者设置文件编辑的权限来有效保护文档内容。此外,对于不再需要密码保护或者需要定期更换密码的文档,我们也可以根据需要来设置。此经验就将分享操作ppt文档密码保护的方法。
工具/原料
Free Spire.Presentation for .NET 3.3 (社区版)
Visual Studio
dll引用
1、在E-iceblue官网或者Nuget网站上下载Free Spire.Presentation for .NET的安装包后,注意在编辑代码时,添加引用Spire.Presentation.dll到程序。dll文件可在安装路径下的Bin文件夹中获取。
1.设置PPT文件打开密码
1、using Spire.Presentation;namespace Encrypt{ class Program { static void Main(string[] args) { //新建Presentation对象 Presentation presentation = new Presentation(); //加载文档 presentation.LoadFromFile("sample.pptx"); //设置密码 presentation.Encrypt("test"); //保存并打开文档 presentation.SaveToFile("encrypt.pptx", FileFormat.Pptx2010); System.Diagnostics.Process.Start("encrypt.pptx"); } }}
2、调试运行程序后,测试文档将受密码保护。打开文件时,须正确输入密码。如下图:
2. 设置PPT文档编辑权限
1、using Spire.Presentation;namespace EditProtection{ class Progra罪焐芡拂m { static void Main(string[] args) { //新建Presentation对象 Presentation presentation = new Presentation(); //加载文档 presentation.LoadFromFile("sample.pptx"); //设置启用编辑PPT的密码 presentation.Protect("keytoedit"); //保存并打开文档 presentation.SaveToFile("readonly.pptx", FileFormat.Pptx2010); System.Diagnostics.Process.Start("readonly.pptx"); } }}
2、编辑权限设置结果:
3.删除文件保护密码
1、using Spire.Presentation;namespace RemoveEncryption{ class Program { static void Main(string[] args) { // 新建Presentation 对象并加载文档 Presentation presentation = new Presentation(); presentation.LoadFromFile("encrypt.pptx", "test"); //解除密码 presentation.RemoveEncryption(); //保存文档 presentation.SaveToFile("result.pptx", FileFormat.Pptx2010); System.Diagnostics.Process.Start("result.pptx"); } }}
2、调试运行程序后,原来有密码保护的PPT文档将不再受密码保护。
4.修改原有密码
1、using Spire.Presentation;namespace Modify{ class Program { static void Main(string[] args) { // 新建Presentation 对象并加载文档 Presentation presentation = new Presentation(); presentation.LoadFromFile("encrypt.pptx", FileFormat.Pptx2010, "test"); //删除旧密码,设置新密码 presentation.RemoveEncryption(); presentation.Protect("newpassword"); //保存并打开文档 presentation.SaveToFile("result.pptx", FileFormat.Pptx2010); System.Diagnostics.Process.Start("result.pptx"); } }}
2、运行程序,原有文件中的密码将被修改成新密码。
5.标记文档最终编辑状态
1、using Spire.Presentation;namespace Down{ class Program { static void Main(string[] args) { // 新建Presentation 对象并加载文档 Presentation ppt = new Presentation(); ppt.LoadFromFile("sample.pptx", FileFormat.Pptx2010); //标记为最终状态 ppt.DocumentProperty["_MarkAsFinal"] = true; //保存并打开文档 ppt.SaveToFile("output.pptx", FileFormat.Pptx2010); System.Diagnostics.Process.Start("output.pptx"); } }}
2、调试程序,生成文档: