跳至页脚内容
使用IRONPDF

x509certificate2以编程方式在PDF上添加数字签名

x509certificate2 可用于获取有关现有证书的重要信息(有效日期、发行者等)。 IronPDF 允许您使用 C# 对 PDF 进行数字签名。 您可以创建新文档或签署现有的 PDF 文件。只需要一行代码即可实现,如下简单步骤所示。

role="alert">您的公司在年度 PDF 安全和合规订阅上花费过多。考虑 IronSecureDoc,它提供数字签名、编辑、加密和保护等 SaaS 服务管理解决方案,全部只需一次性付款。立即试用
class="hsg-featured-snippet">

如何在 C# 中添加数字 PDF 签名

  1. 安装 C# 库以为 PDF 添加数字签名
  2. 使用 Windows 窗体更好地可视化流程
  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 进行数字签名

现在,让我们看看如何创建 x509certificate2 来使用 C# 对 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 窗体应用,其中:

  • 一个按钮用于打开文件对话框以选择 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](/) 是处理 PDF 相关任务的完美工具,使用 C#。 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 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。