跳至页脚内容
使用IRONPDF

如何在.NET中创建PDF签名

role="alert"> 您的企业在每年的PDF安全和合规订阅上花费过多。请考虑使用IronSecureDoc,它提供管理SaaS服务(如数字签名、修订、加密和保护)的解决方案,全部只需一次性付款。立即试用

本文将深入探讨IronPDF的附加功能和能力,以及示例源代码片段,帮助您开始将数字签名集成到PDF文件中。

理解数字签名和IronPDF

数字签名作为传统手写签名的电子对应物,为电子文档提供了一层额外的安全性和可信度。 借助IronPDF,数字签署PDF文档变得轻而易举,允许开发人员轻松生成、验证、管理和添加数字签名

设置IronPDF

为了确保IronPDF库在您的.NET项目中的顺利集成,遵循以下步骤进行设置和实施非常重要。 这些说明假设您使用的是Visual Studio作为开发环境。

  1. 启动Visual Studio并打开应用程序。
  2. 从“文件”菜单中选择“新建项目”。
  3. 在出现的对话框中,选择“控制台应用程序”模板。
  4. 点击下一步按钮继续。

请注意,这些初始步骤将为将IronPDF库无缝插入到您的项目中奠定基础。

如何在.NET中创建PDF签名,图2:在Visual Studio中创建新项目 在Visual Studio中创建新项目

要继续创建项目,请在指定字段中提供必要的信息。 在“项目名称”框中输入合适的项目名称,并在“位置”字段中指定新项目的期望位置。 输入此信息后,请点击下一步按钮继续。

如何在.NET中创建PDF签名,图3:配置您的新项目 配置您的新项目

要配置项目的框架,请遵循以下步骤。 首先从下拉菜单中提供的选项中选择一个.NET Framework。 在这种情况下,建议使用.NET版本6.0。 做出选择后,继续点击创建按钮以启动项目创建过程。

如何在.NET中创建PDF签名,图4:.NET Framework选择 .NET框架选择

要继续,您需要为您的解决方案下载所需的IronPDF库。 为此,您可以在软件包管理器控制台中使用以下命令。

Install-Package IronPdf

也可以使用NuGet包管理器查找IronPDF包。

如何在.NET中创建PDF签名,图5:在NuGet包管理器UI中导航并安装IronPDF包 在NuGet包管理器UI中导航并安装IronPDF包

以编程方式添加数字签名

让我们探讨如何使用IronPDF向PDF文档添加数字签名。

创建新PDF文档

首先,您需要使用ChromePdfRenderer创建一个新的PDF文档。 使用RenderHtmlAsPdf方法将HTML字符串转换为PDF文档。 下面是一个例子:

using IronPdf;

// Create a PDF from an HTML string
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1>");

// Export to a file or stream
pdf.SaveAs("output.pdf");
using IronPdf;

// Create a PDF from an HTML string
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1>");

// Export to a file or stream
pdf.SaveAs("output.pdf");
Imports IronPdf

' Create a PDF from an HTML string
Private renderer = New ChromePdfRenderer()
Private pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1>")

' Export to a file or stream
pdf.SaveAs("output.pdf")
$vbLabelText   $csharpLabel

您还可以使用RenderHtmlFileAsPdf方法将HTML文件渲染为PDF。 下面是一个例子:

using IronPdf;

// Create a PDF from an existing HTML file
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlFileAsPdf("example.html");

// Export to a file or stream
pdf.SaveAs("output.pdf");
using IronPdf;

// Create a PDF from an existing HTML file
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlFileAsPdf("example.html");

// Export to a file or stream
pdf.SaveAs("output.pdf");
Imports IronPdf

' Create a PDF from an existing HTML file
Private renderer = New ChromePdfRenderer()
Private pdf = renderer.RenderHtmlFileAsPdf("example.html")

' Export to a file or stream
pdf.SaveAs("output.pdf")
$vbLabelText   $csharpLabel

加载现有的PDF文档

如果您想签署现成的PDF文档,IronPDF允许您使用PdfDocument.FromFile方法加载它们。 下面是一个例子:

using IronPdf;

// Load an existing PDF document
var document = PdfDocument.FromFile("path/to/document.pdf");
using IronPdf;

// Load an existing PDF document
var document = PdfDocument.FromFile("path/to/document.pdf");
Imports IronPdf

' Load an existing PDF document
Private document = PdfDocument.FromFile("path/to/document.pdf")
$vbLabelText   $csharpLabel

生成自签名证书和签署PDF

添加数字签名之前,您需要一个数字证书。 您可以使用Adobe Acrobat轻松创建OpenSSL数字证书; 它们可以是PFX数字证书或P12数字证书。 一旦这些证书生成,您可以使用IronPDF轻松地使用它们签署PDF。

在以下代码中,您可以使用IronPDF和PFX文件高级电子签名对PDF文件进行签名。 此教程演示了如何使用Adobe Acrobat生成证书。

using IronPdf;
using IronPdf.Signing;
using System;

// Create a PDF from HTML
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument doc = renderer.RenderHtmlAsPdf("<h1>foo</h1>");

// Create a digital signature
var signature = new PdfSignature("Iron.pfx", "123456")
{
    SigningContact = "support@ironsoftware.com",
    SigningLocation = "Chicago, USA",
    SigningReason = "To show how to sign a PDF"
};

// Sign the PDF document
doc.Sign(signature);

// Save the signed PDF
doc.SaveAs("signed.pdf");
using IronPdf;
using IronPdf.Signing;
using System;

// Create a PDF from HTML
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument doc = renderer.RenderHtmlAsPdf("<h1>foo</h1>");

// Create a digital signature
var signature = new PdfSignature("Iron.pfx", "123456")
{
    SigningContact = "support@ironsoftware.com",
    SigningLocation = "Chicago, USA",
    SigningReason = "To show how to sign a PDF"
};

// Sign the PDF document
doc.Sign(signature);

// Save the signed PDF
doc.SaveAs("signed.pdf");
Imports IronPdf
Imports IronPdf.Signing
Imports System

' Create a PDF from HTML
Private renderer As New ChromePdfRenderer()
Private doc As PdfDocument = renderer.RenderHtmlAsPdf("<h1>foo</h1>")

' Create a digital signature
Private signature = New PdfSignature("Iron.pfx", "123456") With {
	.SigningContact = "support@ironsoftware.com",
	.SigningLocation = "Chicago, USA",
	.SigningReason = "To show how to sign a PDF"
}

' Sign the PDF document
doc.Sign(signature)

' Save the signed PDF
doc.SaveAs("signed.pdf")
$vbLabelText   $csharpLabel

向PDF文档添加数字签名

为了向PDF文档添加电子签名,IronPDF提供了PdfSignature类。 此方法接受证书和其他必要参数以创建数字签名字段并将其嵌入到PDF文档中。 以下是使用数字签名对PDF签名的示例:

using IronPdf.Signing;
using IronSoftware.Drawing;

// Create a PdfSignature object
var sig = new PdfSignature("IronSoftware.pfx", "123456");
sig.SignatureImage = new PdfSignatureImage("IronSoftware.png", 0, new CropRectangle(0, 600, 100, 100));
using IronPdf.Signing;
using IronSoftware.Drawing;

// Create a PdfSignature object
var sig = new PdfSignature("IronSoftware.pfx", "123456");
sig.SignatureImage = new PdfSignatureImage("IronSoftware.png", 0, new CropRectangle(0, 600, 100, 100));
Imports IronPdf.Signing
Imports IronSoftware.Drawing

' Create a PdfSignature object
Private sig = New PdfSignature("IronSoftware.pfx", "123456")
sig.SignatureImage = New PdfSignatureImage("IronSoftware.png", 0, New CropRectangle(0, 600, 100, 100))
$vbLabelText   $csharpLabel

验证数字签名

IronPDF允许您以编程方式验证PDF文档中的数字签名。 您可以使用VerifyPdfSignatures方法来验证文档中存在的签名是否仍然有效。 下面是一个例子:

using IronPdf;

// Load a PDF document
PdfDocument pdf = PdfDocument.FromFile("annual_census.pdf");

// Validate the signatures
bool isValid = pdf.VerifyPdfSignatures();
using IronPdf;

// Load a PDF document
PdfDocument pdf = PdfDocument.FromFile("annual_census.pdf");

// Validate the signatures
bool isValid = pdf.VerifyPdfSignatures();
Imports IronPdf

' Load a PDF document
Private pdf As PdfDocument = PdfDocument.FromFile("annual_census.pdf")

' Validate the signatures
Private isValid As Boolean = pdf.VerifyPdfSignatures()
$vbLabelText   $csharpLabel

PDF加水印

如果没有可用的数字证书来签署PDF文档,您可以在一个角落或空白处添加水印以数字方式占有PDF文件。在以下源码中,使用ApplyWatermark方法将水印应用于PDF文件。

using IronPdf;
using IronPdf.Editing;

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

// Apply a watermark
pdf.ApplyWatermark("<img src='signature.png'/>", 90, VerticalAlignment.Bottom, HorizontalAlignment.Right);

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

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

// Apply a watermark
pdf.ApplyWatermark("<img src='signature.png'/>", 90, VerticalAlignment.Bottom, HorizontalAlignment.Right);

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

' Load a PDF document
Private pdf = PdfDocument.FromFile("invoice.pdf")

' Apply a watermark
pdf.ApplyWatermark("<img src='signature.png'/>", 90, VerticalAlignment.Bottom, HorizontalAlignment.Right)

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

授权和定价

IronPDF is an outstanding C# library that offers developers the freedom to use it for development purposes without any cost. You can try the 免费试用以测试其所有功能。 此外,它也可以在任何时候许可用于商业用途。该库适合各种实体的需求,包括个人开发者、代理机构、跨国组织以及SaaS和OEM再分发。

使用IronPDF,有多种许可选项可供选择,确保所有人都能使用。 这些许可证包含一系列好处,例如30天退款保证、一年的综合支持和升级、在开发、登台和生产环境中的验证,以及永久许可证,这意味着一次性购买。

对于那些寻求预算友好型选项的人,Lite套餐提供了固定价格$799,不包含任何经常性费用。 如果您需要更详细的信息或帮助以确定最适合您需求的许可证,请参阅产品许可页面,在那里您可以找到其他资源和指导。

如何在.NET中创建PDF签名,图6:IronPDF许可 IronPDF许可

结论

在PDF文档中实施数字签名是确保文档完整性、真实性和安全性的重要方面。

IronPDF, a comprehensive .NET PDF签名库,通过提供强大的工具和API简化了这一过程。

本文探讨了IronPDF的功能和能力,并提供了示例代码片段,以帮助您开始以编程方式向PDF文档添加数字签名。 通过利用IronPDF的功能,您可以增强PDF文件的安全性和可信度,实现各种应用中安全的文档交换。

Additionally, IronPDF offers developers methods to render PDF documents into images and extract text and content from a PDF. IronPDF is also capable of rendering charts in PDFs, adding barcodes, watermarking, and even handling PDF forms programmatically.

请记得参考IronPDF的官方文档和本文中提供的示例代码,以获取更详细的信息和全面的实施指南。 有关使用IronPDF的数字签名库的详细教程和完整概览,请访问以下指导

常见问题解答

如何将.NET Core PDF库集成到我的项目中?

要将像IronPDF这样的.NET Core PDF库集成到项目中,可以在Visual Studio中创建一个新的控制台应用程序,选择首选的.NET Framework,并使用Install-Package IronPdf命令通过Package Manager Console安装IronPDF。

一个.NET PDF库提供哪些文档安全功能?

像IronPDF这样的.NET PDF库提供了添加数字签名、加密和修订等功能,以增强文档的安全性和真实性。

如何以编程方式向PDF文档添加数字签名?

使用IronPDF创建一个具有数字证书的PdfSignature对象并应用Sign方法到PDF可以添加数字签名。使用SaveAs方法保存签署的文档。

PDF中数字签名的验证过程是什么?

要验证PDF中的数字签名,使用IronPDF的VerifyPdfSignatures方法以确保签名是有效的且未被更改。

我可以在PDF中使用水印作为数字签名的替代品吗?

是的,当没有用于签名的数字证书时,水印可以用作指示文档所有权或状态的替代方案。

是否有供测试的.NET PDF库的试用版?

IronPDF提供一个免费试用版,允许开发者在购买商业许可证之前测试其全套功能。

数字签名如何增强PDF文档的完整性?

数字签名提供了一种验证PDF文档真实性和完整性的方法,确保自应用签名以来内容未被更改。

创建用于PDF签名的自签名证书的步骤是什么?

可以通过生成一个PFX文件并将其与PdfSignature类一起使用以在文档中签名,从而在IronPDF中创建用于PDF签名的自签名证书。

IronPDF 是否完全兼容 .NET 10?兼容 .NET 10 能带来哪些好处?

是的——IronPDF 完全兼容 .NET 10,支持其所有关键的新运行时和语言特性。开发者无需任何变通方案,即可在 .NET 10 中使用 IronPDF 开发 Web 应用、桌面应用、微服务和 MAUI 应用。兼容性意味着通过减少堆内存分配来提升性能,并提供增强型哈希等新的安全 API,以及对 C# 14 改进(例如扩展块和简单的 lambda 参数)的访问。像往常一样通过 NuGet 安装,API 的工作方式与之前的 .NET 版本完全相同。

Curtis Chau
技术作家

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。