pdf动态图章通常由动态文字(如日期、时间)、经办人/组织名称、“已审核”等字样组成。本教程展示如何使用spire.pdf for java在pdf中添加动态图章。
import com.spire.pdf.annotations.pdfrubberstampannotation;
import com.spire.pdf.annotations.appearance.pdfappearance;
import com.spire.pdf.graphics.*;
import java.awt.*;
import java.awt.geom.point2d;
import java.awt.geom.rectangle2d;
import java.text.simpledateformat;
public class dynamicstamp {
public static void main(string[] args) {
//创建pdfdocument对象
pdfdocument document = new pdfdocument();
//加载pdf文档
document.loadfromfile("c:\\users\\administrator\\desktop\\test.pdf");
//获取第一页
pdfpagebase page = document.getpages().get(0);
//创建pdftamplate对象
pdftemplate template = new pdftemplate(185, 50);
//创建两种字体
pdftruetypefont font1 = new pdftruetypefont(new font("arial unicode ms", font.plain ,15), true);
pdftruetypefont font2 = new pdftruetypefont(new font("arial unicode ms", font.plain ,10), true);
//创建单色画刷和渐变画刷
pdfsolidbrush solidbrush = new pdfsolidbrush(new pdfrgbcolor(color.blue));
rectangle2d rect1 = new rectangle2d.float();
rect1.setframe(new point2d.float(0,0),template.getsize());
pdflineargradientbrush lineargradientbrush = new pdflineargradientbrush(rect1,new pdfrgbcolor(color.white),new pdfrgbcolor(color.orange),pdflineargradientmode.horizontal);
//创建圆角矩形路径
int cornerradius = 20;
pdfpath path = new pdfpath();
path.addarc(template.getbounds().getx(), template.getbounds().gety(), cornerradius, cornerradius, 180, 90);
path.addarc(template.getbounds().getx() template.getwidth() - cornerradius,template.getbounds().gety(), cornerradius, cornerradius, 270, 90);
path.addarc(template.getbounds().getx() template.getwidth() - cornerradius, template.getbounds().gety() template.getheight() - cornerradius, cornerradius, cornerradius, 0, 90);
path.addarc(template.getbounds().getx(), template.getbounds().gety() template.getheight() - cornerradius, cornerradius, cornerradius, 90, 90);
path.addline( template.getbounds().getx(), template.getbounds().gety() template.getheight() - cornerradius, template.getbounds().getx(), template.getbounds().gety() cornerradius / 2);
//绘制路径到模板,并进行填充
template.getgraphics().drawpath(lineargradientbrush, path);
template.getgraphics().drawpath(pdfpens.getblue(), path);
//在模板上绘制文字及动态日期
string s1 = "已审核\n";
string s2 = "销售部 " datetostring(new java.util.date(),"yyyy-mm-dd hh:mm:ss");
template.getgraphics().drawstring(s1, font1, solidbrush, new point2d.float(5, 5));
template.getgraphics().drawstring(s2, font2, solidbrush, new point2d.float(5, 28));
//创建pdfrubberstampannotation对象,并指定其位置和大小
rectangle2d rect2= new rectangle2d.float();
rect2.setframe(new point2d.float((float)(page.getactualsize().getwidth()-250),(float)(page.getactualsize().getheight()-120)), template.getsize());
pdfrubberstampannotation stamp = new pdfrubberstampannotation(rect2);
//创建pdfappearance对象,应用模板为一般状态
pdfappearance appearance = new pdfappearance(stamp);
appearance.setnormal(template);
//应用样式到图章
stamp.setappearance(appearance);
//添加图章到annotation集合
page.getannotationswidget().add(stamp);
//保存文档
document.savetofile("dynamicstamp.pdf");
document.close();
}
//将日期转化成字符串
public static string datetostring(java.util.date podate,string pcformat) {
simpledateformat loformat = new simpledateformat(pcformat);
return loformat.format(podate);
}
}