使用“通過C#在 PDF 中添加數字簽名

本機和高性能 PDF 文件數位簽名生成和驗證使用伺服器端 Aspose.PDF用於 .NET API,而無需使用包括 Adobe PDF 在內的任何軟體 - C#。

如何使用 .NET 庫將數位簽名添加到 PDF 檔

為了在PDF檔中添加數位簽名,我們將使用[Aspose.PDF用於.NET](https://products.aspose.com/pdf/net)API,這是一個功能豐富,功能強大且易於使用的文檔操作API,適用於 net 平臺。打開 [NuGet](https://www.nuget.org/packages/aspose.pdf) 包管理器,搜索“.PDF”並安裝。您也可以從程式包管理器主控台使用以下命令。

Package Manager Console

PM > Install-Package Aspose.PDF

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


您需要使用 [Aspose.PDF for .NET](https://releases.aspose.com/pdf/net) 來嘗試環境中的代碼。

  1. 載入包含文件實例的 PDF。
  2. 載入所有具有完整路徑的PDF檔。 設置文字的樣式,如字體,字體大小,顏色等。 保存PDF檔,您將獲得插入簽名的文件。
  3. 要驗證簽名的有效性,請使用簽名()方法。

使用 - C#將電子簽名添加到 PDF 檔。

<% signature.code-block.subtitle %>

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);
        }
    }
}