如何使用IronPDF在C#中签署PDF文件 | IronPDF教程

A Developer's Guide to Digitally Signing PDFs with C

This article was translated from English: Does it need improvement?
Translated
View the article in English

本综合指南向 C# 开发人员展示了如何使用 IronPDF 对 PDF 进行数字签名,内容涵盖基于证书的签名、可视化印章以及交互式表单字段,以确保文档的真实性与安全性。

在 PDF 文档中添加签名是许多应用程序的常见需求,但"签名"一词可能具有不同的含义。 对某些人而言,这涉及使用安全证书应用防篡改的数字签名。 对于其他场景,可能是将手写签名图像叠加到文档上,或是添加交互式表单字段供用户进行电子签名。

本指南为 C# 开发人员提供了全面的操作指南,介绍如何使用 IronPDF for .NET 库完成所有这些任务。 我们将涵盖从应用安全的 X509Certificate2 数字签名,到添加图形签名及创建交互式签名字段等所有内容,确保您的 PDF 文档真实、安全且 Professional。

快速入门:使用 IronPdf 轻松对 PDF 进行数字签名

立即开始使用 IronPDF,通过简单直观的流程对您的 PDF 文档进行数字签名。 本示例演示了如何使用 .pfx 证书对 PDF 文件进行身份验证和签名,以确保文档的完整性和真实性。 请按照以下步骤将数字签名无缝集成到您的应用程序中。

  1. 使用 NuGet 包管理器安装 https://www.nuget.org/packages/IronPdf

    PM > Install-Package IronPdf
  2. 复制并运行这段代码。

    new IronPdf.Signing.PdfSignature("certificate.pfx", "password").SignPdfFile("input.pdf");
  3. 部署到您的生产环境中进行测试

    通过免费试用立即在您的项目中开始使用IronPDF

    arrow pointer

如何使用证书对 PDF 文件添加数字签名?

您可以使用数字证书文件(例如 .pfx.p12)对 PDF 文档进行数字签名,以确保文档的真实性和完整性。 此流程可确保文件自签署以来未被篡改。 如需全面了解数字签名功能,请查阅我们的《数字签名指南》

IronPDF为此提供了一个简便的API,支持多种数字签名应用方式。 该功能的核心围绕 PdfSignature 类展开,该类封装了证书以及与签名相关的所有元数据。

签名方法 描述
签名 使用您创建并配置的PdfSignature对象对PDF进行签名。
SignWithFile 使用数字签名证书文件对 PDF 进行签名(.pfx.p12) 进行签名。
SignWithStore 使用计算机证书存储库中的数字签名对 PDF 进行签名,该签名通过其指纹 ID 进行标识。

使用 X509Certificate2 对象

为了实现最大程度的控制,您可以从证书文件中创建一个 X509Certificate2 对象。IronPDF 完全符合 X509Certificate2 标准,为数字签名的实现提供了一种强大且安全的方法。 创建证书对象时,请确保将 X509KeyStorageFlags 设置为 Exportable,因为这是底层加密 API 的要求。请查阅我们代码库中的数字签名实际示例

Install-Package IronPdf
using IronPdf;
using IronPdf.Signing;
using System.Security.Cryptography.X509Certificates;

// Create a new PDF from an HTML string for demonstration.
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf("<h1>Signed Document</h1><p>This document has been digitally signed.</p>");

// Load the certificate from a .pfx file with its password.
// The X509KeyStorageFlags.Exportable flag is crucial for allowing the private key to be used in the signing process.
var cert = new X509Certificate2("IronSoftware.pfx", "123456", X509KeyStorageFlags.Exportable);

// Create a PdfSignature object using the loaded certificate.
var signature = new PdfSignature(cert);

// Apply the signature to the PDF document.
pdf.Sign(signature);

// Save the securely signed PDF document.
pdf.SaveAs("Signed.pdf");
using IronPdf;
using IronPdf.Signing;
using System.Security.Cryptography.X509Certificates;

// Create a new PDF from an HTML string for demonstration.
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf("<h1>Signed Document</h1><p>This document has been digitally signed.</p>");

// Load the certificate from a .pfx file with its password.
// The X509KeyStorageFlags.Exportable flag is crucial for allowing the private key to be used in the signing process.
var cert = new X509Certificate2("IronSoftware.pfx", "123456", X509KeyStorageFlags.Exportable);

// Create a PdfSignature object using the loaded certificate.
var signature = new PdfSignature(cert);

// Apply the signature to the PDF document.
pdf.Sign(signature);

// Save the securely signed PDF document.
pdf.SaveAs("Signed.pdf");
Imports IronPdf
Imports IronPdf.Signing
Imports System.Security.Cryptography.X509Certificates

' Create a new PDF from an HTML string for demonstration.
Private renderer = New ChromePdfRenderer()
Private pdf = renderer.RenderHtmlAsPdf("<h1>Signed Document</h1><p>This document has been digitally signed.</p>")

' Load the certificate from a .pfx file with its password.
' The X509KeyStorageFlags.Exportable flag is crucial for allowing the private key to be used in the signing process.
Private cert = New X509Certificate2("IronSoftware.pfx", "123456", X509KeyStorageFlags.Exportable)

' Create a PdfSignature object using the loaded certificate.
Private signature = New PdfSignature(cert)

' Apply the signature to the PDF document.
pdf.Sign(signature)

' Save the securely signed PDF document.
pdf.SaveAs("Signed.pdf")
$vbLabelText   $csharpLabel

上面的代码首先生成一个简单的 PDF。 随后,它将一个 .pfx 证书文件加载到 X509Certificate2 对象中。 该对象代表数字身份,会被传递给 PdfSignature 构造函数。 最后,pdf.Sign 方法会在保存文档前对其应用此签名。 For more information on the X509Certificate2 class, you can refer to the official Microsoft documentation.

为数字签名添加细粒度信息

数字签名不仅包含证书; 您可以嵌入丰富的元数据,以提供有关该签名的背景信息。 这包括签名位置、签名原因、联系信息以及来自可信权威机构的安全时间戳。 您还可以设置和编辑元数据以配置其他文档属性。

添加这些细节有助于完善文档的审计记录,并为审核人员提供有价值的信息。 IronPDF 还支持采用现代 SHA256SHA512 哈希算法的时间戳服务器。

using IronPdf;
using IronPdf.Signing;
using IronSoftware.Drawing;
using System;

// Load an existing PDF document to be signed.
var pdf = PdfDocument.FromFile("invoice.pdf");

// Create a PdfSignature object directly from the certificate file and password.
var signature = new PdfSignature("IronSoftware.pfx", "123456");

// Add detailed metadata to the signature for a comprehensive audit trail.
// These properties enhance the signature's credibility and provide context
signature.SignatureDate = DateTime.Now;
signature.SigningContact = "legal@ironsoftware.com";
signature.SigningLocation = "Chicago, USA";
signature.SigningReason = "Contractual Agreement";

// Add a secure timestamp from a trusted Time Stamp Authority (TSA).
// This provides cryptographic proof of the signing time.
signature.TimeStampUrl = new Uri("[http://timestamp.digicert.com](http://timestamp.digicert.com)");
signature.TimestampHashAlgorithm = TimestampHashAlgorithms.SHA256;

// Apply a visual appearance to the signature. (More on this in the next section)
signature.SignatureImage = new PdfSignatureImage("assets/visual-signature.png", 0, new Rectangle(350, 750, 200, 100));

// Sign the PDF document with the configured signature object.
pdf.Sign(signature);

// Save the final, signed PDF document.
pdf.SaveAs("DetailedSignature.pdf");
using IronPdf;
using IronPdf.Signing;
using IronSoftware.Drawing;
using System;

// Load an existing PDF document to be signed.
var pdf = PdfDocument.FromFile("invoice.pdf");

// Create a PdfSignature object directly from the certificate file and password.
var signature = new PdfSignature("IronSoftware.pfx", "123456");

// Add detailed metadata to the signature for a comprehensive audit trail.
// These properties enhance the signature's credibility and provide context
signature.SignatureDate = DateTime.Now;
signature.SigningContact = "legal@ironsoftware.com";
signature.SigningLocation = "Chicago, USA";
signature.SigningReason = "Contractual Agreement";

// Add a secure timestamp from a trusted Time Stamp Authority (TSA).
// This provides cryptographic proof of the signing time.
signature.TimeStampUrl = new Uri("[http://timestamp.digicert.com](http://timestamp.digicert.com)");
signature.TimestampHashAlgorithm = TimestampHashAlgorithms.SHA256;

// Apply a visual appearance to the signature. (More on this in the next section)
signature.SignatureImage = new PdfSignatureImage("assets/visual-signature.png", 0, new Rectangle(350, 750, 200, 100));

// Sign the PDF document with the configured signature object.
pdf.Sign(signature);

// Save the final, signed PDF document.
pdf.SaveAs("DetailedSignature.pdf");
Imports IronPdf
Imports IronPdf.Signing
Imports IronSoftware.Drawing
Imports System

' Load an existing PDF document to be signed.
Dim pdf = PdfDocument.FromFile("invoice.pdf")

' Create a PdfSignature object directly from the certificate file and password.
Dim signature = New PdfSignature("IronSoftware.pfx", "123456")

' Add detailed metadata to the signature for a comprehensive audit trail.
' These properties enhance the signature's credibility and provide context
signature.SignatureDate = DateTime.Now
signature.SigningContact = "legal@ironsoftware.com"
signature.SigningLocation = "Chicago, USA"
signature.SigningReason = "Contractual Agreement"

' Add a secure timestamp from a trusted Time Stamp Authority (TSA).
' This provides cryptographic proof of the signing time.
signature.TimeStampUrl = New Uri("http://timestamp.digicert.com")
signature.TimestampHashAlgorithm = TimestampHashAlgorithms.SHA256

' Apply a visual appearance to the signature. (More on this in the next section)
signature.SignatureImage = New PdfSignatureImage("assets/visual-signature.png", 0, New Rectangle(350, 750, 200, 100))

' Sign the PDF document with the configured signature object.
pdf.Sign(signature)

' Save the final, signed PDF document.
pdf.SaveAs("DetailedSignature.pdf")
$vbLabelText   $csharpLabel

如果签名证书未位于系统的受信任存储中,您可能会在某些 PDF 阅读器中看到警告图标。 要获得绿色勾选标记,必须将该证书添加到查看器的受信任身份列表中。

如何为数字签名添加可视化表示?

虽然数字签名已通过加密方式嵌入 PDF 中,但在页面上显示其可视化表示通常会很有帮助。 这可以是公司徽标、手写签名图像或其他图形。 IronPDF 使向 PdfSignature 对象添加图片变得轻而易举。

您可以从文件或流中加载图像,并将其精确定位在 PDF 的任意页面上。 支持的图像格式包括 PNG、JPEG、GIF、BMP、TIFF 和 WebP。该技术类似于在 PDF 文档上添加文本和图像水印的方式。

using IronPdf.Signing;
using IronSoftware.Drawing;

// This example demonstrates various ways to add a visual image to a PDF signature.

// Create a PdfSignature object.
var signature = new PdfSignature("IronSoftware.pfx", "123456");

// Define the position and size for the signature image on the first page (index 0).
// Rectangle parameters: x position, y position, width, height
var signatureRectangle = new Rectangle(350, 750, 200, 100);

// Option 1: Set the SignatureImage property directly.
// This is the most straightforward approach
signature.SignatureImage = new PdfSignatureImage("assets/visual-signature.png", 0, signatureRectangle);

// Option 2: Use the LoadSignatureImageFromFile method.
// This method provides the same functionality with a different syntax
signature.LoadSignatureImageFromFile("assets/visual-signature.png", 0, signatureRectangle);

// Option 3: Load an image from a stream. This is useful for images generated in memory.
// Perfect for scenarios where images are retrieved from databases or web services
AnyBitmap image = AnyBitmap.FromFile("assets/visual-signature.png");
using (var imageStream = image.ToStream())
{
    signature.LoadSignatureImageFromStream(imageStream, 0, signatureRectangle);
}

// After configuring the signature image, apply it to a PDF.
var pdf = PdfDocument.FromFile("invoice.pdf");
pdf.Sign(signature);
pdf.SaveAs("VisualSignature.pdf");
using IronPdf.Signing;
using IronSoftware.Drawing;

// This example demonstrates various ways to add a visual image to a PDF signature.

// Create a PdfSignature object.
var signature = new PdfSignature("IronSoftware.pfx", "123456");

// Define the position and size for the signature image on the first page (index 0).
// Rectangle parameters: x position, y position, width, height
var signatureRectangle = new Rectangle(350, 750, 200, 100);

// Option 1: Set the SignatureImage property directly.
// This is the most straightforward approach
signature.SignatureImage = new PdfSignatureImage("assets/visual-signature.png", 0, signatureRectangle);

// Option 2: Use the LoadSignatureImageFromFile method.
// This method provides the same functionality with a different syntax
signature.LoadSignatureImageFromFile("assets/visual-signature.png", 0, signatureRectangle);

// Option 3: Load an image from a stream. This is useful for images generated in memory.
// Perfect for scenarios where images are retrieved from databases or web services
AnyBitmap image = AnyBitmap.FromFile("assets/visual-signature.png");
using (var imageStream = image.ToStream())
{
    signature.LoadSignatureImageFromStream(imageStream, 0, signatureRectangle);
}

// After configuring the signature image, apply it to a PDF.
var pdf = PdfDocument.FromFile("invoice.pdf");
pdf.Sign(signature);
pdf.SaveAs("VisualSignature.pdf");
Imports IronPdf.Signing
Imports IronSoftware.Drawing

' This example demonstrates various ways to add a visual image to a PDF signature.

' Create a PdfSignature object.
Dim signature As New PdfSignature("IronSoftware.pfx", "123456")

' Define the position and size for the signature image on the first page (index 0).
' Rectangle parameters: x position, y position, width, height
Dim signatureRectangle As New Rectangle(350, 750, 200, 100)

' Option 1: Set the SignatureImage property directly.
' This is the most straightforward approach
signature.SignatureImage = New PdfSignatureImage("assets/visual-signature.png", 0, signatureRectangle)

' Option 2: Use the LoadSignatureImageFromFile method.
' This method provides the same functionality with a different syntax
signature.LoadSignatureImageFromFile("assets/visual-signature.png", 0, signatureRectangle)

' Option 3: Load an image from a stream. This is useful for images generated in memory.
' Perfect for scenarios where images are retrieved from databases or web services
Dim image As AnyBitmap = AnyBitmap.FromFile("assets/visual-signature.png")
Using imageStream = image.ToStream()
    signature.LoadSignatureImageFromStream(imageStream, 0, signatureRectangle)
End Using

' After configuring the signature image, apply it to a PDF.
Dim pdf As PdfDocument = PdfDocument.FromFile("invoice.pdf")
pdf.Sign(signature)
pdf.SaveAs("VisualSignature.pdf")
$vbLabelText   $csharpLabel

此代码演示了三种向数字签名添加视觉组件的等效方法。 无论图像存储在磁盘上还是内存中,您都可以在签名过程中将其轻松添加到 PDF 中。 这弥合了隐形加密安全与可见文档审批之间的鸿沟。

签署后如何控制文档权限?

在签署文件时,您可能需要明确后续允许进行哪些修改(如有)。 例如,您可以选择完全锁定文档,或仅允许用户填写表单字段。 IronPdf 允许您使用 SignaturePermissions 枚举来设置这些权限。 如需更高级的权限控制,请参阅我们关于设置 PDF 密码和权限的指南。

设置签名权限是管理文档生命周期的关键环节。 它确保在应用您的签名后,文档的完整性将按照您的规则得到保持。 如果用户执行了不允许的操作,签名将被视为无效。

`SignaturePermissions` 成员 定义
`禁止修改` 严禁进行任何形式的修改。该文档已锁定。
`FormFillingAllowed` 仅允许填写现有表单字段并签名。
`允许添加注释和表单填写` 支持表单填写、签名以及创建或修改注释。

保存并签署特定的 PDF 修订版本

PDF 文件可以存储更改历史记录,这与版本控制系统非常相似。 这被称为增量节省。 当您对 PDF 进行签名时,该签名仅适用于文档的特定版本。 对于文档需经过多级审批的工作流程而言,这一点至关重要。 请参阅我们的详细指南,了解如何管理 PDF 修订历史记录

在下面的示例中,我们加载一个 PDF 文件,进行编辑,然后对当前版本进行签名,同时仅允许将来进行表单填写操作。 我们使用 SaveAsRevision 在保存文件前将当前状态提交至文档历史记录。

GetRevision

理解增量保存是掌握高级 PDF 工作流的关键。 虽然简单的查看器可能只显示最新版本,但像 Adobe Acrobat 这样的工具可以显示完整的修订历史,包括谁签署了哪个版本,以及两次签署之间进行了哪些更改。 IronPDF 为您提供了对该过程的完全程序化控制。

对于需要管理复杂文档工作流且对安全性和合规性要求较高的企业,可能需要一套全面的解决方案。 Iron Software 提供 Iron Suite 套件,其中包含用于签名和文档处理的 IronPDF,以及用于各类文档处理任务的其他库,仅需一次性付费即可使用。

如何在不同版本间管理和验证签名?

一份 PDF 文档在其各个修订版本中可能包含多个签名。 IronPDF 提供了有效管理此历史记录的工具。

  • 回滚到之前的版本:您可以使用 RollBackToRevision 方法将文档恢复到较早的状态。 这将丢弃该修订版本之后所做的所有更改和签名。
  • 验证所有签名VerifyAllSignatures 方法会检查文档所有修订版本中_所有_签名的有效性。 仅当所有签名均有效且未发生任何未经授权的更改时,才会返回 SignatureStatus.Valid
  • 移除签名RemoveSignatures 方法将从文档的每个版本中移除所有数字签名,生成一个干净的未签名版本。
// Load a PDF with a complex signature history.
var pdf = PdfDocument.FromFile("multi_signed_report.pdf");

// Verify all signatures across all revisions.
// This ensures document integrity throughout its entire history
bool allSignaturesValid = pdf.VerifySignatures();
Console.WriteLine($"All signatures are valid: {allSignaturesValid}");

// Roll back to the first revision (index 0).
// Useful for reviewing the original document state
if (pdf.RevisionCount &gt; 1)
{
    PdfDocument firstRevision = pdf.GetRevision(0);
    firstRevision.SaveAs("report_first_revision.pdf");
}

// Create a completely unsigned version of the document.
// This removes all digital signatures while preserving content
pdf.RemoveSignatures();
pdf.SaveAs("report_unsigned.pdf");
// Load a PDF with a complex signature history.
var pdf = PdfDocument.FromFile("multi_signed_report.pdf");

// Verify all signatures across all revisions.
// This ensures document integrity throughout its entire history
bool allSignaturesValid = pdf.VerifySignatures();
Console.WriteLine($"All signatures are valid: {allSignaturesValid}");

// Roll back to the first revision (index 0).
// Useful for reviewing the original document state
if (pdf.RevisionCount &gt; 1)
{
    PdfDocument firstRevision = pdf.GetRevision(0);
    firstRevision.SaveAs("report_first_revision.pdf");
}

// Create a completely unsigned version of the document.
// This removes all digital signatures while preserving content
pdf.RemoveSignatures();
pdf.SaveAs("report_unsigned.pdf");
Imports System

' Load a PDF with a complex signature history.
Dim pdf = PdfDocument.FromFile("multi_signed_report.pdf")

' Verify all signatures across all revisions.
' This ensures document integrity throughout its entire history
Dim allSignaturesValid As Boolean = pdf.VerifySignatures()
Console.WriteLine($"All signatures are valid: {allSignaturesValid}")

' Roll back to the first revision (index 0).
' Useful for reviewing the original document state
If pdf.RevisionCount > 1 Then
    Dim firstRevision As PdfDocument = pdf.GetRevision(0)
    firstRevision.SaveAs("report_first_revision.pdf")
End If

' Create a completely unsigned version of the document.
' This removes all digital signatures while preserving content
pdf.RemoveSignatures()
pdf.SaveAs("report_unsigned.pdf")
$vbLabelText   $csharpLabel

如何在 PDF 上添加手写签名?

有时,您并不需要数字签名的加密安全功能,而仅仅希望应用一种可视化的电子签名,例如扫描的手写签名。 这通常被称为"盖章"。 IronPDF 可通过其 WatermarkStamp 功能实现此功能。 如需了解更高级的水印选项,请参阅我们的自定义水印指南

让我们从一份样本发票 PDF 和一张 .png 手写签名图片开始。

VerifySignatures

盖章前的原始发票PDF文件。

以下是我们将应用的签名图片:

用于PDF加盖教程的黑色墨水手写签名样本,显示 手写签名示例图片.

以下代码使用 SignatureImage 属性将此图像水印印在 PDF 的右下角。

using IronPdf.Editing;

// Load the existing PDF document.
var pdf = PdfDocument.FromFile("invoice.pdf");

// Create an HtmlStamp containing our signature image.
// HtmlStamp allows us to position HTML content precisely on the page
var signatureStamp = new HtmlStamp("&lt;img src='assets/signature.png'/>")
{
    // Configure the stamp's position and appearance.
    VerticalAlignment = VerticalAlignment.Bottom,
    HorizontalAlignment = HorizontalAlignment.Right,
    Margin = 10,  // Add some space from the edge.
    Opacity = 90  // Make it slightly transparent for a more authentic look.
};

// Apply the stamp to all pages of the PDF.
// You can also specify specific page numbers if needed
pdf.ApplyStamp(signatureStamp);

// Save the modified PDF document.
pdf.SaveAs("official_invoice.pdf");
using IronPdf.Editing;

// Load the existing PDF document.
var pdf = PdfDocument.FromFile("invoice.pdf");

// Create an HtmlStamp containing our signature image.
// HtmlStamp allows us to position HTML content precisely on the page
var signatureStamp = new HtmlStamp("&lt;img src='assets/signature.png'/>")
{
    // Configure the stamp's position and appearance.
    VerticalAlignment = VerticalAlignment.Bottom,
    HorizontalAlignment = HorizontalAlignment.Right,
    Margin = 10,  // Add some space from the edge.
    Opacity = 90  // Make it slightly transparent for a more authentic look.
};

// Apply the stamp to all pages of the PDF.
// You can also specify specific page numbers if needed
pdf.ApplyStamp(signatureStamp);

// Save the modified PDF document.
pdf.SaveAs("official_invoice.pdf");
Imports IronPdf.Editing

' Load the existing PDF document.
Dim pdf = PdfDocument.FromFile("invoice.pdf")

' Create an HtmlStamp containing our signature image.
' HtmlStamp allows us to position HTML content precisely on the page
Dim signatureStamp = New HtmlStamp("<img src='assets/signature.png'/>") With {
    ' Configure the stamp's position and appearance.
    .VerticalAlignment = VerticalAlignment.Bottom,
    .HorizontalAlignment = HorizontalAlignment.Right,
    .Margin = 10,  ' Add some space from the edge.
    .Opacity = 90  ' Make it slightly transparent for a more authentic look.
}

' Apply the stamp to all pages of the PDF.
' You can also specify specific page numbers if needed
pdf.ApplyStamp(signatureStamp)

' Save the modified PDF document.
pdf.SaveAs("official_invoice.pdf")
$vbLabelText   $csharpLabel

加盖水印的 PDF 文件效果如何?

运行代码后,签名图像将盖印在文档上,生成一份可视化签名的发票。

最终生成的 PDF 文件右下角将盖有手写签名图像。

如何在 PDF 中添加交互式签名字段?

对于需要在 Adobe Acrobat 等 PDF 阅读器中由最终用户签署的文档,您可以添加交互式签名表单字段。 这会生成一个可点击的空白区域,提示用户应用自己的数字签名。 有关 PDF 表单的完整指南,请参阅我们的《创建 PDF 表单》教程

您可以创建一个 SignatureFormField 并将其添加到 PDF 的表单集合中。 您可以精确控制其在页面上的位置和大小。 这对于需要多方签名或需要从外部方收集签名的文档尤为有用。

true``RemoveSignatures

using IronPdf.Forms;
using IronSoftware.Drawing;

// Create a new PDF to add the signature field to.
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf("<h1>Please Sign Below</h1>");

// Define the properties for the signature form field.
string fieldName = "ClientSignature";  // Unique identifier for the field
int pageIndex = 0;  // Add to the first page (zero-indexed)
var fieldRect = new Rectangle(50, 200, 300, 100);  // Position: (x, y), Size: (width, height)

// Create the SignatureFormField object.
// This creates an interactive field that users can click to sign
var signatureField = new SignatureFormField(fieldName, pageIndex, fieldRect);

// Add the signature field to the PDF's form.
pdf.Form.Add(signatureField);

// Save the PDF with the new interactive signature field.
pdf.SaveAs("interactive_signature.pdf");
using IronPdf.Forms;
using IronSoftware.Drawing;

// Create a new PDF to add the signature field to.
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf("<h1>Please Sign Below</h1>");

// Define the properties for the signature form field.
string fieldName = "ClientSignature";  // Unique identifier for the field
int pageIndex = 0;  // Add to the first page (zero-indexed)
var fieldRect = new Rectangle(50, 200, 300, 100);  // Position: (x, y), Size: (width, height)

// Create the SignatureFormField object.
// This creates an interactive field that users can click to sign
var signatureField = new SignatureFormField(fieldName, pageIndex, fieldRect);

// Add the signature field to the PDF's form.
pdf.Form.Add(signatureField);

// Save the PDF with the new interactive signature field.
pdf.SaveAs("interactive_signature.pdf");
Imports IronPdf.Forms
Imports IronSoftware.Drawing

' Create a new PDF to add the signature field to.
Dim renderer As New ChromePdfRenderer()
Dim pdf = renderer.RenderHtmlAsPdf("<h1>Please Sign Below</h1>")

' Define the properties for the signature form field.
Dim fieldName As String = "ClientSignature"  ' Unique identifier for the field
Dim pageIndex As Integer = 0  ' Add to the first page (zero-indexed)
Dim fieldRect As New Rectangle(50, 200, 300, 100)  ' Position: (x, y), Size: (width, height)

' Create the SignatureFormField object.
' This creates an interactive field that users can click to sign
Dim signatureField As New SignatureFormField(fieldName, pageIndex, fieldRect)

' Add the signature field to the PDF's form.
pdf.Form.Add(signatureField)

' Save the PDF with the new interactive signature field.
pdf.SaveAs("interactive_signature.pdf")
$vbLabelText   $csharpLabel

当用户打开此 PDF 时,将看到一个可点击的字段,允许他们使用自己的数字 ID 完成签名流程。 您可以在我们的《PDF 表单创建指南》中了解更多关于创建和管理交互式表单的信息。

PDF编辑器在名为 通过编程方式添加到 PDF 文档中的未签名交互式签名字段。

如何从已验证的签名中获取签名者姓名?

要获取签署签名的证书所有者的常见名称,我们可以使用 VerifiedSignature 类来访问 SignerName 属性。 以下代码片段演示了如何实现这一目标。

:path=/static-assets/pdf/content-code-examples/how-to/signing-find-signer-name.cs
using IronPdf;
using System;

// Import the Signed PDF report
var pdf = PdfDocument.FromFile("multi_signed_report.pdf");

// Using GetVerifiedSignatures() obtain a list of `VerifiedSignature` objects from the PDF
pdf.GetVerifiedSignatures().ForEach(signature =>
{
    // Print out the SignerName of each `VerifiedSignature` object
    Console.WriteLine($"SignatureName: {signature.SignerName}");
});
Imports IronPdf
Imports System

' Import the Signed PDF report
Dim pdf = PdfDocument.FromFile("multi_signed_report.pdf")

' Using GetVerifiedSignatures() obtain a list of `VerifiedSignature` objects from the PDF
pdf.GetVerifiedSignatures().ForEach(Sub(signature)
    ' Print out the SignerName of each `VerifiedSignature` object
    Console.WriteLine($"SignatureName: {signature.SignerName}")
End Sub)
$vbLabelText   $csharpLabel

导入已签名的 PDF 文件后,我们使用 GetSignatures(...) 方法检索报告中的 VerifiedSignature 对象列表,并为每个签名 PRINT SignerName

GetVerifiedSignatures``SignerName

请注意,此值是从证书的"主体区分名称"(SubjectDN)中提取的,如果"通用名称"(CN)字段不存在,则返回 null。

IronPDF 的 PDF 签名功能下一步将如何发展?

本指南展示了 IronPDF 强大且灵活的 PDF 签名功能。 无论您需要应用带有详细元数据的安全数字签名、管理文档修订版本、添加视觉签名,还是创建交互式表单,IronPDF 都提供了一个全面且对开发者友好的 API 来完成这些任务。

如需进一步了解,您可以下载 IronPDF for .NET 并获取免费试用许可证,在您的项目中测试其所有功能。 如需了解更高级的文档处理技术(包括添加注释和处理表单字段),请查阅我们的详尽文档和教程。

准备好探索更多可能了吗? 点击此处查看我们的教程页面:PDF 签名与加密

常见问题解答

如何使用 C# 中的证书对 PDF 进行数字签名?

有了 IronPDF,只需一行代码,您就可以使用 PdfSignature 类对 PDF 进行数字签名。只需用证书文件(.pfx 或 .p12)和密码创建一个新的 PdfSignature 对象,然后调用 SignPdfFile() 方法即可。例如:new IronPdf.Signing.PdfSignature("certificate.pfx", "password").SignPdfFile("input.pdf").这将使用你的 X509Certificate2 应用防篡改数字签名,以确保文档的真实性。

支持哪些类型的 PDF 签名?

IronPDF 支持三种主要的 PDF 签名类型:1)使用 X509Certificate2 证书进行身份验证和防篡改的数字签名;2)在文档中添加图形或手写签名图像的可视签名图章;3)允许用户对 PDF 进行电子签名的交互式签名表单字段。每种类型都能满足不同的文档安全和工作流程要求。

哪些证书格式可用于数字签名?

IronPDF 支持常见的数字证书格式,包括 .pfx(个人信息交换)和 .p12 文件。这些证书文件包含数字签名所需的公钥和私钥。IronPDF 中的 PdfSignature 类可以与任何 X509Certificate2 对象一起工作,从而为您加载和管理签名证书提供了灵活性。

我可以在数字签名中添加可视化表示吗?

是的,IronPDF 允许您在数字签名中添加可视化元素。您可以在加密签名旁添加图形表示,如手写签名图像、公司徽标或自定义印章。这将数字证书的安全性与可视化确认相结合,使签名文档既安全又具有专业性。

如何创建交互式签名字段供用户进行电子签名?

IronPDF 可让您在 PDF 文档中添加交互式签名表单字段。这些字段允许用户通过点击和绘制签名或上传签名图像来签署电子文档。该功能非常适合创建需要收集签名的文档,如需要多方签名的合同或表格。

签署 PDF 是否能确保文档的完整性?

是的,当您使用 IronPDF 使用 X509Certificate2 对 PDF 进行数字签名时,就会创建一个防篡改印章,确保文档的完整性。数字签名可保证文档在签署后未被篡改。签署后对 PDF 的任何修改都会导致签名无效,从而提醒接收者文档可能已被篡改。

Jacob Mellor,Team Iron 的首席技术官
首席技术官

Jacob Mellor 是 Iron Software 的首席技术官,也是一位开创 C# PDF 技术的有远见的工程师。作为 Iron Software 核心代码库的原始开发者,他从公司成立之初就开始塑造公司的产品架构,与首席执行官 Cameron Rimington 一起将公司转变为一家拥有 50 多名员工的公司,为 NASA、特斯拉和全球政府机构提供服务。

Jacob 拥有曼彻斯特大学土木工程一级荣誉工程学士学位(BEng)(1998-2001 年)。他的旗舰产品 IronPDF 和 Iron Suite for .NET 库在全球的 NuGet 安装量已超过 3000 万次,其基础代码继续为全球使用的开发人员工具提供动力。Jacob 拥有 25 年的商业经验和 41 年的编码专业知识,他一直专注于推动企业级 C#、Java 和 Python PDF 技术的创新,同时指导下一代技术领导者。

审核者
Jeff Fritz
Jeffrey T. Fritz
首席项目经理 - .NET 社区团队
Jeff 也是 .NET 和 Visual Studio 团队的首席项目经理。他是 .NET Conf 虚拟会议系列的执行制片人,并主持“Fritz and Friends”直播节目,每周两次与观众一起谈论技术并编写代码。Jeff 撰写研讨会、演示文稿并计划包括 Microsoft Build、Microsoft Ignite、.NET Conf 和 Microsoft MVP 峰会在内的最大型微软开发者活动的内容。
准备开始了吗?
Nuget 下载 18,926,724 | 版本: 2026.5 just released
Still Scrolling Icon

还在滚动吗?

想快速获得证据? PM > Install-Package IronPdf
运行示例看着你的HTML代码变成PDF文件。