如何在 C# 中为 PDF 添加数字签名

数字电子签名是一种验证文档真实性的方法。我们将在本文中创建分步说明以在 C# 中向 PDF 添加数字签名。我们还将按照综合步骤创建一个在 C#** 中使用**数字签名的示例。此处概述了使用数字签名对 PDF 文档进行签名的整个方法以及示例代码。

在 C# 中向 PDF 添加数字签名的步骤

  1. 从 NuGet 包管理器安装 GroupDocs.Signature for .NET
  2. 添加对所需命名空间的引用以实现数字签名
  3. 创建 Signature 类的对象并加载输入的 PDF 文档
  4. 使用所需的证书及其密码创建 DigitalSignOptions 类的实例
  5. 调用 Signature 类的 Sign 方法并将输出的 PDF 文件与 DigitalSignOptions 一起传递

通过以上几点,您可以用几行代码快速创建 C# 数字签名 功能。首先,从 NuGet 包管理器设置所需的包,并在代码中包含必要的命名空间。在下一步中,初始化 Signature 类以加载源 PDF,创建 DigitalSignOptions 类的实例并传递证书文件。最后,通过调用 Sign 方法将数字签名添加到 PDF 文档并存储在光盘上。

在 C# 中向 PDF 添加数字签名的代码

using System;
using System.Collections.Generic;
using System.IO;
using GroupDocs.Signature;
using GroupDocs.Signature.Domain;
using GroupDocs.Signature.Options;
namespace AddDigitalSignatureToPdfInCSharp
{
class Program
{
public static void Main(string[] args) // Main function to add Digital signature to PDF using C#
{
// Remove the watermark in output PDF document by adding license
string licensePath = "GroupDocs.Signature.lic";
GroupDocs.Conversion.License lic = new GroupDocs.Conversion.License();
lic.SetLicense(licensePath);
// load the source PDF for sign with digital signature
Signature signature = new Signature("sample.pdf");
// initialize digital option with certificate file path
DigitalSignOptions options = new DigitalSignOptions("MrSmithSignature.pfx")
{
// set signature position
Left = 100,
Top = 100,
// set
Password = "1234567890"
};
// sign document to file
signature.Sign("signed.pdf", options);
Console.WriteLine("Done");
}
}
}

按照前面部分提供的综合说明,我们已经演示了 c#* 中的*数字签名实现。我们为签名设置了一些属性,包括它的左侧和顶部位置。另一方面,此示例代码可用作添加联系人、原因、位置、可见性等属性的起点。

在本主题中,我们讨论了使用 C# 在 PDF 中添加数字签名的过程。最近,我们在 如何在 C# 中使用条码签名对 PDF 文档进行签名 上写了一篇文章,请查看它以获取更多信息。

 简体中文