当前位置: 移动技术网 > IT编程>开发语言>Java > Java 处理PDF图章(印章)——图片图章、动态图章

Java 处理PDF图章(印章)——图片图章、动态图章

2019年01月29日  | 移动技术网IT编程  | 我要评论

图章(印章)是一种在合同、票据、公文等文件中表明法律效应、部门机关权威的重要指示物,常见于各种格式的文件、文档中。对于纸质文档可以手动盖章,但对于电子文档,则需要通过特定的方法来实现。本篇文档分享通过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);
    }
}

动态图章添加效果:

 

(本文完)

转载请注明出处!!

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网