PDFsharp用法:取消文档保护

此示例演示如何取消对文档的保护(如果知道密码)。

// 获取示例PDF文件的新副本。
// 此示例中的密码为“user”和“owner”。
string filename = "HelloWorld (protected).pdf";
File.Copy(Path.Combine("../../../../PDFs/", filename), 
  Path.Combine(Directory.GetCurrentDirectory(), filename), true);
 
PdfDocument document;
 
// 打开文档将失败,密码无效。
try
{
  document = PdfReader.Open(filename, "invalid password");
}
catch (Exception ex)
{
  Debug.WriteLine(ex.Message);
}
 
// 可以指定委托,如果文档需要密码。如果要修改文档,必须提供所有者密码
document = PdfReader.Open(filename, PdfDocumentOpenMode.Modify,
  new PdfPasswordProvider(PasswordProvider));
 
// 用用户密码打开文档。
document = PdfReader.Open(filename, "user", PdfDocumentOpenMode.ReadOnly);
 
// 使用属性hasOwnerPermissions确定是否使用了密码
// 是用户或所有者密码。在这两种情况下,PDFSharp都提供
// 访问PDF文档。由使用PDFSharp的程序员决定
// 尊重访问权。PDFSharp不尝试保护文档
// 因为这对开放源码库没有什么意义。
bool hasOwnerAccess = document.SecuritySettings.HasOwnerPermissions;
 
// 用所有者密码打开文档。
document = PdfReader.Open(filename, "owner");
hasOwnerAccess = document.SecuritySettings.HasOwnerPermissions;
 
// 使用所有者密码打开的文档完全不受保护,可以修改。
XGraphics gfx = XGraphics.FromPdfPage(document.Pages[0]);
gfx.DrawString("Some text...",
  new XFont("Times", 12), XBrushes.Firebrick, 50, 100);
 
// 修改后的文档将在不应用任何保护的情况下保存。
PdfDocumentSecurityLevel level = document.SecuritySettings.DocumentSecurityLevel;
 
//如果要将其保存为受保护的,则必须设置documentsecuritylevel或应用新密码。在当前实现中,旧密码不会自动重用。有关更多信息,请参阅“ProtectDocument”示例。
 
// 保存
document.Save(filename);
// 预览
Process.Start(filename);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值