x509certificate2以编程方式在PDF上添加数字签名
x509certificate2 可用于获取有关现有证书的重要信息(有效日期、发行者等)。 IronPDF 允许您使用 C# 对 PDF 进行数字签名。 您可以创建新文档或签署现有的 PDF 文件。只需要一行代码即可实现,如下简单步骤所示。
如何在 C# 中添加数字 PDF 签名
- 安装 C# 库,为 PDF 添加数字签名。
- 使用Windows窗体更好地可视化过程
- 将数字签名和私人密码传递给
PdfSignature类 - 使用
SignPdfFile方法打开并签署现有 PDF 文件 - 检查步骤4中指定目录下的已签署PDF
步骤1
1. 获得 IronPDF
首先,将 IronPDF 安装到您的 Visual Studio 项目中。 从 DLL 下载或在 NuGet 网站上获取,以您觉得方便的方式。 在 Visual Studio 中访问 C# 库,让我们添加一个签名。
# Product Installation using NuGet
nuget install IronPdf# Product Installation using NuGet
nuget install IronPdf如何使用教程
2. 了解数字签名
数字签名类似于电子驾照或护照,用于证明您的身份。 数字 ID 通常包含您的姓名和电子邮件地址、颁发组织的名称、序列号和到期日期。 数字 ID 用于 证书安全和数字签名。 这需要使用 Adobe Acrobat 创建才能生效。
3. 对 PDF 进行数字签名
现在,让我们看看如何创建 x509certificate2 来使用 C# 对 PDF 进行数字签名的步骤。
如今,IronPDF 库提供了一种简单的应用签名的方法,只需一行代码即可节省时间和精力。 您可以在开发期间免费使用它来测试您的工作。 然后,决定您的项目。 您是要创建新文档还是要签署现有 PDF?
在下面的代码示例中,使用了 C# 窗体来允许用户选择其所需的 PDF,只需单击即可接收数字签名。
应准备一个 .pfx 文件(个人信息交换格式),用于在私钥的帮助下传输证书。
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;
}
}
}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 窗体应用程序,其中:
- 按钮用于打开选择 PDF 文件的文件对话框。
- A second button triggers the signing of the selected PDF using a predefined `.pfx` file and password.
- 更新标签以确认签名过程的完成。
4. 审查文档签名
正如您在下面的输出中看到的,只要选择了 PDF 文件并点击导入签名按钮,就成功地对文档进行数字签名。 使用 IronPDF,只需一行代码即可。


IronPDF 是处理 PDF 相关任务的完美工具,使用 C#。 IronPDF 为开发人员提供了 将 PDF 文档渲染为图像和从 PDF 提取文本和内容的方法。 此外,IronPDF 能够在 PDF 中渲染图表,使用 IronBarcode 库添加条码,增强安全性与密码,添加水印,甚至以编程方式处理 PDF 表单。
图书馆快速访问
常见问题解答
如何在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。对于早期框架或不受支持的版本,该库可能无法提供完全兼容性,或者可能需要额外的依赖项。






