随着电子账单、回单、通知、合同的流行,电子文档的可信度变得非常重要。为防止非法篡改,确保文档的权威性,我们可以对pdf进行数字签名。
注意:在服务器上进行数字签名时,请将应用池配置中“加载用户配置文件”设置为true。
添加数字签名
spire.pdf支持使用pfx数字证书生成数字签名,添加签名时,请务必保证pfx证书的有效性。
c#
//初始化一个pdfdocument实例
pdfdocument doc = new pdfdocument();
//加载pdf文档
doc.loadfromfile("sample.pdf");
//根据pfx证书实例化一个pdfcertificate对象
pdfcertificate cert = new pdfcertificate("gary.pfx", "e-iceblue");
//在指定页面添加数字签名
pdfsignature signature = new pdfsignature(doc, doc.pages[0], cert, "signature1");
//设置签名的位置和大小
signature.bounds = new rectanglef(new pointf(200, 600), new sizef(180, 90));
//设置签名内容
signature.istag = true;
signature.digitalsignerlable = "digitally signed by: ";
signature.digitalsigner = "gary";
signature.locationinfolabel = "location:";
signature.locationinfo = "cn";
signature.reasonlabel = "reason: ";
signature.reason = "ensure authenticity";
signature.datelabel = "date: ";
signature.date = datetime.now;
signature.contactinfolabel = "contact number: ";
signature.contactinfo = "028-81705109";
//设置被签名文档的编辑权限
signature.documentpermissions = pdfcertificationflags.allowformfill | pdfcertificationflags.forbidchanges;
//保存文档
doc.savetofile("数字签名.pdf");
vb.net
'初始化一个pdfdocument实例
dim doc as new pdfdocument()
'加载pdf文档
doc.loadfromfile("sample.pdf")
'根据pfx证书实例化一个pdfcertificate对象
dim cert as new pdfcertificate("gary.pfx", "e-iceblue")
'在指定页面添加数字签名
dim signature as new pdfsignature(doc, doc.pages(0), cert, "signature1")
'设置签名的位置和大小
signature.bounds = new rectanglef(new pointf(200, 600), new sizef(180, 90))
'设置签名内容
signature.istag = true
signature.digitalsignerlable = "digitally signed by: "
signature.digitalsigner = "gary"
signature.locationinfolabel = "location:"
signature.locationinfo = "cn"
signature.reasonlabel = "reason: "
signature.reason = "ensure authenticity"
signature.datelabel = "date: "
signature.[date] = datetime.now
signature.contactinfolabel = "contact number: "
signature.contactinfo = "028-81705109"
'设置被签名文档的编辑权限
signature.documentpermissions = pdfcertificationflags.allowformfill or pdfcertificationflags.forbidchanges
'保存文档
doc.savetofile("数字签名.pdf")
添加数字签名域
除了直接添加数字签名以外,我们也可以在pdf文档创建数字签名域。点击该签名域,用户就可以手动添加数字签名。
c#
//新建一个pdf文档并添加一页
pdfdocument doc = new pdfdocument();
pdfpagebase page = doc.pages.add();
//实例化一个pdfsignaturefield对象,指定创建页面及名称
pdfsignaturefield signaturefield = new pdfsignaturefield(page, "signature");
//设置签名域的相关属性
signaturefield.borderwidth = 1.0f;
signaturefield.borderstyle = pdfborderstyle.solid;
signaturefield.bordercolor = new pdfrgbcolor(system.drawing.color.black);
signaturefield.highlightmode = pdfhighlightmode.outline;
signaturefield.bounds = new rectanglef(100, 100, 100, 100);
//添加签名域到fields collection
doc.form.fields.add(signaturefield);
//保存文档
doc.savetofile("addsignfield.pdf", fileformat.pdf);
vb.net
'新建一个pdf文档并添加一页
dim doc as new pdfdocument()
dim page as pdfpagebase = doc.pages.add()
'实例化一个pdfsignaturefield对象,指定创建页面及名称
dim signaturefield as new pdfsignaturefield(page, "signature")
'设置签名域的相关属性
signaturefield.borderwidth = 1f
signaturefield.borderstyle = pdfborderstyle.solid
signaturefield.bordercolor = new pdfrgbcolor(system.drawing.color.black)
signaturefield.highlightmode = pdfhighlightmode.outline
signaturefield.bounds = new rectanglef(100, 100, 100, 100)
'添加签名域到fields collection
doc.form.fields.add(signaturefield)
'保存文档
doc.savetofile("addsignfield.pdf", fileformat.pdf)