本文實(shí)例為大家分享了java通過pdf模板填寫pdf表單的具體代碼,包括圖片,供大家參考,具體內(nèi)容如下
需要用到的java包:
itext.jar、itextasian.jar的jar包。這個包里面定義了與中文輸出相關(guān)的一些文件。
編寫的表單如下:
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
|
import java.io.bytearrayoutputstream; import java.io.fileoutputstream; import java.io.ioexception; import java.util.hashmap; import java.util.map; import com.itextpdf.text.documentexception; import com.itextpdf.text.image; import com.itextpdf.text.rectangle; import com.itextpdf.text.pdf.acrofields; import com.itextpdf.text.pdf.basefont; import com.itextpdf.text.pdf.pdfcontentbyte; import com.itextpdf.text.pdf.pdfreader; import com.itextpdf.text.pdf.pdfstamper; /** * pdf工具類 * @author moshunwei * @since 2018-02-01 */ public class pdfutil { /** * 根據(jù)模板生成pdf * @param data map(string,object) * @return */ public static boolean createpdf(string path,map<string, object> data) { pdfreader reader = null ; acrofields s = null ; pdfstamper ps = null ; bytearrayoutputstream bos = null ; try { reader = new pdfreader( "d:\\test.pdf" ); bos = new bytearrayoutputstream(); ps = new pdfstamper(reader, bos); s = ps.getacrofields(); /** * 使用中文字體 使用 acrofields填充值的不需要在程序中設(shè)置字體,在模板文件中設(shè)置字體為中文字體 adobe 宋體 std l */ basefont bfchinese = basefont.createfont( "stsongstd-light" , "unigb-ucs2-h" , false ); /** * 設(shè)置編碼格式 */ s.addsubstitutionfont(bfchinese); // 遍歷data 給pdf表單表格賦值 for (string key : data.keyset()) { s.setfield(key,data.get(key).tostring()); } // 如果為false那么生成的pdf文件還能編輯,一定要設(shè)為true ps.setformflattening( true ); /** * 添加圖片 */ string imgpath= "d:/n5.jpg" ; int pageno = s.getfieldpositions( "img" ).get( 0 ).page; rectangle signrect = s.getfieldpositions( "img" ).get( 0 ).position; float x = signrect.getleft(); float y = signrect.getbottom(); // 讀圖片 image image = image.getinstance(imgpath); // 獲取操作的頁面 pdfcontentbyte under = ps.getovercontent(pageno); // 根據(jù)域的大小縮放圖片 image.scaletofit(signrect.getwidth(), signrect.getheight()); // 添加圖片 image.setabsoluteposition(x, y); under.addimage(image); @suppresswarnings ( "resource" ) fileoutputstream fos = new fileoutputstream( "d:\\shouju_fb.pdf" ); fos.write(bos.tobytearray()); return true ; } catch (ioexception | documentexception e) { system.out.println( "讀取文件異常" ); e.printstacktrace(); return false ; } finally { try { bos.close(); ps.close(); reader.close(); } catch (ioexception | documentexception e) { system.out.println( "關(guān)閉流異常" ); e.printstacktrace(); } } } public static void main(string[] args) { map<string, object> data = new hashmap<string, object>(); data.put( "id" , "12312321" ); data.put( "name" , "小帥哥" ); data.put( "sex" , "男" ); data.put( "age" , "21" ); pdfutil.createpdf( "d:/n5.jpg" ,data); } } |
還有相應(yīng)的編輯pdf表單的工具,默認(rèn)用adobe acrobat。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/qq_40150691/article/details/79249029?utm_source=blogxgwz3