如果想在页眉或者页脚加上第x页这样的信息,用普通的HeaderFooter就能轻松搞定。
HeaderFooter footer = new HeaderFooter(new Phrase("第:",FontChinese), new Phrase("页",FontChinese));
footer.setBorder(Rectangle.NO_BORDER);
document.setFooter(footer);
document.open();
如果想在每页显示第x页/共y页这样的功能,实现起来没有那么轻松。
官方页面有具体实现的例子,连接地址:
http://itextdocs.lowagie.com/tutorial/directcontent/pageevents/index.php
源码:
http://itextdocs.lowagie.com/examples/com/lowagie/examples/directcontent/pageevents/PageNumbersWatermark.java
源码里面的东西比较繁多,精简以后如下:
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();
}
}
分享到:
- 2009-11-10 16:35
- 浏览 18089
- 评论(1)
- 论坛回复 / 浏览 (1 / 12049)
- 查看更多
相关推荐
itext 生成pdf 目录,itext 生成pdf 目录,itext 生成pdf 目录
iText生成pdf解决中文不显示字库,pdf凉字不显示,由于生成iText插件生成pdf的时候中文会显示不出来,遇到过的是"凉"字,查到是字体库的原因,网上下载字体库msyh.ttc,生成的时候指定字体库,就可以解决了,小bug一...
Android使用iText生成pdf并读取pdf内容,iText是著名的开放源码的站点sourceforge的一个项目,可用来生成Pdf文件和读取Pdf文件内容.
使用IText生成PDF和WORD文档 包括源码和IText所需jar包
itext7 html转换为pdf;iText7页码、页眉、页脚,itext 的复杂表格实现;完整springboot项目代码
此文档主要讲述了使用Java组件iText来生成PDF,操作PDF
利用itext 生成pdf 文件,可以自定义生成的格式,字体的大小,颜色
html渲染页面转换为pdf并设置页眉,页码,水印,目录,二维码
实现 在生成的PDF封面显示总页数, 在每页显示页眉、页脚, 在页脚显示 第几页/共几页 实现 在生成的PDF封面显示总页数, 在每页显示页眉、页脚, 在页脚显示 第几页/共几页
java解决Itext生成PDF中文不换行的问题
NULL 博文链接:https://aa00aa00.iteye.com/blog/1124216
NULL 博文链接:https://x125858805.iteye.com/blog/2239773
java使用itext实现pdf文件下载小案例,通过系统路径下载,和通过浏览器下载
在Java代码中使用itext生成PDF文档,实现PDF文档的导出。
itextpdf-5.5.10.jar、itext-asian-5.2.0.jar、iText中文帮助、iText英文帮助文档 以及生成PDF文件时生成表格时单元格合并处理方法
用itext生成pdf文档,生成的内容有表格形式,文本形式,设置字体样式,文档页边距。
itext生成PDF图片文档
NULL 博文链接:https://yizhaorong.iteye.com/blog/2157848
Java IText生成PDF,~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
用itext方法生成 与Word一样的目录