美文网首页
Java 添加动态图章和图片图章到PDF

Java 添加动态图章和图片图章到PDF

作者: Tina_Tang | 来源:发表于2020-03-03 10:46 被阅读0次

       在日常办公中,时常需对公司内部的规章制度或对外的文件、报告等进行盖印公章,使其具有法律效力。对于纸质版文档来说,只需手动盖印。而电子档则需通过特定的方式来添加,且不同性质的文档需添加不同的图章。本文将通过使用Java程序来演示如何添加动态图章和图片图章到PDF文档中。

使用工具:Free Spire.PDF for Java(免费版)

Jar文件获取及导入:

方法1通过官网下载获取jar包。解压后将lib文件夹下的Spire.Pdf.jar文件导入Java程序。(如下图)

方法2通过maven仓库安装导入。具体安装详情参见此网页

【示例1】添加动态图章

动态图章通常由动态文字(如日期、时间)、经办人/组织名称、“已审核”等字样组成。

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("C:\\Users\\Test1\\Desktop\\Sample.pdf");

//获取第1页

        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("output/DynamicStamp.pdf");

document.close();

}

//将日期转化成字符串

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

SimpleDateFormat loFormat =new SimpleDateFormat(pcFormat);

return loFormat.format(poDate);

}

}

动态图章添加效果:

【示例2】添加图片图章

图片图章是以图片格式保存的现有印章。

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("C:\\Users\\Test1\\Desktop\\Sample.pdf");

//获取文档第1页

        PdfPageBase page = doc.getPages().get(0);

//加载印章图片

        PdfImage image = PdfImage.fromFile("C:\\Users\\Test1\\Desktop\\Image.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("output/ImageStamp.pdf",FileFormat.PDF);

}

}

图片图章添加效果:

(本文完)

相关文章

  • Java 添加动态图章和图片图章到PDF

    在日常办公中,时常需对公司内部的规章制度或对外的文件、报告等进行盖印公章,使其具有法律效力。对于纸质版文档来说,...

  • PDF文件添加图片怎么操作

    图片有助于帮助读者理解文章和增强读者阅读体验的作用。那么,PDF文件添加图片怎么操作呢?众所周知,虽然PDF文件格...

  • 如何在PDF编辑器中使用图章工具

    如何在PDF编辑器中使用图章工具呢?现在在使用PDF文件的时候,想要给PDF添加一个图章不知道怎么做,想要给PDF...

  • 技能!如何在PDF文档创建图章?

    关于PDF文件编辑的话,我们总是会遇到各种问题。比如说如何给PDF文件添加图章的问题,很多小伙伴都会不解。图章对于...

  • PDF阅读器可以添加个性图章!

    之前我们说过更新的轻快PDF阅读器出现了很多的新功能,比如说图章功能。但是我只说了怎么去添加图章,但是图章的种类就...

  • 如何给PDF添加电子图章

    如何给PDF添加电子图章工作中有许多PDF文件需要盖章。诸如财务报表、合同、内部审核文件等。如果每次都采用打印后再...

  • 敲图章和N/A

    在厦门鼓浪屿,几乎所有的小店里都会售卖景点护照,游客可以带着这本护照一个个景点和特色店逛下去,每到一个景点或特色店...

  • PDF如何创建动态图章?它来帮忙

    PDF文件是一种常用的文件格式,不管是工作亦或者是生活,都会用的PDF文件。但是很多的PDF文件无法更改,如何给P...

  • Java 添加页眉页脚到PDF

    页眉和页脚通常是显示文档的附加信息,常用来插入页码、时间、日期、个人信息、微标等。特别是其中插入的页码,通...

  • 如何给gif图片添加文字?GIF图片添加文字教程

    我们经常会在网上下载一下GIF动态图片,有时候我们想给我们下载的GIF动态图片添加文字,这时候该怎么操作,下面小编...

网友评论

      本文标题:Java 添加动态图章和图片图章到PDF

      本文链接:https://www.haomeiwen.com/subject/qvxqlhtx.html