c# - 保护 PDF 文件的密码

标签 c#

我有以下内容:

  • 每天创建 PDF 文件的例程 X。
  • 例程 Y,将此文件附加到 Outlook 电子邮件并将其发送给收件人。

以上都是VBA。它们是从 C# 控制台应用程序调用的。

创建 PDF 后,我需要对其进行密码保护。 在不购买第三方软件的情况下通过 VBA 执行此操作非常复杂。

使用 C# 的最简单解决方案是什么?

(我怀疑我们花费的金额与答案的复杂性之间存在反比关系!)

最佳答案

PDFSharp应该能够使用密码保护 PDF 文件:

// Open an existing document. Providing an unrequired password is ignored.
PdfDocument document = PdfReader.Open(filename, "some text");

PdfSecuritySettings securitySettings = document.SecuritySettings;

// Setting one of the passwords automatically sets the security level to 
// PdfDocumentSecurityLevel.Encrypted128Bit.
securitySettings.UserPassword  = "user";
securitySettings.OwnerPassword = "owner";

// Don't use 40 bit encryption unless needed for compatibility reasons
//securitySettings.DocumentSecurityLevel = PdfDocumentSecurityLevel.Encrypted40Bit;

// Restrict some rights.
securitySettings.PermitAccessibilityExtractContent = false;
securitySettings.PermitAnnotations = false;
securitySettings.PermitAssembleDocument = false;
securitySettings.PermitExtractContent = false;
securitySettings.PermitFormsFill = true;
securitySettings.PermitFullQualityPrint = false;
securitySettings.PermitModifyDocument = true;
securitySettings.PermitPrint = false;

// Save the document...
document.Save(filename);

引用:
http://www.pdfsharp.net/wiki/ProtectDocument-sample.ashx

关于c# - 保护 PDF 文件的密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12383409/

相关文章:

c# - dll 中的 WPF 自定义控件

c# - ASP 核心 HttpClient.Get 从容器到本地主机站点失败

c# - 在什么时候评估 Thread.CurrentThread?

javascript - 内容安全政策

c# - 使用 2 个 DBContext 开始事务

c# - 在运行存储过程 C# 之前检查文本框或日期选择器是否已更改

c# - 将数据从 View 绑定(bind)到 ItemsPanelTemplate

c# - EF Core 为用户添加角色

c# - 在不实现的情况下使用 Linq Indexof StartIndex

c# - WebAPI - 统一来自 ApiController 和 OAuthAuthorizationServerProvider 的错误消息格式