1

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.

1
  • what framework are you using to generate the PDF? iText, PDFBox, other?
    – Sean
    Apr 5, 2011 at 12:06

2 Answers 2

2

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

3
1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.