PDFsharp 数字签名PDF文档对比IronPDF(代码示例)
数字签名是一种数学技术,用于验证电子文档的真实性和完整性。 它作为电子签名在多个司法管辖区中用于数字签署文档,确保高度安全性和法律有效性。
数字签名是使用只有签名者知道的私钥创建的。 私钥在签署时为文档建立了一个独特的数字签名。 签名包括签名者的姓名、电子邮件地址和其他个人信息。 要确认数字签名文件的真实性,接收方需要访问签名者的公钥。 通过使用公钥对签名进行解密来验证其合法性。
在本教程中,我们比较了如何使用PDFsharp和IronPDF向PDF文档添加数字签名。 数字签名对于验证文档的真实性至关重要,而PDF文件是一种常用于此类操作的格式。
PDFsharp 是一个知名的开源库,用于PDF的创建和处理,而 IronPDF 是一个功能强大的 .NET PDF 库,提供类似的功能,还有额外的高级功能。
本指南涵盖了使用私钥签署 PDF 文件和验证签名的过程,并包括这两个库的示例源代码。
为什么数字签名很重要?
数字签名确保文档的完整性并提供坚固的安全性。 它们通常用于合同、协议和其他法律文件。
关键收益:
- 比传统签名更安全、防篡改。
- 通过电子方式验证,减少手动验证工作。
- 实现全球范围内的远程文档签名。
- 提供比传统签名更高的保证。
PDFsharp概述
PDFSharp 是一个主要用于创建和处理 PDF 文档的开源 C# 库。 它广泛用于基本的 PDF 任务,如生成简单的 PDF 文件、编辑现有文档和渲染图形。 然而,其对如数字签名等高级功能的原生支持有限,开发者常常需要依赖第三方库(如 BouncyCastle)来集成这些功能。 PDFsharp 是开源的,采用 MIT 许可证,是那些对成本和灵活性优先的项目的一个好选择。
主要功能
- 开源并免费,采用 MIT 许可证。
- 基本的 PDF 创建和处理。
- 可以使用像 BouncyCastle 这样的外部库进行扩展以实现数字签名。
- 缺乏原生支持高级 PDF 功能,如 HTML 到 PDF 转换和复杂的表单处理。
IronPDF 概览

IronPDF 是一个强大的 .NET PDF 库,提供了一个简单而强大的 API 用于生成、编辑和处理 PDF。 其突出特点之一是开发者可以轻松实现数字签名,而数字签名对于验证文档的真实性至关重要。 除了数字签名,IronPDF 还支持如 HTML 到 PDF 转换、水印和表单处理等高级功能。 这对于从事商业项目的开发者特别有价值,因为快速实现和强大功能是其重点。
主要功能
使用 PDFsharp 程序化添加数字签名
PDFsharp 是一个设计用于 C# 中 PDF 创建和处理的开源库。 然而,虽然它确实提供支持添加签名,但您需要集成类似 BouncyCastle 的第三方工具以确保 PDF 文档的数字签名安全、准确。
使用 PDFsharp 添加数字签名的步骤
- 通过 NuGet 安装 PDFsharp 和 BouncyCastle。
- 使用 X509Certificate2 创建数字证书。
- 使用 BouncyCastle 签署 PDF。
示例代码
using System;
using System.IO;
using System.Security.Cryptography.X509Certificates;
using PdfSharp.Drawing;
using PdfSharp.Pdf;
using BouncyCastleSigner; // Hypothetical namespace for illustration
// Ensure that you have the appropriate namespaces added
class Program
{
static void Main(string[] args)
{
// Create a font for the appearance of the signature
var font = new XFont("Verdana", 10.0, XFontStyle.Regular);
// Create a new PDF document
var document = new PdfDocument();
var pdfPage = document.AddPage();
// Prepare graphics for drawing on the PDF page
var xGraphics = XGraphics.FromPdfPage(pdfPage);
var layoutRectangle = new XRect(0.0, 0.0, pdfPage.Width.Point, pdfPage.Height.Point);
// Add some text to the page
xGraphics.DrawString("Signed sample document", font, XBrushes.Black, layoutRectangle, XStringFormats.TopCenter);
// Define digital signature appearance options
var options = new DigitalSignatureOptions
{
ContactInfo = "John Doe",
Location = "Seattle",
Reason = "License Agreement",
Rectangle = new XRect(36.0, 700.0, 400.0, 50.0),
AppearanceHandler = new SignatureAppearanceHandler()
};
// Sign the document using BouncyCastle signer
var pdfSignatureHandler = DigitalSignatureHandler.ForDocument(document,
new PdfSharp.Snippets.Pdf.BouncyCastleSigner(GetCertificate(), PdfMessageDigestType.SHA256), options);
// Save the signed document
document.Save("PdfSharpSignature.pdf");
}
static (X509Certificate2, X509Certificate2Collection) GetCertificate()
{
// Locate the certificate file and read its data
var certFolder = "C:\\Users\\kyess\\AppData\\Roaming\\Adobe\\Acrobat\\DC\\Security";
var pfxFile = Path.Combine(certFolder, "IronSoftware.pfx");
var rawData = File.ReadAllBytes(pfxFile);
// Load the certificate using its password (example password)
var certificatePassword = "Passw0rd";
var certificate = new X509Certificate2(rawData, certificatePassword,
X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable);
// Create and return the certificate collection
var collection = new X509Certificate2Collection();
collection.Import(rawData, certificatePassword,
X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable);
return (certificate, collection);
}
}using System;
using System.IO;
using System.Security.Cryptography.X509Certificates;
using PdfSharp.Drawing;
using PdfSharp.Pdf;
using BouncyCastleSigner; // Hypothetical namespace for illustration
// Ensure that you have the appropriate namespaces added
class Program
{
static void Main(string[] args)
{
// Create a font for the appearance of the signature
var font = new XFont("Verdana", 10.0, XFontStyle.Regular);
// Create a new PDF document
var document = new PdfDocument();
var pdfPage = document.AddPage();
// Prepare graphics for drawing on the PDF page
var xGraphics = XGraphics.FromPdfPage(pdfPage);
var layoutRectangle = new XRect(0.0, 0.0, pdfPage.Width.Point, pdfPage.Height.Point);
// Add some text to the page
xGraphics.DrawString("Signed sample document", font, XBrushes.Black, layoutRectangle, XStringFormats.TopCenter);
// Define digital signature appearance options
var options = new DigitalSignatureOptions
{
ContactInfo = "John Doe",
Location = "Seattle",
Reason = "License Agreement",
Rectangle = new XRect(36.0, 700.0, 400.0, 50.0),
AppearanceHandler = new SignatureAppearanceHandler()
};
// Sign the document using BouncyCastle signer
var pdfSignatureHandler = DigitalSignatureHandler.ForDocument(document,
new PdfSharp.Snippets.Pdf.BouncyCastleSigner(GetCertificate(), PdfMessageDigestType.SHA256), options);
// Save the signed document
document.Save("PdfSharpSignature.pdf");
}
static (X509Certificate2, X509Certificate2Collection) GetCertificate()
{
// Locate the certificate file and read its data
var certFolder = "C:\\Users\\kyess\\AppData\\Roaming\\Adobe\\Acrobat\\DC\\Security";
var pfxFile = Path.Combine(certFolder, "IronSoftware.pfx");
var rawData = File.ReadAllBytes(pfxFile);
// Load the certificate using its password (example password)
var certificatePassword = "Passw0rd";
var certificate = new X509Certificate2(rawData, certificatePassword,
X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable);
// Create and return the certificate collection
var collection = new X509Certificate2Collection();
collection.Import(rawData, certificatePassword,
X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable);
return (certificate, collection);
}
}输出

正如您在这里看到的,虽然它能够创建一个数字签名字段并将证书应用于我们的新文档,但与如 IronPDF 等库相比,此过程庞大、手动且实施效率不高。
使用 IronPDF 添加数字签名
IronPDF 为开发者提供了一种简明的方法来数字签署PDF文档。
using IronPdf;
using System.Security.Cryptography.X509Certificates;
public class Program
{
static void Main(string[] args)
{
// Load the certificate with its password
var sig = new PdfSignature("IronSoftware.pfx", "your-password");
// Configure additional signature details
sig.TimestampHashAlgorithm = TimestampHashAlgorithms.SHA256;
sig.TimestampUrl = "http://timestamp.digicert.com";
sig.SignatureImage = new PdfSignatureImage("IronPdf.png", 0, new Rectangle(150, 100, 200, 200));
// Sign and save the PDF document
sig.SignPdfFile("output.pdf");
}
}using IronPdf;
using System.Security.Cryptography.X509Certificates;
public class Program
{
static void Main(string[] args)
{
// Load the certificate with its password
var sig = new PdfSignature("IronSoftware.pfx", "your-password");
// Configure additional signature details
sig.TimestampHashAlgorithm = TimestampHashAlgorithms.SHA256;
sig.TimestampUrl = "http://timestamp.digicert.com";
sig.SignatureImage = new PdfSignatureImage("IronPdf.png", 0, new Rectangle(150, 100, 200, 200));
// Sign and save the PDF document
sig.SignPdfFile("output.pdf");
}
}输出

此代码展示了如何使用 IronPDF 的 PdfSignature 类签署 PDF 文档。 程序首先创建一个 PdfSignature 对象,指定 .pfx 证书文件的位置及其密码。 然后设置附加签名属性,如散列算法(SHA256)、时间戳 URL 和用于签名的自定义图像(IronPdf.png)。
最后,调用 SignPdfFile 方法将数字签名应用于 PDF 文档并将其保存为 output.pdf。 此过程通过嵌入带时间戳和可视图像的数字签名,确保 PDF 的完整性和真实性。
PDFSharp:
- 采用 MIT 许可证的开源。
- 需要外部库(如 BouncyCastle)用于像数字签名这样的高级功能。
IronPDF:
结论:C# 中的 IronPDF 与 PDFsharp 的数字签名对比
在比较 C# 中 向 PDF 添加数字签名时,IronPDF 和 PDFsharp 都提供了各自的优势,这取决于您的项目需求。
IronPDF 是寻找简单易用的 API 来为 PDF 应用数字签名的开发者的理想选择,无论是独立的自由软件开发者还是公司的开发者,并带有现代功能。 其与数字签名应用、HTML 到 PDF 转换及其他 PDF 功能的无缝集成使其成为商业项目的绝佳选择,这些项目优先考虑易用性和快速实施。 有了付费支持和清晰的商业许可结构,IronPDF 非常适合需要直观、可靠解决方案的企业。
- PDFsharp 在基本的 PDF 创建和处理中表现出色,但缺乏 IronPDF 提供的高级功能和对数字签名的直接支持。 虽然 PDFsharp 是开源和免费使用的,但其在处理数字签名时的 API 比 IronPDF 不那么直观,开发者可能需要采用额外的解决方案或第三方库来处理这些功能。
总结来说,IronPDF 是寻找简单、快速解决方案来处理数字签名及相关 PDF 任务的开发者的最佳选择,特别是在商业环境中。 PDFsharp 更适合基本的 PDF 任务,但在数字签名的使用便捷性和功能集方面不如 IronPDF,这使其更适合较简单的项目或有额外定制需求的项目。
常见问题解答
数字签名如何确保PDF文档的真实性和完整性?
数字签名使用加密技术来验证PDF文档在签署后未被更改。它们确认签署者的身份并确保文档内容保持不变,从而提供安全性和法律效力。
开发人员在使用PDFsharp添加数字签名时可能面临哪些挑战?
使用PDFsharp添加数字签名时,开发人员通常需要集成第三方库如BouncyCastle。这一要求可能会使实施过程复杂化,因为开发人员需要管理额外的依赖项来创建数字证书并签署PDF。
为什么开发人员可能会选择商业PDF库而非开源库进行数字签名?
商业PDF库提供了高级功能、内置的数字签名支持以及用户友好的API。这些属性使其更适合开发人员寻求快速、可靠解决方案,尤其是在时间和效率至关重要的商业项目中。
如何在.NET应用程序中将HTML转换为PDF?
HTML可以通过使用如IronPDF的库在.NET应用程序中转换为PDF,该库提供的方法如RenderHtmlAsPdf可以直接将HTML字符串或文件转换为PDF文档,支持各种样式和脚本选项。
在商业项目中使用IronPDF处理PDF有哪些好处?
IronPDF提供了处理PDF的完整功能集,包括数字签名、HTML到PDF转换和表单处理。其强大的支持和用户友好的API使其非常适合优先考虑高级功能和快速部署的商业项目。
可以使用.NET库向PDF数字签名添加时间戳吗?
是的,使用如IronPDF的库,可以在签名配置中指定时间戳服务器URL来向PDF数字签名添加时间戳。此功能增强了已签署文档的可信度和法律合规性。
在选择用于实现数字签名的PDF库时应考虑哪些因素?
在选择用于数字签名的PDF库时,考虑因素包括易用性、高级功能的支持、与第三方工具的集成能力以及专用客户支持的可用性,这些通常在如IronPDF的商业库中更为全面。






