本文介绍使用spire.pdf for .net在pdf文档中添加可视化数字签名和不可见数字签名的方法 。
1、添加可视化数字签名
c#
using spire.pdf;
using spire.pdf.graphics;
using spire.pdf.interactive.digitalsignatures;
using system.drawing;
using system.security.cryptography.x509certificates;
namespace visibledigitalsignature
{
class program
{
static void main(string[] args)
{
//加载pdf测试文档
pdfdocument pdf = new pdfdocument();
pdf.loadfromfile("sample.pdf");
//获取pdf文档中的第二页
pdfpagebase page = pdf.pages[1];
//在页面中的指定位置添加可视化签名
x509certificate2 x509 = new x509certificate2("cer.pfx", "e-iceblue");
pdfordinarysignaturemaker signaturemaker = new pdfordinarysignaturemaker(pdf, x509);
ipdfsignatureappearance signatureappearance = new pdfcustomsignatureappearance();
signaturemaker.makesignature("signname_" , page, page.actualsize.width-300,page.actualsize.height-300,150,150, signatureappearance);
//保存文档
pdf.savetofile("visibledigitalsignature.pdf", fileformat.pdf);
system.diagnostics.process.start("visibledigitalsignature.pdf");
pdf.close();
}
public class pdfcustomsignatureappearance : ipdfsignatureappearance
{
public void generate(pdfcanvas g)
{
float fontsize = 15;
pdftruetypefont font = new pdftruetypefont(new font("arial", fontsize), true);
float lineheight = fontsize; g.drawstring("sign here:", font, pdfbrushes.black, new pointf(0, 0));
g.drawimage(pdfimage.fromfile("sign.png"), new pointf(20, 20));
}
}
}
}
vb.net
imports spire.pdf
imports spire.pdf.graphics
imports spire.pdf.interactive.digitalsignatures
imports system.drawing
imports system.security.cryptography.x509certificates
namespace visibledigitalsignature
class program
private shared sub main(args as string())
'加载pdf测试文档
dim pdf as new pdfdocument()
pdf.loadfromfile("sample.pdf")
'获取pdf文档中的第二页
dim page as pdfpagebase = pdf.pages(1)
'在页面中的指定位置添加可视化签名
dim x509 as new x509certificate2("cer.pfx", "e-iceblue")
dim signaturemaker as new pdfordinarysignaturemaker(pdf, x509)
dim signatureappearance as ipdfsignatureappearance = new pdfcustomsignatureappearance()
signaturemaker.makesignature("signname_", page, page.actualsize.width - 300, page.actualsize.height - 300, 150, 150, _
signatureappearance)
'保存文档
pdf.savetofile("visibledigitalsignature.pdf", fileformat.pdf)
system.diagnostics.process.start("visibledigitalsignature.pdf")
pdf.close()
end sub
public class pdfcustomsignatureappearance
implements ipdfsignatureappearance
public sub generate(g as pdfcanvas)
dim fontsize as single = 15
dim font as new pdftruetypefont(new font("arial", fontsize), true)
dim lineheight as single = fontsize
g.drawstring("sign here:", font, pdfbrushes.black, new pointf(0, 0))
g.drawimage(pdfimage.fromfile("sign.png"), new pointf(20, 20))
end sub
end class
end class
end namespace
可视化数字签名效果:
2、添加不可见数字签名
c#
using spire.pdf;
using spire.pdf.interactive.digitalsignatures;
using system.security.cryptography.x509certificates;
namespace invisibledigitalsignature
{
class program
{
static void main(string[] args)
{
//初始化一个pdfdocument实例
pdfdocument pdf = new pdfdocument();
//加载pdf文档
pdf.loadfromfile("sample.pdf");
//添加签名
x509certificate2 x509 = new x509certificate2(@"cer.pfx", "e-iceblue");
pdfordinarysignaturemaker signaturemaker = new pdfordinarysignaturemaker(pdf, x509);
signaturemaker.makesignature("signname");
signaturemaker.setlocation("chengdu");
signaturemaker.setreason("edit limited");
//保存文档
pdf.savetofile("result.pdf", fileformat.pdf);
system.diagnostics.process.start("result.pdf");
}
}
}
vb.net
imports spire.pdf
imports spire.pdf.interactive.digitalsignatures
imports system.security.cryptography.x509certificates
namespace invisibledigitalsignature
class program
private shared sub main(args as string())
'初始化一个pdfdocument实例
dim pdf as new pdfdocument()
'加载pdf文档
pdf.loadfromfile("sample.pdf")
'添加签名
dim x509 as new x509certificate2("cer.pfx", "e-iceblue")
dim signaturemaker as new pdfordinarysignaturemaker(pdf, x509)
signaturemaker.makesignature("signname")
signaturemaker.setlocation("chengdu")
signaturemaker.setreason("edit limited")
'保存文档
pdf.savetofile("result.pdf", fileformat.pdf)
system.diagnostics.process.start("result.pdf")
end sub
end class
end namespace
数字签名效果: