itext生成PDF设置页眉页脚的实例详解

2025-05-29 0 17

itext生成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
/**

* 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();

}

}

}

工程结构图:

itext生成PDF设置页眉页脚的实例详解

itext生成PDF设置页眉页脚的实例详解

itext生成PDF设置页眉页脚的实例详解

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

原文链接:http://blog.csdn.net/tujiyue/article/details/6528372

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 itext生成PDF设置页眉页脚的实例详解 https://www.kuaiidc.com/115002.html

相关文章

发表评论
暂无评论