保护PPT文档是Spire.Presentation组件的主要功能之一。 Spire.Presentation组件支持通过用密码加密,限制访问和标记最终状态的形式,有效地保护演示文稿。另外如果演示文稿在加密的情况下,Spire.Presentation支持解除和修改原有密码。 本节将详细介绍Spire.Presentation保护演示文稿的功能。
用密码加密
开发者可直接使用encrypt方法并指定一个密码来加密文档,这样用户在打开文档时需要输入对应的密码才能对文档进行预览和编辑,很好地保证了文件的安全。
C#
//新建Presentation对象
Presentation presentation = new Presentation();
//加载文档
presentation.LoadFromFile(@"..\..\..\..\..\..\Data\sample.pptx");
//设置密码
presentation.Encrypt("test");
//保存文档
presentation.SaveToFile("encrypt.ppt", FileFormat.PPT);
VB.NET
'新建Presentation对象
Dim presentation As New Presentation()
'加载文档
presentation.LoadFromFile("..\..\..\..\..\..\Data\sample.pptx")
'设置密码
presentation.Encrypt("test")
'保存文档
presentation.SaveToFile("encrypt.ppt", FileFormat.PPT)
限制访问
Spire.Presentation提供了Protect方法保护文档。使用Protect方法保护文档后,用户需输入密码才能进行编辑,如无密码用户可以选择在只读模式下预览,但无法对文档进行编辑、打印等一系列操作。
C#
//新建Presentation对象
Presentation presentation = new Presentation();
//加载文档
presentation.LoadFromFile(@"..\..\..\..\..\..\Data\sample.pptx");
//保护文档
presentation. Protect ("test");
//保存文档
presentation.SaveToFile("readonly.pptx", FileFormat. Pptx2007);
VB.NET
'新建Presentation对象
Dim presentation As New Presentation()
'加载文档
presentation.LoadFromFile("..\..\..\..\..\..\Data\sample.pptx")
'保护文档
presentation.Protect("test")
'保存文档
presentation.SaveToFile("readonly.pptx", FileFormat.Pptx2007)
解除密码
使用RemoveEncryption方法可快速解除加密的PPT文档,有利于快捷操作文档和避免忘记密码的困扰。
C#
// 新建Presentation 对象并加载文档
Presentation presentation = new Presentation();
presentation.LoadFromFile("Presentation1.pptx", "test");
//解除密码
presentation.RemoveEncryption();
//保存文档
presentation.SaveToFile("result.pptx", FileFormat.Pptx2010);
VB.NET
' 新建Presentation 对象并加载文档
Dim presentation As New Presentation()
presentation.LoadFromFile("Presentation1.pptx", "test")
'解除密码
presentation.RemoveEncryption()
'保存文档
presentation.SaveToFile("result.pptx", FileFormat.Pptx2010)
修改密码
修改文档密码需先使用RemoveEncryption方法解除加密,再调用Protect方法重新设置密码以保护文档。
C#
// 新建Presentation 对象并加载文档
Presentation presentation = new Presentation();
presentation.LoadFromFile("Encrypted.pptx",FileFormat.Pptx2010, "oldPassword");
//修改密码
presentation.RemoveEncryption();
presentation.Protect("newPassword");
//保存文档
presentation.SaveToFile("result.pptx", FileFormat.Pptx2010);
VB.NET
' 新建Presentation 对象并加载文档
Dim presentation As New Presentation()
presentation.LoadFromFile("Encrypted.pptx", FileFormat.Pptx2010, "oldPassword")
'修改密码
presentation.RemoveEncryption()
presentation.Protect("newPassword")
'保存文档
presentation.SaveToFile("result.pptx", FileFormat.Pptx2010)
标记为最终状态
通过设置MarkAsFinal文档属性为ture,输出的PPT文档便会标记为最终状态,表示已完成编辑,这是文档的最终版本。
C#
// 新建Presentation 对象并加载文档
Presentation ppt = new Presentation();
ppt.LoadFromFile("sample.pptx",FileFormat.Pptx2010);
//标记为最终状态
ppt.DocumentProperty["_MarkAsFinal"] =true;
//保存文档
ppt.SaveToFile("result.pptx",FileFormat.Pptx2010);
VB.NET
' 新建Presentation 对象并加载文档
Dim ppt As New Presentation()
ppt.LoadFromFile("sample.pptx", FileFormat.Pptx2010)
'标记为最终状态
ppt.DocumentProperty("_MarkAsFinal") = True
'保存文档
ppt.SaveToFile("result.pptx", FileFormat.Pptx2010)