
使用C#程式化填寫PDF表單 (編碼教學)
x509certificate2 可用於獲取現有憑證的重要資訊(有效日期、發行者等)。 IronPDF允許您使用C#對PDF進行數位簽名。 您可以建立一個新文件或簽署現有的PDF文件。這僅需一行程式碼,如下所示的簡單步驟。
您的公司在年度PDF安全性和合規性訂閱上花費太多。考慮IronSecureDoc,它為管理數位簽名、編輯、加密和保護等SaaS服務提供解決方案,僅需一次性付款。立即試用
如何在C#中新增數位PDF簽名
- 安裝C#程式庫以新增PDF數位簽名
- 使用Windows Form以更好地可視化過程
- 將數位簽名和私人密碼傳遞給
PdfSignature類別 - 使用
SignPdfFile方法打開並簽署現有的PDF - 檢查在步驟4中指定目錄中的簽署PDF
步驟1
1. 獲取IronPDF
首先,將IronPDF安裝到您的Visual Studio專案中。 以您認為最方便的方式獲取它,無論是通過DLL下載還是NuGet網站。 存取Visual Studio中的C#程式庫,然後讓我們新增一個簽名。
PM > Install-Package IronPdf
如何教程
2. 理解數位簽名
數位簽名就像是電子駕駛執照或護照,證明您的身份。 數位ID通常包含您的姓名和電子郵件地址,發行機構的名稱,序號,及過期日期。 數位IDs用於憑證安全和數位簽名。 這需要通過Adobe Acrobat建立才能工作。
3. 對PDF進行數位簽名
現在,讓我們看看使用C#建立一個x509certificate2來對PDF進行數位簽名的步驟。
如今,IronPDF程式庫提供了一種簡單的方法來應用簽名,只需一行程式碼即可節省時間和精力。 您可以在開發期間免費使用它來測試您的作品。 然後,決定您的專案。 您將建立一個新文件還是簽署現有的PDF?
在下面的程式碼範例中,使用C#表單允許使用者選擇其所需的PDF,可以單擊一次接收到數位簽名。
應準備一個.pfx file(個人資料交換格式),用於在私鑰的幫助下傳輸憑證。
SignPdfFile(FileName)方法來自PdfSignature類,這是數位簽名的主要方法。 簡單選擇所需的文件。
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上述C#程式碼代表一個Windows Form應用程式,其中:
- 一個按鈕用於開啟文件對話以選擇PDF文件。
- 第二個按鈕使用預先定義的
.pfx文件和密碼觸發所選PDF的簽署。 - 標籤被更新以確認簽署過程的完成。
4. 查看文件簽名
如您所見,以下輸出中,只要選擇了一個PDF文件並點擊導入簽名按鈕,它會成功地對文件進行數位簽名。 使用IronPDF,只需一行程式碼即可完成。


IronPDF是使用C#處理PDF相關任務的理想工具。 IronPDF為開發者提供將PDF文件渲染為圖像的方法,以及從PDF中提取文字和內容。 此外,IronPDF還能在PDF中渲染圖表,使用IronBarcode程式庫新增條碼,通過密碼增強安全性、新增浮水印,甚至程式化處理PDF表單。
程式庫快速存取

Curtis Chau holds a Bachelor’s degree in Computer Science (Carleton University) and specializes in front-end development with expertise in Node.js, TypeScript, JavaScript, and React. Passionate about crafting intuitive and aesthetically pleasing user interfaces, Curtis enjoys working with modern frameworks and creating well-structured, visually appealing manuals.
Related Articles


