itext生成pdf间距_itext生成PDF设置页眉页脚的实例详解

本文提供了一个使用iText库生成PDF并设置页眉、页脚和页码的Java实例。示例代码详细展示了如何添加列表、设置字体、创建页眉和页脚,以及调整元素间距。此外,还展示了如何添加注释。
摘要由CSDN通过智能技术生成

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("-",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();

}

}

}

工程结构图:

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值