iText生成PDF文件,每页显示页码以及总页数的实现

如果想在页眉或者页脚加上第x页这样的信息,用普通的HeaderFooter就能轻松搞定。
HeaderFooter footer = new HeaderFooter(new Phrase("第:",FontChinese), new Phrase("页",FontChinese));    
footer.setBorder(Rectangle.NO_BORDER);
document.setFooter(footer);
document.open();


如果想在每页显示第x页/共y页这样的功能,实现起来没有那么轻松。
官方页面有具体实现的例子,连接地址:
[url]http://itextdocs.lowagie.com/tutorial/directcontent/pageevents/index.php[/url]
源码:
[url]http://itextdocs.lowagie.com/examples/com/lowagie/examples/directcontent/pageevents/PageNumbersWatermark.java[/url]

源码里面的东西比较繁多,精简以后如下:


import java.io.FileOutputStream;

import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.ExceptionConverter;
import com.lowagie.text.Font;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfPageEventHelper;
import com.lowagie.text.pdf.PdfTemplate;
import com.lowagie.text.pdf.PdfWriter;

public class PdfExport extends PdfPageEventHelper{

public PdfTemplate tpl;
public BaseFont bf;

public static void main(String[] args) {
Document document = new Document(PageSize.A4, 20, 20, 20, 20);
try {
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("D:\\HelloItext.pdf"));
writer.setPageEvent(new PdfExport());

BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);

document.open();

Paragraph title = new Paragraph("测试内容。。。。", new Font(bfChinese,15));
title.setAlignment(Element.ALIGN_CENTER);
document.add(title);
} catch (Exception de) {
de.printStackTrace();
}
document.close();
}

public void onOpenDocument(PdfWriter writer, Document document) {
try {
tpl = writer.getDirectContent().createTemplate(100, 100);
bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
}
catch(Exception e) {
throw new ExceptionConverter(e);
}
}

public void onEndPage(PdfWriter writer, Document document) {
//在每页结束的时候把“第x页”信息写道模版指定位置
PdfContentByte cb = writer.getDirectContent();
cb.saveState();
String text = "第" + writer.getPageNumber() + "页,共";
cb.beginText();
cb.setFontAndSize(bf, 8);
cb.setTextMatrix(460, 786);//定位“第x页,共” 在具体的页面调试时候需要更改这xy的坐标
cb.showText(text);
cb.endText();
cb.addTemplate(tpl, 492, 786);//定位“y页” 在具体的页面调试时候需要更改这xy的坐标

cb.saveState();
cb.stroke();
cb.restoreState();
cb.closePath();//sanityCheck();
}

public void onCloseDocument(PdfWriter writer, Document document) {
//关闭document的时候获取总页数,并把总页数按模版写道之前预留的位置
tpl.beginText();
tpl.setFontAndSize(bf, 8);
tpl.showText(Integer.toString(writer.getPageNumber() - 1)+"页");
tpl.endText();
tpl.closePath();//sanityCheck();
}
}
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值