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("宋体", Font.PLAIN ,15), true);
PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("宋体", 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);
}
}