pdf动态图章可以自动获取系统登录用户名、日期及时间信息,然后生成图章。本文将介绍如何使用spire.pdf在pdf文档中添加动态图章。
c#
//创建pdfdocument对象
pdfdocument doc = new pdfdocument();
//加载现有pdf文档
doc.loadfromfile(@"c:\users\administrator\desktop\系统测试.pdf");
//获取要添加动态印章的页面
pdfpagebase page = doc.pages[4];
//创建模板对象
pdftemplate template = new pdftemplate(180, 50);
//创建字体
pdfcjkstandardfont font1 = new pdfcjkstandardfont(pdfcjkfontfamily.sinotypesonglight, 16f, pdffontstyle.bold | pdffontstyle.italic);
pdftruetypefont font2 = new pdftruetypefont(new font("宋体", 10f), true);
//创建单色画刷和渐变画刷
pdfsolidbrush brush = new pdfsolidbrush(color.purple);
rectanglef rect = new rectanglef(new pointf(0, 0), template.size);
pdflineargradientbrush gradientbrush = new pdflineargradientbrush(rect, color.white, color.lightblue, pdflineargradientmode.horizontal);
//创建圆角矩形路径
int cornerradius = 10;
pdfpath path = new pdfpath();
path.addarc(template.getbounds().x, template.getbounds().y, cornerradius, cornerradius, 180, 90);
path.addarc(template.getbounds().x template.width - cornerradius, template.getbounds().y, cornerradius, cornerradius, 270, 90);
path.addarc(template.getbounds().x template.width - cornerradius, template.getbounds().y template.height - cornerradius, cornerradius, cornerradius, 0, 90);
path.addarc(template.getbounds().x, template.getbounds().y template.height - cornerradius, cornerradius, cornerradius, 90, 90);
path.addline(template.getbounds().x, template.getbounds().y template.height - cornerradius, template.getbounds().x, template.getbounds().y cornerradius / 2);
//在模板上画圆角矩形路径,并用渐变色填充
template.graphics.drawpath(gradientbrush, path);
//在模板上画圆角矩形路径,并用紫色填充路径
template.graphics.drawpath(pdfpens.purple, path);
//在模板上绘制想要的文字、系统用户名及日期
string s1 = "已审阅\n";
string s2 = system.environment.username " " datetime.now.tostring("f");
template.graphics.drawstring(s1, font1, brush, new pointf(5, 5));
template.graphics.drawstring(s2, font2, brush, new pointf(2, 28));
//创建pdfrubberstampannotation对象,并指定其位置和大小
pdfrubberstampannotation stamp = new pdfrubberstampannotation(new rectanglef(new pointf(page.actualsize.width - 250, 200), template.size));
//创建pdfapperance对象,并将模板应用为一般状态
pdfappearance apprearance = new pdfappearance(stamp);
apprearance.normal = template;
//在印章上应用pdfapperance对象(即样式)
stamp.appearance = apprearance;
//将印章添加到pdfannotation集合
page.annotationswidget.add(stamp);
//保存文档
doc.savetofile("output.pdf", fileformat.pdf);
vb.net
'创建pdfdocument对象
dim doc as new pdfdocument()
'加载现有pdf文档
doc.loadfromfile("c:\users\administrator\desktop\系统测试.pdf")
'获取要添加动态印章的页面
dim page as pdfpagebase = doc.pages(4)
'创建模板对象
dim template as new pdftemplate(180, 50)
'创建字体
dim font1 as new pdfcjkstandardfont(pdfcjkfontfamily.sinotypesonglight, 16f, pdffontstyle.bold or pdffontstyle.italic)
dim font2 as new pdftruetypefont(new font("宋体", 10f), true)
'创建单色画刷和渐变画刷
dim brush as new pdfsolidbrush(color.purple)
dim rect as new rectanglef(new pointf(0, 0), template.size)
dim gradientbrush as new pdflineargradientbrush(rect, color.white, color.lightblue, pdflineargradientmode.horizontal)
'创建圆角矩形路径
dim cornerradius as integer = 10
dim path as new pdfpath()
path.addarc(template.getbounds().x, template.getbounds().y, cornerradius, cornerradius, 180, 90)
path.addarc(template.getbounds().x template.width - cornerradius, template.getbounds().y, cornerradius, cornerradius, 270, 90)
path.addarc(template.getbounds().x template.width - cornerradius, template.getbounds().y template.height - cornerradius, cornerradius, cornerradius, 0, 90)
path.addarc(template.getbounds().x, template.getbounds().y template.height - cornerradius, cornerradius, cornerradius, 90, 90)
path.addline(template.getbounds().x, template.getbounds().y template.height - cornerradius, template.getbounds().x, template.getbounds().y cornerradius / 2)
'在模板上画圆角矩形路径,并用渐变色填充
template.graphics.drawpath(gradientbrush, path)
'在模板上画圆角矩形路径,并用紫色填充路径
template.graphics.drawpath(pdfpens.purple, path)
'在模板上绘制想要的文字、系统用户名及日期
dim s1 as [string] = "已审阅" & vblf
dim s2 as [string] = system.environment.username " " datetime.now.tostring("f")
template.graphics.drawstring(s1, font1, brush, new pointf(5, 5))
template.graphics.drawstring(s2, font2, brush, new pointf(2, 28))
'创建pdfrubberstampannotation对象,并指定其位置和大小
dim stamp as new pdfrubberstampannotation(new rectanglef(new pointf(page.actualsize.width - 250, 200), template.size))
'创建pdfapperance对象,并将模板应用为一般状态
dim apprearance as new pdfappearance(stamp)
apprearance.normal = template
'在印章上应用pdfapperance对象(即样式)
stamp.appearance = apprearance
'将印章添加到pdfannotation集合
page.annotationswidget.add(stamp)
'保存文档
doc.savetofile("output.pdf", fileformat.pdf)