跳過到頁腳內容
使用IRONPDF

x509certificate2程式化添加數碼簽名到PDF

x509certificate2 可以用來獲取現有證書的重要信息(有效日期、發行者等)。 IronPDF 允許您使用 C# 對 PDF 進行數位簽名。 您可以創建新文件或簽署現有的 PDF 文件。它只需要一行代碼,如下簡單步驟所示。

role="alert">您的企業在 PDF 安全和合規的年度訂閱上花費太多。考慮使用 IronSecureDoc,它為數位簽名、遮蔽、加密和保護等 SaaS 服務提供管理解決方案,只需一次性付款。立即試用
class="hsg-featured-snippet">

如何在 C# 中添加數位 PDF 簽名

  1. 安裝 C# 庫以添加數位簽名至 PDFs
  2. 使用 Windows Form 更好地可視化過程
  3. 將數位簽名和私人密碼傳遞給 PdfSignature 類別
  4. 使用 SignPdfFile 方法打開並簽署現有的 PDF
  5. 檢查在步驟 4 中指定目錄中的簽名 PDF

class="tutorial-segment-title">步驟 1

1. 獲取 IronPDF

首先,將 IronPDF 安裝到您的 Visual Studio 專案中。 Get it in whichever way is easier for you, either from DLL download or on the NuGet website. 在 Visual Studio 中訪問 C# 庫,然後讓我們添加一個簽名。

# Product Installation using NuGet
nuget install IronPdf
# Product Installation using NuGet
nuget install IronPdf
SHELL


class="tutorial-segment-title">操作指南

2. 理解數位簽名

數位簽名就像電子駕駛執照或護照,證明您的身份。 數位 ID 通常包含您的姓名和電子郵件地址、發行機構的名稱、序列號和到期日期。 數位 ID 用於證書安全和數位簽名。 這需要使用 Adobe Acrobat 創建才能運作。


3. 對 PDF 進行數位簽名

現在,讓我們看看使用 C# 創建 x509certificate2 來對 PDF 進行數位簽名的步驟。

今天,IronPDF 庫提供了一種簡單的方式來應用簽名,只需一行代碼即可節省時間和精力。 您可以在開發過程中免費使用它來測試您的工作。 然後,決定您的專案。 您是要創建一個新文件還是簽署現有的 PDF?

在下面的代碼示例中,使用了 C# 表單允許用戶選擇他們想要的 PDF,可以通過單次點擊獲得數位簽名。

應準備一個 .pfx 文件(個人信息交換格式),這用來在私鑰幫助下傳輸證書。

The SignPdfFile(FileName) method from the PdfSignature class is the main method for a digital signature. 簡單地選擇所需的文件。

using System.Drawing;
using System.Windows.Forms;
using IronPdf;

namespace DigitalSign
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent(); 
        }

        private void button1_Click(object sender, System.EventArgs e)
        {
            // Open a dialog to select the desired PDF file
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = openFileDialog1.FileName; // Display selected PDF path in the textbox
            }
        }

        private void button2_Click(object sender, System.EventArgs e)
        {
            // Use PdfSignature method to digitally sign the selected PDF
            new PdfSignature("Ironpdf.pfx", "123456").SignPdfFile(textBox1.Text);

            // Provide user feedback that signing is complete
            label3.Text = "Completed!";
            label3.BackColor = Color.LightGreen;
            label3.ForeColor = Color.Black;
        }
    }
}
using System.Drawing;
using System.Windows.Forms;
using IronPdf;

namespace DigitalSign
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent(); 
        }

        private void button1_Click(object sender, System.EventArgs e)
        {
            // Open a dialog to select the desired PDF file
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = openFileDialog1.FileName; // Display selected PDF path in the textbox
            }
        }

        private void button2_Click(object sender, System.EventArgs e)
        {
            // Use PdfSignature method to digitally sign the selected PDF
            new PdfSignature("Ironpdf.pfx", "123456").SignPdfFile(textBox1.Text);

            // Provide user feedback that signing is complete
            label3.Text = "Completed!";
            label3.BackColor = Color.LightGreen;
            label3.ForeColor = Color.Black;
        }
    }
}
Imports System.Drawing
Imports System.Windows.Forms
Imports IronPdf

Namespace DigitalSign
	Partial Public Class Form1
		Inherits Form

		Public Sub New()
			InitializeComponent()
		End Sub

		Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
			' Open a dialog to select the desired PDF file
			If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
				textBox1.Text = openFileDialog1.FileName ' Display selected PDF path in the textbox
			End If
		End Sub

		Private Sub button2_Click(ByVal sender As Object, ByVal e As System.EventArgs)
			' Use PdfSignature method to digitally sign the selected PDF
			Call (New PdfSignature("Ironpdf.pfx", "123456")).SignPdfFile(textBox1.Text)

			' Provide user feedback that signing is complete
			label3.Text = "Completed!"
			label3.BackColor = Color.LightGreen
			label3.ForeColor = Color.Black
		End Sub
	End Class
End Namespace
$vbLabelText   $csharpLabel

上面的 C# 代碼代表了一個 Windows Form 應用,在其中:

  • 一個按鈕用於打開文件對話框以選擇 PDF 文件。
  • 第二個按鈕觸發使用預先定義的 `.pfx` 文件和密碼簽署選定的 PDF。
  • 標籤被更新以確認簽署過程的完成。

## 4. 審核文件簽名 如您在下面的輸出中所見,只要選擇了一個 PDF 文件並單擊 **導入簽名** 按鈕,即成功數位簽署了文件。 使用 IronPDF,只需要一行代碼。 ![x509certificate2 對 PDF 進行程序化的數位簽名,第 1 圖:](/img/faq/add-digital-signature-pdf-programmatically/digital-sign.png) **** ![x509certificate2 對 PDF 進行程序化的數位簽名,第 2 圖:](/img/faq/add-digital-signature-pdf-programmatically/digital-sign2.png) **** [IronPDF](/) 是使用 C# 進行 PDF 相關任務的完美工具。 IronPDF offers developers methods to [render PDF documents into images](/how-to/rasterize-pdf-to-images/) and [extract text and content](https://ironpdf.com/how-to/extract-text-and-images/) from a PDF. Additionally, IronPDF is capable of rendering charts in PDFs, adding barcodes using the IronBarcode library, [enhancing security with passwords](/examples/security-and-metadata/), [watermarking](/examples/pdf-watermarking/), and even [handling PDF forms](/how-to/create-forms/) programmatically.

class="tutorial-segment-title">庫快速訪問

class="tutorial-section">
class="row">
class="col-sm-4">
class="tutorial-image"> Documentation related to class=庫快速訪問" class="img-responsive add-shadow img-responsive img-popup" src="/img/svgs/documentation.svg" loading="lazy">
class="col-sm-8">

API 參考

閱讀 IronPDF 文檔和完整的功能列表。

API 參考

常見問題解答

如何使用 C# 以程式設計方式為 PDF 新增數位簽章?

若要在 C# 中為 PDF 新增數位簽名,請使用 IronPDF。首先,透過 NuGet 安裝 IronPDF 或下載其 DLL 檔案。然後,使用 `x509certificate2` 類別處理證書,並使用 `PdfSignature` 類別透過 `SignPdfFile` 方法,使用 `.pfx` 檔案和密碼套用簽署。

數位簽名在PDF文件中有什麼作用?

PDF 中的數位簽名透過驗證寄件者的身份,提供額外的安全保障,類似於電子護照。它們確保文件的真實性和完整性,防止未經授權的修改。

使用 IronPDF 簽署 PDF 文件是否容易?

是的,使用 IronPDF 對 PDF 檔案進行簽名非常簡單。只需使用 `SignPdfFile` 方法編寫一行程式碼,即可輕鬆將數位簽章整合到 PDF 檔案中。

`x509certificate2` 類別在數位簽章中扮演什麼角色?

C# 中的 `x509certificate2` 類別用於管理和處理 X.509 證書,這些證書對於應用數位簽章至關重要。它提供有關證書的信息,並與 IronPDF 配合使用以對 PDF 文件進行簽名。

我可以在購買前免費試用IronPDF嗎?

是的,在開發過程中,使用者可以免費使用 IronPDF 來測試其功能,包括數位簽名,然後再做購買決定。

我應該使用哪種文件格式來傳輸包含私鑰的數位簽章憑證?

若要傳輸帶有私鑰的證書,請使用 `.pfx` 檔案格式,即個人資訊交換格式。

如何在我的 Visual Studio 專案中安裝 IronPDF?

您可以透過直接下載 DLL 或使用 NuGet 命令 `nuget install IronPdf` 將 IronPDF 安裝到您的 Visual Studio 專案中。

`SignPdfFile` 方法的用途是什麼?

IronPDF 中的 `SignPdfFile` 方法用於使用指定的憑證和私密金鑰對 PDF 檔案進行數位簽名,從而增強文件的安全性。

IronPDF在新增數位簽章時是否完全支援.NET 10?

是的。 IronPDF 完全相容於 .NET 10,所有數位簽章功能(包括 `PdfSignature`、`x509certificate2`、`Sign` 和 `SignPdfFile` 方法)無需額外配置即可正常運作。 IronPDF 不僅支援 .NET 10,還支援許多其他 .NET 版本(例如 .NET 9、8、7 等)。

在使用 IronPDF 的數位簽章功能之前,是否有任何先決條件或 .NET 版本要求?

IronPDF 的數位簽章功能需要 .NET 5 或更高版本,包括 .NET 10、.NET 9、.NET Core 和 .NET Standard。對於早期框架或不受支援的版本,該程式庫可能無法提供完全相容性,或者可能需要額外的依賴項。

Curtis Chau
技術作家

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。