C# 设置PPT密码保护(加密、解密、编辑权限)
1、在E-iceblue官网或者Nuget网站上下载Free Spire.Presentation for .NET的安装包后,注意在编辑代码时,添加引用Spire.Presentation.dll到程序。dll文件可在安装路径下的Bin文件夹中获取。

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、调试运行程序后,测试文档将受密码保护。打开文件时,须正确输入密码。如下图:

1、using Spire.Presentation;
namespace EditProtection
{
class Program
{
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、编辑权限设置结果:

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文档将不再受密码保护。
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、运行程序,原有文件中的密码将被修改成新密码。

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、调试程序,生成文档:
