itext生成PDF设置页眉页脚的实例详解
更新时间:2017年09月23日 11:41:25 作者:tujiyue
这篇文章主要介绍了itext生成PDF设置页眉页脚的实例详解的相关资料,希望通过本文能帮助到大家,需要的朋友可以参考下
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, 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();
}
}
}
工程结构图:



如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
相关文章
详解springboot通过Async注解实现异步任务及回调的方法
这篇文章主要介绍了springboot通过Async注解实现异步任务及回调,文中通过一个简单示例来直观的理解什么是同步调用,在单元测试用例中,注入 SyncTask 对象,并在测试用例中执行 doTaskOne(),doTaskTwo(),doTaskThree() 三个方法,具体实现方式跟随小编一起看看吧2022-05-05
Java多线程之线程通信生产者消费者模式及等待唤醒机制代码详解
这篇文章主要介绍了Java多线程之线程通信生产者消费者模式及等待唤醒机制代码详解,具有一定参考价值,需要的朋友可以了解下。2017-10-10
springboot实现string转json json里面带数组
这篇文章主要介绍了springboot实现string转json json里面带数组,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2022-06-06


最新评论