1

I'm generating a PDF with iText, in that I'm displaying a header and footer. Now i want to remove header for a particular page.

For eg: If I'm generating a 50 pages pdf, for the final 50th I don't want to show header, how could this be achieved?

Here's my code where I'm generating footer (header part removed).

public class HeaderAndFooter extends PdfPageEventHelper {

public void onEndPage (PdfWriter writer, Document document) {
    Rectangle rect = writer.getBoxSize("art");
    switch(writer.getPageNumber() % 2) {
    case 0:

    case 1:
        ColumnText.showTextAligned(writer.getDirectContent(),
                Element.ALIGN_CENTER, new Phrase(String.format("%d", writer.getPageNumber())),
                300f, 62f, 0);
        break;
    }

}

}

Any suggestions? Thanks in advance.

Alexis Pigeon
  • 7,423
  • 11
  • 39
  • 44
Aravinth
  • 363
  • 2
  • 10
  • 33

2 Answers2

1

You can use a 2-pass approach:

  • 1st pass : generate the PDF file without header

  • 2nd pass : stamp the header on all but the last page

Have a look at this example taken from the iText book. You'll just have to adapt the second pass by only going through the N-1 first pages:

int n = reader.getNumberOfPages() - 1;

instead of

int n = reader.getNumberOfPages();
Alexis Pigeon
  • 7,423
  • 11
  • 39
  • 44
0

I was also in need to do the same. I want to share how I resolved this issue.

The Idea is, for the automatic generation of header footer, we set page event on PDFWriter like:

HeaderAndFooter event= new HeaderAndFooter(); //HeaderAndFooter is the implementation of PdfPageEventHelper class
writer.setPageEvent(event);// writer is the instance of PDFWriter

So, before the content of the last page, We can remove the event:

 event=null;
 writer.setPageEvent(event);

It works for me without any error or exception.