實例代碼:
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
|
/** * itexttest * itext生成pdf加入列表,注釋等內容,同時設置頁眉和頁腳及頁碼等。 */ package com.labci.itext.test; import java.awt.color; import java.io.filenotfoundexception; import java.io.fileoutputstream; import java.io.ioexception; import com.lowagie.text.annotation; import com.lowagie.text.document; import com.lowagie.text.documentexception; import com.lowagie.text.font; import com.lowagie.text.headerfooter; import com.lowagie.text.list; import com.lowagie.text.listitem; import com.lowagie.text.phrase; import com.lowagie.text.rectangle; import com.lowagie.text.pdf.basefont; import com.lowagie.text.pdf.pdfwriter; /** * @author bill tu(tujiyue/iwtxokhtd) * jun 6, 2011[4:10:35 pm] * */ public class itextlist { private final static string result_file= "itext_list.pdf" ; public static void main(string []args){ document doc= new document(); try { pdfwriter.getinstance(doc, new fileoutputstream(result_file)); basefont fontchinese= null ; try { fontchinese = basefont.createfont( "stsong-light" , "unigb-ucs2-h" ,basefont.not_embedded); //設置中文字體 } catch (ioexception e) { e.printstacktrace(); } font chinese = new font(fontchinese, 10 , font.normal); /** * headerfooter的第2個參數為非false時代表打印頁碼 * 頁眉頁腳中也可以加入圖片,并非只能是文字 */ headerfooter header= new headerfooter( new phrase( "這僅僅是個頁眉,頁碼在頁腳處" ,chinese), false ); //設置是否有邊框等 // header.setborder(rectangle.no_border); header.setborder(rectangle.bottom); header.setalignment( 1 ); header.setbordercolor(color.red); doc.setheader(header); headerfooter footer= new headerfooter( new phrase( "-" ,chinese), new phrase( "-" ,chinese)); /** * 0是靠左 * 1是居中 * 2是居右 */ footer.setalignment( 1 ); footer.setbordercolor(color.red); footer.setborder(rectangle.box); doc.setfooter(footer); /** * 頁眉頁腳的設置一定要在open前設置好 */ doc.open(); /** * true:代表要排序,10代表序號與文字之間的間距 * false:代表不排序,則文字前的符號為"-" */ list itextlist= new list( true , 10 ); /** * 也可以改變列表的符號[可選] * $$$ * 要改變列表符號時,上面的list構造方法第一參數值必須為false * $$$ * 可以使用字符串,chunk,image等作列表符號,如下 */ //itextlist.setlistsymbol("*"); listitem firstitem= new listitem( "first paragraph" ); listitem seconditem= new listitem( "second paragraph" ); listitem thirditem= new listitem( "third paragraph" ); itextlist.add(firstitem); itextlist.add(seconditem); itextlist.add(thirditem); doc.add(itextlist); //添加注釋,注釋有標題和內容,注釋可以是文本,內部鏈接,外部鏈接,圖片等 annotation annotation= new annotation( "what's this?" , "it's a tree and it is not a big" ); doc.add(annotation); doc.close(); } catch (filenotfoundexception e) { e.printstacktrace(); } catch (documentexception e) { e.printstacktrace(); } } } |
工程結構圖:
如有疑問請留言或者到本站社區交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
原文鏈接:http://blog.csdn.net/tujiyue/article/details/6528372