1.概述
       iText5中并没有之前版本HeaderFooter对象设置页眉和页脚,可以利用PdfPageEventHelper来完成页眉页脚的设置工作。PdfPageEventHelper中包含以下事件处理器。
       onOpenDocument() — 当打开一个文档时触发,可以用于初始化文档的全局变量。
       onStartPage() — 当一个页面初始化时触发,可用于初始化页面的设置参数,但是注意这个函数触发时,该页面并没有创建好,不用利用这个函数添加内容,最好利用onEndPage()处理页面的初始化。
       onEndPage() — 在创建一个新页面完成但写入内容之前触发,是添加页眉、页脚、水印等最佳时机。
       onCloseDocument() — 在文档关闭之前触发,可以用于释放一些资源。

2.重载PdfPageEventHelper类
       定义一个类HeaderFooter,继承了父类PdfPageEventHelper
       在文档的每个页面中,必须定义一个Ractangle矩形,其中参数为art,这样在HeaderFooter类中就可以通过这个矩形,获取文档的边框位置,从而设置页眉和页脚。
代码如下:

  1. import com.itextpdf.text.Document;
  2. import com.itextpdf.text.Element;
  3. import com.itextpdf.text.Phrase;
  4. import com.itextpdf.text.Rectangle;
  5. import com.itextpdf.text.pdf.ColumnText;
  6. import com.itextpdf.text.pdf.PdfPageEventHelper;
  7. import com.itextpdf.text.pdf.PdfWriter;
  8. public class HeaderFooter extends PdfPageEventHelper{
  9. public void onEndPage (PdfWriter writer, Document document) {
  10. Rectangle rect = writer.getBoxSize("art");
  11. switch(writer.getPageNumber() % 2) {
  12. case 0:
  13. ColumnText.showTextAligned(writer.getDirectContent(),
  14. Element.ALIGN_RIGHT, new Phrase("even header"),
  15. rect.getRight(), rect.getTop(), 0);
  16. break;
  17. case 1:
  18. ColumnText.showTextAligned(writer.getDirectContent(),
  19. Element.ALIGN_LEFT, new Phrase("odd header"),
  20. rect.getLeft(), rect.getTop(), 0);
  21. break;
  22. }
  23. ColumnText.showTextAligned(writer.getDirectContent(),
  24. Element.ALIGN_CENTER, new Phrase(String.format("page %d", writer.getPageNumber())),
  25. (rect.getLeft() + rect.getRight()) / 2, rect.getBottom() - 18, 0);
  26. }
  27. }

调用代码如下

  1. import com.itext.HeaderFooter;
  2. import com.itextpdf.text.pdf.PdfPageEventHelper;
  3. import com.itextpdf.text.pdf.PdfWriter;
  4. import com.itextpdf.text.pdf.ColumnText;
  5. import com.itextpdf.text.*;
  6. import java.io.FileOutputStream;
  7. public class HeaderAndFooterDemo {
        public static void main(String[] args){
  8. Document document = new Document(PageSize.A4, 50, 50, 50, 50);
  9. try{
  10. PdfWriter writer=PdfWriter.getInstance(document,
  11. new FileOutputStream("C:\\testHeaderAndFooter.pdf") );
  12. Rectangle rect = new Rectangle(36, 54, 559, 788);
  13. rect.setBorderColor(BaseColor.BLACK);
  14. writer.setBoxSize("art", rect);
  15. HeaderFooter header=new HeaderFooter();
  16. writer.setPageEvent(header);
  17. document.open();
  18. document.newPage();
  19. Paragraph par = new Paragraph("first paragraph");
  20. document.add(par);
  21. document.newPage();
  22. Paragraph par2 = new Paragraph("second paragraph");
  23. document.add(par2);
  24. document.close();
  25. }catch(Exception e){
  26. e.printStackTrace();
  27. }
  28. }
  29. }

3. 解决第X页/共Y页问题
       我们通过PdfWriter中的getPageNumber()函数获取当前是第几页,但是没有办法获取文档共多少页。
        我们可以利用XObject对象,iText仅在调用释放模板方法后才将PdfTemplate写入到OutputStream中,否则对象将一直保存在内存中,直到关闭文档。
       我们可以给第1个页面添加template,直到最后一个页面才将内容写入到这个模板。

  1. import com.itextpdf.text.Document;
  2. import com.itextpdf.text.DocumentException;
  3. import com.itextpdf.text.Element;
  4. import com.itextpdf.text.ExceptionConverter;
  5. import com.itextpdf.text.Image;
  6. import com.itextpdf.text.Phrase;
  7. import com.itextpdf.text.Rectangle;
  8. import com.itextpdf.text.pdf.ColumnText;
  9. import com.itextpdf.text.pdf.PdfPCell;
  10. import com.itextpdf.text.pdf.PdfPTable;
  11. import com.itextpdf.text.pdf.PdfPageEventHelper;
  12. import com.itextpdf.text.pdf.PdfTemplate;
  13. import com.itextpdf.text.pdf.PdfWriter;
  14. public class TableHeader extends PdfPageEventHelper{
  15. String header;
  16. PdfTemplate total;
  17. public void setHeader(String header){
  18. this.header=header;
  19. }
  20. public void onOpenDocument(PdfWriter writer,Document document){
  21. total = writer.getDirectContent().createTemplate(30,16);
  22. }
  23. public void onEndPage (PdfWriter writer, Document document) {
  24. PdfPTable table = new PdfPTable(3);
  25. try{
  26. table.setWidths(new int[]{24,24,2});
  27. table.setTotalWidth(527);
  28. table.setLockedWidth(true);
  29. table.getDefaultCell().setFixedHeight(20);
  30. table.getDefaultCell().setBorder(Rectangle.BOTTOM);
  31. table.addCell(header);
  32. table.getDefaultCell().setHorizontalAlignment(
  33. Element.ALIGN_RIGHT);
  34. table.addCell(String.format("page %d of",writer.getPageNumber()));
  35. PdfPCell cell = new PdfPCell(Image.getInstance(total));
  36. cell.setBorder(Rectangle.BOTTOM);
  37. table.addCell(cell);
  38. table.writeSelectedRows(0,-1,34,803,writer.getDirectContent());
  39. }
  40. catch(DocumentException de){
  41. throw new ExceptionConverter(de);
  42. }
  43. }
  44. public void onCloseDocument(PdfWriter writer,Document document){
  45. ColumnText.showTextAligned(total,Element.ALIGN_LEFT,new Phrase(String.valueOf(writer.getPageNumber()-1)),2,2,0);
  46. }
  47. }

调用代码如上面HeaderAndFooterDemo.java所示。

参考文献
1.Adding page events to PdfWriter (iText 5).http://what-when-how.com/itext-5/adding-page-events-to-pdfwriter-itext-5/
2iText5参考. http://api.itextpdf.com/itext/

iText5报表_页眉与页脚的更多相关文章

  1. NCreport报表控件教程:设计页眉和页脚

    一.设计页眉 一般来说页眉部分一般是用于包含标题的内容, 首先我们会添加列标签到页眉部分,标签都是简单的文本,标签项一般是用于在报表上显示一些描述信息,标签都是静态项,所以它们的值不会有变化. 添加标 ...

  2. iText导出PDF(图片,水印,页眉,页脚)

    项目需要导出PDF,导出的内容包含图片和文本,而且图片的数量不确定,在网上百度发现大家都在用iText,在官网发现可以把html转换为PDF,但是需要收费,那就只能自己写了. 在开始之前先在网上百度了 ...

  3. openxml(二) 添加页眉,页脚

    openxml 中 word 文档的结构是如下图: 其中,页眉是 header,属于headerpart 部件,页脚是footer,属于footerpart 部件,图上还有其他的东西,之后会一一介绍. ...

  4. iOS开发——UI_swift篇&TableView实现页眉和页脚

    TableView实现页眉和页脚   在UItableView中header和footer是很常见的,而且他能让你实现很复杂的功能,我们见过最多的就是下拉刷新和上啦加载更多,当然你还可以在上面添加一个 ...

  5. Swift - 给表格TableView添加页眉和页脚

    UITableView具有var tableHeaderView:UIView?属性和var tableFooterView:UIView?属性,可以通过给其赋值来创建列表TableView的页眉和页 ...

  6. C# 操作Word 文档——添加Word页眉、页脚和页码

    在Word文档中,我们可以通过添加页眉.页脚的方式来丰富文档内容.添加页眉.页脚时,可以添加时间.日期.文档标题,文档引用信息.页码.内容解释.图片/LOGO等多种图文信息.同时也可根据需要调整文字或 ...

  7. C# 添加Word页眉、页脚和页码

    在Word文档中,我们可以通过添加页眉.页脚的方式来丰富文档内容.添加页眉.页脚时,可以添加时间.日期.文档标题,文档引用信息.页码.内容解释.图片/LOGO等多种图文信息.同时也可根据需要调整文字或 ...

  8. C# 给现有PDF文档添加页眉、页脚

    概述 页眉页脚是一篇完整.精致的文档的重要组成部分.在页眉页脚处,可以呈现的内容很多,如公司名称.页码.工作表名.日期.图片,如LOGO.标记等.在之前的文章中介绍了如何通过新建一页空白PDF页来添加 ...

  9. JQuery Mobile - 解决页面点击时候,页眉和页脚消失问题!

    当点击页面时候,页眉和页脚会消失!解决方法,在页面和页脚中加入: data-quicklinks="true" 实际使用代码: <div data-role="pa ...

随机推荐

  1. 使用Asp.Net Core MVC 开发项目实践[第二篇:EF Core]

    在项目中使用EF Core还是比较容易的,在这里我们使用的版本是EF Core 2.2. 1.使用nuget获取EF Core包 这个示例项目使用的是SQLSERVER,所以还需要下载Microsof ...

  2. [AGC 018 E] Sightseeing plan

    STO ZKY ORZ Description 给定一张网格图和三个矩形,每次只能向上或向右走.你需要从矩形 \(A\) 中的一个点 \(S\) 出发,到达矩形 \(B\) 中的一个点 \(P\) , ...

  3. [转]Ionic国际化解决方案

    本文转自:http://www.cnblogs.com/crazyprogrammer/p/7904436.html 1.     核心内容 使用Angular2的国际化(i18n)库:ngx-tra ...

  4. Linux中inotify软件部署及参数事件演示

    声明:博主使用的是CentOS6.9的系统 参考资料: https://github.com/rvoicilas/inotify-tools/wiki http://www.ibm.com/devel ...

  5. SQL查询语句如何能够让指定的记录排在最后

    方法如下:select * from <表名> order by case when <条件> then 1 else 0 end asc 举例:把threads表中列id值小 ...

  6. 【RabbitMQ】7、RabbitMQ主备复制是异步还是同步?

    转自:https://yq.aliyun.com/articles/73040?spm=5176.100240.searchblog.116.RcXYdl 我们知道RabbitMQ可以配置成Queue ...

  7. java_查找里程

    题目内容: 下图为国内主要城市之间的公路里程: 你的程序要读入这样的一张表,然后,根据输入的两个城市的名称,给出这两个城市之间的里程. 注意:任何两个城市之间的里程都已经给出,不需要计算经第三地中转. ...

  8. Tomcat意外宕机分析

    之前在网上看过一篇文章,是讲Tomcat进程意外退出的,我看完感觉好奇,自己也测试了下,果然是有这种问题,所以自己也借此总结一下. 先简单说下测试过程,先创建一个web服务启动 test.sh,内容如 ...

  9. 微信服务号获取openid方法

    public function tetst(){ if(!isset($_GET['code'])){ $APPID = $this->app_id; $ran = rand(1,100); / ...

  10. html一些标签在不同浏览器或者不同版本浏览器的注意事项

    最近在IE10下运行一个以前的做web系统发现了两个小问题:   一.图片上使用"alt"属性来添加一些文字提示信息在IE10下无法正常显示出来   上网查了一下原因:原来是现在一 ...