本文实例为大家分享了java将图片组合成pdf文件的具体代码,供大家参考,具体内容如下
程序界面图:
代码清单:
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
|
package 将图片组合成pdf文件; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.io.file; import java.io.filefilter; import java.io.fileoutputstream; import java.io.ioexception; import javax.swing.jbutton; import javax.swing.jfilechooser; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.joptionpane; import javax.swing.jpanel; import javax.swing.jtextfield; import javax.swing.filechooser.filenameextensionfilter; import com.itextpdf.text.document; import com.itextpdf.text.documentexception; import com.itextpdf.text.image; import com.itextpdf.text.pagesize; import com.itextpdf.text.pdf.pdfwriter; /* * 绘制主界面以及处理按钮事件类——jiemian_main * * */ class jiemian_mian extends jframe{ private static final long serialversionuid = 1657254256189721759l; final private string shuoming = " 使用说明: 本程序的主要功能就是将图片组按比例缩放合成于同一pdf文件里。 ——winmin"; private string dir_open = ""; private string dir_save = ""; public jframe jf = null; private jpanel jp = null; private jbutton jb_open = null; private jbutton jb_save =null; private jbutton jb_ok = null; private jtextfield jt_dir_open = null; private jtextfield jt_dir_save = null; private jlabel jl_dir_open = null; private jlabel jl_dir_save = null; private jlabel jl_lujing_open = null; private jlabel jl_lujing_save = null; private jtextfield jtf = null; public jiemian_mian(){ jf = new jframe("将图片组合成pdf文件"); jp = new jpanel(); jp.setlayout(null); /*标签*/ jl_dir_open = new jlabel("请选择图片组所在的文件夹:"); jl_dir_save = new jlabel("请选择pdf的合成位置:"); jl_lujing_open = new jlabel("路径:"); jl_lujing_save = new jlabel("路径:"); jl_dir_open.setbounds(50, 50, 200, 20); jl_dir_save.setbounds(420, 50, 200, 20); jl_lujing_open.setbounds(50, 80, 40, 20); jl_lujing_save.setbounds(420, 80, 40, 20); jp.add(jl_dir_open); jp.add(jl_dir_save); jp.add(jl_lujing_open); jp.add(jl_lujing_save); /*按钮*/ jb_open = new jbutton("浏览"); jb_save = new jbutton("浏览"); jb_ok = new jbutton("开始合成"); jb_open.setbounds(230, 80, 65, 20); jb_save.setbounds(600, 80, 65, 20); jb_ok.setbounds(310, 165, 90, 30); jb_open.addactionlistener(new open()); jb_save.addactionlistener(new save()); jb_ok.addactionlistener(new begin()); jp.add(jb_open); jp.add(jb_save); jp.add(jb_ok); /*单行文本(路径显示)*/ jt_dir_open = new jtextfield(); jt_dir_save = new jtextfield(); jt_dir_open.setbounds(90, 80, 130, 20); jt_dir_save.setbounds(460, 80, 130, 20); jt_dir_open.seteditable(false); jt_dir_save.seteditable(false); jp.add(jt_dir_open); jp.add(jt_dir_save); /*文本框(使用说明)*/ jtf = new jtextfield(shuoming); jtf.setbounds(50,225, 615, 50); jtf.setenabled(false); jp.add(jtf); /*主框*/ jf.add(jp); jf.setsize(715,315); jf.setresizable(false); jf.setlocationrelativeto(null); jf.setvisible(true); jf.setdefaultcloseoperation(jframe.exit_on_close); } /*获取图片路径的按钮事件处理*/ private class open implements actionlistener{ public void actionperformed(actionevent e){ lujing_get lujing_get = new lujing_get(jf); //创建lujing_get对象,获取图片组路径 dir_open = lujing_get.open_get(); jt_dir_open.settext(dir_open); } } /*获取选择保存pdf所生成位置的路径的按钮事件处理*/ private class save implements actionlistener{ public void actionperformed(actionevent e){ lujing_get lujing_get = new lujing_get(jf); //创建lujing_get对象,获取选择保存pdf所生成位置的路径 dir_save = lujing_get.save_get(); jt_dir_save.settext(dir_save); } } /*合成pdf的按钮事件处理*/ private class begin implements actionlistener{ public void actionperformed(actionevent e){ /*准确处理所获取的路径*/ if(dir_save.equals("") || dir_open.equals("")){ joptionpane.showmessagedialog(jf, "请输入图片组的路径及pdf的保存路径","警告",joptionpane.warning_message); }else{ file_deal fd = new file_deal(dir_open); //创建file_deal对象,将图片组路径下的所有图片文件准确加载 if(fd.files() != null){ /*准确处理pdf的命名规则(去掉后缀名)*/ if(dir_save.lastindexof(".") != (-1)) dir_save = dir_save.substring(0, dir_save.lastindexof(".")); wm_creatpdf pdf_creat = new wm_creatpdf(fd.files(),dir_save); //创建wm_creatpdf对象,生成pdf try { pdf_creat.creatpdf(); //合成pdf文件 } catch (documentexception e1) { // todo 自动生成的 catch 块 e1.printstacktrace(); } catch (ioexception e1) { // todo 自动生成的 catch 块 e1.printstacktrace(); } joptionpane.showmessagedialog(jf,"已合成pdf文件,所在位置:"+dir_save+".pdf", "完成",joptionpane.plain_message); }else joptionpane.showmessagedialog(jf, "该文件夹下没有图片格式文件(.jpg/.png/.bmp/.tif)!", "警告",joptionpane.warning_message); } } } } class lujing_get { private jframe jf = null; public lujing_get(jframe jf){ this.jf = jf; } public string open_get(){ string dir = ""; jfilechooser jfc = new jfilechooser(); //创建“选择文件浏览器”对象 jfc.setdialogtitle("请选择图片组所在的文件夹"); jfc.setfileselectionmode(jfilechooser.directories_only); int returnval = jfc.showopendialog(jf); if(jfilechooser.approve_option == returnval){ dir = jfc.getselectedfile().tostring(); } return dir; //返回路径 } public string save_get(){ string dir = ""; jfilechooser jfc = new jfilechooser(); //创建“选择文件浏览器”对象 jfc.setdialogtitle("请选择pdf的合成位置"); filenameextensionfilter filter = new filenameextensionfilter("pdf","pdf"); jfc.setfilefilter(filter); int returnval = jfc.showsavedialog(jf); if(jfilechooser.approve_option == returnval){ dir = jfc.getselectedfile().tostring(); } return dir; //返回路径 } } /* *获取图片文件 * * */ class file_deal{ private string dir_open = ""; public file_deal(string dir_open){ this.dir_open = dir_open; } public file[] files(){ file f = new file(dir_open); file fs[] = f.listfiles(new photofilefilter()); if(fs.length != 0) return fs; else return null; } } /* * * 文件过滤器,将所在目录下的图片格式文件返回 * */ class photofilefilter implements filefilter{ @override public boolean accept(file file) { // todo 自动生成的方法存根 if(file.isdirectory()) return false; else{ string name = file.getname(); if(name.endswith(".jpg") || name.endswith(".png")) return true; else if(name.endswith(".bmp") || name.endswith(".tif")) return true; else return false; } } } /* * 生成pdf文件(此类要外部引用“itextpdf.jar”,下载地址为:http://www.java2s.com/code/jar/i/itextpdf.htm) * * */ class wm_creatpdf { final private float a4_weight = 595-60; //标准a4的宽 final private float a4_height = 842-60; //标准a4的高 private file[] files = null; private string dir_save =""; public wm_creatpdf(file[] files,string dir_save){ this.files = files; this.dir_save = dir_save; } public void creatpdf() throws documentexception, ioexception{ document document = new document(pagesize.a4,30,30,30,30); //创建文档容器 pdfwriter.getinstance(document,new fileoutputstream(dir_save+".pdf")); //创建编写器(pdf类型) document.open(); //打开容器 float percent=100; float w,h; for(int i=0;i<files.length;i++){ image img = image.getinstance(files[i].getcanonicalpath()); /*处理图片缩放比例*/ w = img.getwidth(); h = img.getheight(); if ((w > a4_weight)&&(h < a4_height)) percent = (a4_weight* 100 )/w ; else if ((w < a4_weight)&&(h > a4_height)) percent = (a4_height* 100 )/h; else if ((w > a4_weight)&&(h > a4_height)){ percent = (a4_weight* 100 )/w ; h = (h*percent)/ 100 ; if (h > a4_height) percent = (a4_height* 100 )/h; } img.scalepercent(percent); document.add(img); } document.close(); //关闭容器 } public static void main(string[] args) { // todo 自动生成的方法存根 new jiemian_mian(); } } |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/winminwu/article/details/78923643