java 盖章_Java 处理PDF图章(印章)——图片图章、动态图章

本文档展示了如何使用Java处理PDF文件,添加图片和动态日期的图章(印章)。通过创建PdfTemplate,设置字体、颜色,绘制圆角矩形和文字,然后创建PdfRubberStampAnnotation来在PDF页面上应用这些图章。
摘要由CSDN通过智能技术生成

importcom.spire.pdf.PdfDocument;importcom.spire.pdf.PdfPageBase;importcom.spire.pdf.annotations.PdfRubberStampAnnotation;importcom.spire.pdf.annotations.appearance.PdfAppearance;import com.spire.pdf.graphics.*;import java.awt.*;importjava.awt.geom.Point2D;importjava.awt.geom.Rectangle2D;importjava.text.SimpleDateFormat;public classDynamicStamp {public static voidmain(String[] args) {//创建PdfDocument对象

PdfDocument document = newPdfDocument();//加载PDF文档

document.loadFromFile("test.pdf");//获取第3页

PdfPageBase page = document.getPages().get(2);//创建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(newPdfRGBColor(Color.blue));

Rectangle2D rect1= newRectangle2D.Float();

rect1.setFrame(new Point2D.Float(0,0),template.getSize());//创建圆角矩形路径

int CornerRadius = 20;

PdfPath path= newPdfPath();

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(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= newRectangle2D.Float();

rect2.setFrame(new Point2D.Float((float)(page.getActualSize().getWidth()-250),(float)(page.getActualSize().getHeight()-150)), template.getSize());

PdfRubberStampAnnotation stamp= newPdfRubberStampAnnotation(rect2);//创建PdfAppearance对象,应用模板为一般状态

PdfAppearance appearance = newPdfAppearance(stamp);

appearance.setNormal(template);//应用样式到图章

stamp.setAppearance(appearance);//添加图章到Annotation集合

page.getAnnotationsWidget().add(stamp);//保存文档

document.saveToFile("DynamicStamp.pdf");

document.close();

}//将日期转化成字符串

public staticString dateToString(java.util.Date poDate,String pcFormat) {

SimpleDateFormat loFormat= newSimpleDateFormat(pcFormat);returnloFormat.format(poDate);

}

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值