Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
902 views
in Technique[技术] by (71.8m points)

java - iText 5 header and footer

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.

Thanks!

Tommaso

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  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

Example:

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());
  }    
}

to register the listener simply do

writer.setPageEvent(new MyPageEventListener());

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...