通过 C# 向 PDF 添加数字签名

使用服务器端 Aspose.PDF 为 .NET API 生成和验证数字签名。原生和高性能库

如何使用 .NET 工具向 PDF 文件添加数字签名

为了在 PDF 文件中添加数字签名,我们将使用 Aspose.PDF for .NET API,这是一款适用于 net 平台的功能丰富、功能强大且易于使用的文档处理 API。打开 NuGet 软件包管理器,搜索 aspose.pdf 然后安装。您也可以在软件包管理器控制台中使用以下命令。

Package Manager Console

PM > Install-Package Aspose.PDF

通过 C# 将数字签名添加到 PDF


你需要 Aspose.PDF for .NET 才能在你的环境中试用这些代码。

  1. 使用 “文档” 实例加载 PDF。
  2. 加载所有具有完整路径的 PDF 文件。
  3. 设置文本的样式,如字体、字体大小、颜色等。
  4. 保存PDF文件,您将获得插入签名的文档。
  5. 要验证签名的有效性,请使用 pdfFilesignature.verifySignature.verifySigned () 方法。

使用-C# 向 PDF 文件添加电子签名

此示例代码说明如何为 PDF 页面签名-C#

Input file:

File not added

Certificate file:

File not added

Output format:

Output file:

public static void SignDocument()
{
    string inFile = System.IO.Path.Combine(_dataDir,"DigitallySign.pdf");
    string outFile = System.IO.Path.Combine(_dataDir,"DigitallySign_out.pdf");
    using (Document document = new Document(inFile))
    {
        using (PdfFileSignature signature = new PdfFileSignature(document))
        {
            PKCS7 pkcs = new PKCS7(@"C:\Keys\test.pfx", "Pa$$w0rd2020"); // Use PKCS7/PKCS7Detached objects
            signature.Sign(1, true, new System.Drawing.Rectangle(300, 100, 400, 200),pkcs);
            // Save output PDF file
            signature.Save(outFile);
        }
    }
}