itext生成PDF设置页眉页脚的实例详解
作者:tujiyue 时间:2023-12-05 19:04:45
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();
}
}
}
工程结构图:
如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
来源:http://blog.csdn.net/tujiyue/article/details/6528372
标签:itext,PDF
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Unity实现Flappy Bird游戏开发实战
2023-03-10 19:56:58
![](https://img.aspxhome.com/file/2023/6/89856_0s.jpg)
java上乘武功入门--反射
2021-06-08 21:32:27
![](https://img.aspxhome.com/file/2023/3/85413_0s.png)
如何使用Java redis实现发送手机验证码功能
2023-11-26 17:25:00
java使用Hashtable过滤数组中重复值的方法
2023-10-22 06:24:08
详解如何实现SpringBoot的底层注解
2023-11-23 06:21:04
![](https://img.aspxhome.com/file/2023/0/59040_0s.png)
Java实现简单通讯录管理系统
2022-06-28 01:15:54
C#多线程学习之(三)生产者和消费者用法分析
2021-08-11 01:55:26
了解Java线程池执行原理
2022-10-12 13:00:23
![](https://img.aspxhome.com/file/2023/5/87175_0s.jpg)
SpringBoot解决跨域请求拦截问题代码实例
2021-07-18 12:08:54
![](https://img.aspxhome.com/file/2023/4/67964_0s.png)
详解App保活实现原理
2022-01-20 17:59:10
Java中static变量作用和用法详解
2021-11-09 12:01:04
mybatis-plus 查询传入参数Map,返回List<Map>方式
2022-01-05 13:39:24
![](https://img.aspxhome.com/file/2023/2/72592_0s.png)
Android Studio实现简易计算器App (Java语言版)
2023-08-02 01:05:39
![](https://img.aspxhome.com/file/2023/5/98625_0s.gif)
c#线程Thread示例
2022-10-21 00:38:56
Android仿QQ在状态栏显示登录状态效果
2021-10-09 04:12:06
![](https://img.aspxhome.com/file/2023/5/118945_0s.jpg)
Android使用RSA加密实现接口调用时的校验功能
2023-11-06 15:24:51
![](https://img.aspxhome.com/file/2023/1/113321_0s.jpg)
java如何使用Lombok更优雅地编码
2022-07-24 23:24:50
Android开发之RecyclerView控件
2023-08-14 08:57:37
![](https://img.aspxhome.com/file/2023/1/123291_0s.png)
android实现来电静音示例(监听来电)
2021-09-23 09:43:48
详解Android Studio安装ButterKnife插件(手动安装)
2023-07-06 13:59:32
![](https://img.aspxhome.com/file/2023/9/138269_0s.png)