数字签名广泛用于保护PDF文档,可见数字签名在日常生活中是相当重要的。本篇文章将介绍添加PDF数字签名的方法
使用组件:免费版的PDF组件-Free Spire.PDF(安装好该控件后,将Spire.PDF.dll文件引用到该项目,路径"...\Spire.pdf-fe\Bin\NET4.0\ Spire.PDF.dll")。
C#(主要代码段)
- //新建一个PDF文档对象,再添加一个新页面。
- PdfDocument doc = new PdfDocument();
- doc.Pages.Add();
- //加载一个PDF证书
- PdfCertificate cert = new PdfCertificate(@"C:\Users\Administrator\Desktop\gary.pfx", "e-iceblue");
- //添加数字签名
- var signature = new PdfSignature(doc, doc.Pages[0], cert, "Requestd1");
- //设置数字签名的位置
- signature.Bounds = new RectangleF(new PointF(280, 600), new SizeF(260, 90));
- //设置显示文本属性
- signature.IsTag = true;
- //填充数字签名的内容
- signature.DigitalSignerLable = "Digitally signed by";
- signature.DigitalSigner = "Gary for Test";
- signature.DistinguishedName = "DN:";
- signature.LocationInfoLabel = "Location:";
- signature.LocationInfo = "London";
- signature.ReasonLabel = "Reason: ";
- signature.Reason = "Le document est certifie";
- signature.DateLabel = "Date: ";
- signature.Date = DateTime.Now;
- signature.ContactInfoLabel = "Contact: ";
- signature.ContactInfo = "123456789";
- signature.Certificated = false;
- signature.ConfigGraphicType = ConfiguerGraphicType.TextSignInformation;
- //设置数字签名的文档权限
- signature.DocumentPermissions = PdfCertificationFlags.ForbidChanges;
- //将文档以PDF格式保存到文件夹中
- doc.SaveToFile("sample.pdf");
- System.Diagnostics.Process.Start("sample.pdf");
运行结果: