在微软Office Word中,我们可以设置Word权限,以保护文档。常规选项有:用密码进行加密和限制编辑。前者是指需要使用密码才能打开此文档,后者指控制其他人可以对此文档所做的更改类型。限制编辑有四个选项:修订;批注;填写窗体;不允许任何更改(只读)。这四个选项规定了仅允许在文档中进行此类型的编辑。
此文将详细介绍使用我司免费产品Free Spire.Doc,在.NET平台上使用C#对Word文档进行上述保护设置。该产品也支持对已加密的文档进行解密。有需要的朋友,可以从以下三个资源站点下载获取Free Spire.Doc:E-iceblue官网,CSDN下载,和Nuget。下载完成后,请将bin文件夹的.Dll用于Visual Studio的引用。使用Spire.Doc可独立创建Word文档,此处微软Word仅用于查看效果。
步骤一:加载Word示例文档。
Document document = new Document();
document.LoadFromFile("S.docx");
步骤二:按所需对Word进行加密,解密,限制编辑处理。
- 加密文档
document.Encrypt("eiceblue");
- 解密文档
document.RemoveEncryption();
- 限制编辑:不允许任何更改(只读),设置解除限制编辑的密码
document.Protect(ProtectionType.AllowOnlyReading, "132");
- 限制编辑:只允许填写窗体,设置解除限制编辑的密码
document.Protect(ProtectionType.AllowOnlyFormFields, "132");
- 限制编辑:只允许批注,设置解除限制编辑的密码
document.Protect(ProtectionType.AllowOnlyComments, "132");
- 限制编辑:只允许修订,设置解除限制编辑的密码
document.Protect(ProtectionType.AllowOnlyRevisions, "132");
步骤三:保存文档,并启动查看效果。
document.SaveToFile("ProtectWord.docx");
System.Diagnostics.Process.Start("ProtectWord.docx");
效果示例:限制编辑,只读
全部代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spire.Doc;
namespace 中文博客0810
{
class Program
{
static void Main(string[] args)
{
Document document = new Document();
document.LoadFromFile("S.docx");
document.Protect(ProtectionType.AllowOnlyReading, "132");
//document.Protect(ProtectionType.AllowOnlyFormFields, "132");
//document.Protect(ProtectionType.AllowOnlyComments, "132");
//document.Protect(ProtectionType.AllowOnlyRevisions, "132");
//document.RemoveEncryption();
//document.Encrypt("eiceblue");
document.SaveToFile("ProtectWord.docx");
System.Diagnostics.Process.Start("ProtectWord.docx");
}
}
}
*感谢大家的阅读,欢迎大家测试使用我司产品。
如需技术支持,请邮件联系我们的 技术支持团队 support@e-iceblue.com。*