C# 和 VB.net 都可以使用 iTextSharp 库来实现 PDF 文件添加数字签名。数字签名可以是可视化的,也可以是不可见的。
以下是实现 PDF 添加数字签名的完整攻略:
步骤 1:引入 iTextSharp 库
在项目中引入 iTextSharp 库。通常会从 NuGet 软件包管理器中安装该库,或者从官方网站 https://github.com/itext/itextsharp 下载。
步骤 2:创建签名类
首先,创建一个签名类,该类继承了 IExternalSignature 接口,实现了 Sign 方法。在 Sign 方法中,我们可以使用数字证书对 PDF 签名,并将签名数据写入 PDF 文件。
public class MyExternalSignature : IExternalSignature
{
private string _pkcs12FilePath;
private string _password;
public MyExternalSignature(string pkcs12FilePath, string password)
{
_pkcs12FilePath = pkcs12FilePath;
_password = password;
}
public string GetHashAlgorithm()
{
return DigestAlgorithms.SHA256;
}
public string GetEncryptionAlgorithm()
{
return EncryptionAlgorithm.RSA;
}
public byte[] Sign(byte[] message)
{
X509Certificate2 pkcs12 = new X509Certificate2(_pkcs12FilePath, _password);
RSACryptoServiceProvider key = (RSACryptoServiceProvider)pkcs12.PrivateKey;
Org.BouncyCastle.X509.X509Certificate[] chain = new Org.BouncyCastle.X509.X509Certificate[] { DotNetUtilities.FromX509Certificate(pkcs12) };
IExternalSignature signature = new PrivateKeySignature(key, GetHashAlgorithm());
return signature.Sign(message);
}
}
步骤 3:创建 PDF 签名
接下来,我们需要创建 PDF 签名。以下是一个简单的方法,它接受输入文件名,数字证书和输出文件名。其中,数字证书作为参数传递给 MyExternalSignature 类的构造函数。
private void SignPdf(string inputFilePath, string certFilePath, string certPassword, string outputFilePath)
{
var reader = new PdfReader(inputFilePath);
var stamper = PdfStamper.CreateSignature(reader, new FileStream(outputFilePath, FileMode.Create), '\0');
var appearance = stamper.SignatureAppearance;
appearance.SetVisibleSignature("Signed by me");
appearance.Reason = "Signed by me";
appearance.Location = "Shanghai";
var externalSignature = new MyExternalSignature(certFilePath, certPassword);
MakeSignature.SignDetached(appearance, externalSignature, new Org.BouncyCastle.X509.X509Certificate[] { });
}
示例 1: 可视化签名
以下是一个多页 PDF 文件的示例。此示例在第一页顶部添加了可视化签名。
var inputFilePath = "input.pdf";
var outputFilePath = "output.pdf";
var certFilePath = "cert.pfx";
var certPassword = "password";
var reader = new PdfReader(inputFilePath);
var stamper = PdfStamper.CreateSignature(reader, new FileStream(outputFilePath, FileMode.Create), '\0');
var signaturePage = 1;
var pageSize = reader.GetPageSizeWithRotation(signaturePage);
var x = pageSize.Left + 10;
var y = pageSize.Top - 10;
var signatureField = PdfFormField.CreateSignature(stamper.Writer);
signatureField.FieldName = "Signature1";
signatureField.SetWidget(new iTextSharp.text.Rectangle(x, y, x + 200, y + 50), PdfAnnotation.HighlightInvert);
signatureField.SetPage(signaturePage);
signatureField.MKBorderColor = BaseColor.GRAY;
signatureField.MKBackgroundColor = BaseColor.LIGHT_GRAY;
stamper.AddAnnotation(signatureField, signaturePage);
var appearance = stamper.SignatureAppearance;
appearance.SetVisibleSignature(new Rectangle(x, y, x + 200, y + 50), signaturePage, "Signed by me");
appearance.SignatureRenderingMode = PdfSignatureAppearance.RenderingMode.GRAPHIC;
appearance.CertificationLevel = PdfSignatureAppearance.CERTIFIED_NO_CHANGES_ALLOWED;
appearance.Reason = "Signed by me";
appearance.Location = "Shanghai";
var externalSignature = new MyExternalSignature(certFilePath, certPassword);
MakeSignature.SignDetached(appearance, externalSignature, new Org.BouncyCastle.X509.X509Certificate[] { });
stamper.Close();
此示例首先创建了一个 signatureField 对象并将其添加到第一页的指定位置。然后,它设置了可视化签名的外观,并使用 MakeSignature.SignDetached 方法进行签名。
示例 2: 不可视化签名
以下示例使用不可视化签名签署 PDF 文件。
var inputFilePath = "input.pdf";
var outputFilePath = "output.pdf";
var certFilePath = "cert.pfx";
var certPassword = "password";
var externalSignature = new MyExternalSignature(certFilePath, certPassword);
using (Stream input = new FileStream(inputFilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
using (Stream output = new FileStream(outputFilePath, FileMode.Create, FileAccess.Write, FileShare.None))
{
var signer = new PdfSigner(new PdfReader(input), output, new StampingProperties().UseAppendMode());
signer.SetCertificationLevel(PdfSigner.NOT_CERTIFIED);
signer.SignExternalContainer(externalSignature, 8192);
signer.Document.Close();
}
此示例使用 PdfSigner 类来签署文档。它设置了不可视化签名,并最终使用 externalSignature 对象来签署 PDF 文件。
希望这个攻略对您有所帮助!
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:C#和vb.net实现PDF 添加可视化和不可见数字签名 - Python技术站