iText 5页眉和页脚 [英] iText 5 header and footer

查看:486
本文介绍了iText 5页眉和页脚的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在我的PDF页面中添加标题页脚
我想要一个在标题和其他表格中有3列的表格,页脚中有3列。
我的页面可以是A3或A4,也可以是横向或纵向。

how I can add in my PDF page the header and the footer? I wanna a table with 3 column in header and other table, 3 column in the footer. My page could be A3 or A4, and landscape or portrait.

任何人都可以帮助我吗?我在互联网上找不到好的例子。

Can anyone help me? I can not found on internet good examples.

谢谢!

Tommaso

推荐答案


  1. 创建一个扩展 PdfPageEventHelper

  2. 添加的类MyPageEventListener PdfWriter对象的页面事件侦听器

  3. 在MyPageEventListener类的 onEndPage 方法中,将代码
    替换为页眉/页脚

  1. Create a class MyPageEventListener that extends PdfPageEventHelper
  2. Add a page event listener to the PdfWriter object
  3. In the onEndPage method of MyPageEventListener class, put the code for header/footer

示例:

public class MyPageEventListener extends PdfPageEventHelper {
  . . .
  @Override
  public void onEndPage(PdfWriter writer, Document document) {
     //code skeleton to write page header
     PdfPTable tbl = new PdfPTable(3);
     tbl.addCell("1st cell");
     tbl.addCell("2nd cell");
     tbl.addCell("3rd cell");
     float x = document.leftMargin();
     float hei = getMyHeaderHeight(); //custom method that return header's height 
     //align bottom between page edge and page margin
     float y = document.top() + hei;

     //write the table
     tbl.writeSelectedRows(0, -1, x, y, writer.getDirectContent());
  }    
}

只需注册监听器

writer.setPageEvent(new MyPageEventListener());

这篇关于iText 5页眉和页脚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆