钤记(印章)是一种在合同、单子、公牍等文件中表明执法效应、部门构造威望的主要指导物,常见于种种花样的文件、文档中。关于纸质文档能够手动盖印,但关于电子文档,则须要经由过程特定的要领来完成。本篇文档分享经由过程Java代码在PDF文档中增加钤记的要领。内容将分两部分引见:
1. 增加图片钤记。即经由过程加载现有的钤记(以图片情势),增加到PDF指定页面位置
2. 增加动态钤记。即加载PDF文档,并在动态的增加印章内容,包含印章字样、日期、时候、经办人、构造称号等。
运用东西:Free Spire.PDF for Java v2.0.0(免费版)
关于jar文件导入:
步骤1:步骤1:在Java顺序中新建一个文件夹可命名为Lib。并将产物包中的2个jar文件复制到新建的文件夹下。
步骤2:复制文件后,增加到援用类库:选中这两个jar文件,点击鼠标右键,挑选“Build Path” – “Add to Build Path”。完成援用。
Java示例(供参考)
1. 增加图片钤记
import com.spire.pdf.FileFormat; import com.spire.pdf.PdfDocument; import com.spire.pdf.PdfPageBase; import com.spire.pdf.annotations.PdfRubberStampAnnotation; import com.spire.pdf.annotations.appearance.PdfAppearance; import com.spire.pdf.graphics.PdfImage; import com.spire.pdf.graphics.PdfTemplate; import java.awt.geom.Rectangle2D; public class ImageStamp { public static void main(String[] args) { //建立PdfDocument对象,加载PDF测试文档 PdfDocument doc = new PdfDocument(); doc.loadFromFile("test.pdf"); //猎取文档第3页 PdfPageBase page = doc.getPages().get(2); //加载印章图片 PdfImage image = PdfImage.fromFile("stamp.png"); //猎取印章图片的宽度和高度 int width = image.getWidth(); int height = image.getHeight(); //建立PdfTemplate对象 PdfTemplate template = new PdfTemplate(width, height); //将图片绘制到模板 template.getGraphics().drawImage(image, 0, 0, width, height); //建立PdfRubebrStampAnnotation对象,指定大小和位置 Rectangle2D rect = new Rectangle2D.Float((float) (page.getActualSize().getWidth() - width - 10), (float) (page.getActualSize().getHeight() - height - 60), width, height); PdfRubberStampAnnotation stamp = new PdfRubberStampAnnotation(rect); //建立PdfAppearance对象 PdfAppearance pdfAppearance = new PdfAppearance(stamp); //将模板运用为PdfAppearance的平常状况 pdfAppearance.setNormal(template); //将PdfAppearance 运用为钤记的款式 stamp.setAppearance(pdfAppearance); //增加钤记到PDF page.getAnnotationsWidget().add(stamp); //保存文档 doc.saveToFile("ImageStamp.pdf",FileFormat.PDF); } }
图片钤记增加结果:
2.增加动态钤记
import com.spire.pdf.PdfDocument; import com.spire.pdf.PdfPageBase; 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("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(new PdfRGBColor(Color.blue)); Rectangle2D rect1 = new Rectangle2D.Float(); rect1.setFrame(new Point2D.Float(0,0),template.getSize()); //建立圆角矩形途径 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(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()-150)), 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); } }
动态钤记增加结果:
(本文完)
以上就是Java处置惩罚PDF钤记的要领示例(代码)的细致内容,更多请关注ki4网别的相干文章!