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

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

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

為了在 PDF 檔案中加入數位簽名,我們將使用 Aspose.PDF for .NET API,這是一個功能豐富、強大且易於使用的文件操作 API,適用於 net 平台。開啟 NuGet 套件管理器,搜尋 Aspose.PDF 並安裝。您也可以在套件管理器控制台中使用下列命令。

Package Manager Console

PM > Install-Package Aspose.PDF

通過C#將數位簽名添加到 PDF


您需要 Aspose.PDF for .NET 才能在您的環境中測試程式碼。

  1. 使用 Document 實例載入 PDF。
  2. 載入所有 PDF 文件,並指定其完整路徑。
  3. 設定文字樣式,例如字體、字號、顏色等。
  4. 儲存 PDF 文件,您將獲得已插入簽署的文件。
  5. 使用 PdfFileSignature.VerifySigned() 方法驗證簽章的有效性。

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

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

Input file:

File not added

Certificate file:

File not added

Output format:

Output file:

{
    var inputFile = Path.Combine(dataDir, "DigitallySign.pdf");
    var outputFile = Path.Combine(dataDir, "DigitallySign_out.pdf");

    using var pdfDocument = new Aspose.Pdf.Document(inputFile);
    using var signature = new Aspose.Pdf.Facades.PdfFileSignature(pdfDocument);
    const string Pfx = @"c:\demo\test.pfx";
    var pkcs = new Aspose.Pdf.Forms.PKCS7(Pfx, "Pa$$w0rd2025"); // Use PKCS7/PKCS7Detached objects
    signature.Sign(1, true, new System.Drawing.Rectangle(300, 100, 400, 200), pkcs);
    signature.Save(outputFile);
}