user590586
user590586

Reputation: 3050

java create pdf with header and footer

I'm creating a pdf file (using iText), and i want to add to each page taht is generated an image at the top left of the page and the page number at center bottom of the page.

How can this be done? examples will be really appritiated.

Thank's In Advance.

Upvotes: 1

Views: 10458

Answers (2)

Sean
Sean

Reputation: 7737

Take A look at the example here : http://www.coderanch.com/how-to/java/ItextExample

There are 2 versions of the same example. The First is from iText2. That is the version I am familiar with, it was very simple to add a header/footer

HeaderFooter footer = new HeaderFooter( new Phrase("This is my footer") );
document.setFooter(footer);

In the more recent version iText5, this seems to have changed considerably. The link above shows

HeaderFooter event = new HeaderFooter();
writer.setPageEvent(event);

In this case, instead of HeaderFooter being part of the iText framework, it is a user created class which is extending PdfPageEventHelper. So you can change that to whatever you require

Upvotes: 2

stu
stu

Reputation: 11

You want to extend com.lowagie.text.pdf.PdfPageEventHelper and implement the following:

public void onStartPage(PdfWriter pdfwriter, Document document)

public void onEndPage(PdfWriter pdfwriter, Document document)

Then set the page event on your PdfWriter:

PdfWriter pdfWriter = PdfWriter.getInstance(document, outputStream);
pdfWriter.setPageEvent(new YourPageEventClassGoesHere());

Upvotes: 1

Related Questions