服务器之家

服务器之家 > 正文

实例讲解Java处理PDF图章的方法

时间:2021-07-14 16:08     来源/作者:Java之家

图章(印章)是一种在合同、票据、公文等文件中表明法律效应、部门机关权威的重要指示物,常见于各种格式的文件、文档中。对于纸质文档可以手动盖章,但对于电子文档,则需要通过特定的方法来实现。本篇文档分享通过java代码在pdf文档中添加图章的方法。内容将分两部分介绍:

1. 添加图片图章。即通过加载现有的图章(以图片形式),添加到pdf指定页面位置

2. 添加动态图章。即加载pdf文档,并在动态的添加印章内容,包括印章字样、日期、时间、经办人、组织名称等。

使用工具:free spire.pdf for java v2.0.0

关于jar文件导入:

步骤1:步骤1:在java程序中新建一个文件夹可命名为lib。并将产品包中的2个jar文件复制到新建的文件夹下。

实例讲解Java处理PDF图章的方法

步骤2:复制文件后,添加到引用类库:选中这两个jar文件,点击鼠标右键,选择“build path” – “add to build path”。完成引用。

实例讲解Java处理PDF图章的方法

java示例(供参考)

1. 添加图片图章

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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);
 
 }
 
}

图片图章添加效果:

实例讲解Java处理PDF图章的方法

2.添加动态图章

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
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图章的方法

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
2021德云社封箱演出完整版 2021年德云社封箱演出在线看
2021德云社封箱演出完整版 2021年德云社封箱演出在线看 2021-03-15
返回顶部