itext生成pdf设置页眉页脚的实例详解
实例代码:
/**
* 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, [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();
}
}
}
工程结构图:
如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!