Image in the Page Header Generated by iTextSharp?

It seems to be a common question on the iText mailing list, probably because it's not a simple, one-step process - you can't pass an Image object to the Header constructor. After some time searching though the mailing list, found a recommended method that involves a few steps:

  1. Create a class that inherits from PdfPageEventHelper, which you should be able to guess, allows you to catch numerous document events. Since the goal is to add a header to every page of the PDF document, we override the OnEndPage() method.
  2. Use the PdfPTable within the (OnEndPage()) method so everything is nicely formatted.
  3. Attach an instance object of our newly created class to PdfWriter's PageEventproperty.
  4. Add content.

On to the code; first Steps 1 & 2 from above, this class is nested within your code-behind class:

// Step 1 - create the event listener
private class _events : PdfPageEventHelper {
  public override void OnEndPage(PdfWriter writer, Document document) {
    Rectangle page = document.PageSize;

    // Step 2 - create two column table;
    PdfPTable head = new PdfPTable(2);
    head.TotalWidth = page.Width / 2;

    // add header image; PdfPCell() overload sizes image
    // to fit cell
    PdfPCell c = new PdfPCell(
      // pass the path to your image
      Image.GetInstance(PATH_TO_IMAGE), true
    );
    c.HorizontalAlignment = Element.ALIGN_RIGHT;
    c.Border = Rectangle.NO_BORDER;
    head.AddCell(c);

    // add header text
    c = new PdfPCell( new Phrase(
      DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss") + " GMT",
      new Font(Font.COURIER, 8)
    ));
    c.Border = Rectangle.NO_BORDER;
    c.VerticalAlignment = Element.ALIGN_BOTTOM;
    head.AddCell(c);

    // write (write) table to PDF document; 
    // WriteSelectedRows() requires you to specify absolute position!
    head.WriteSelectedRows(
      0, -1,          // first/last row; -1 flags all write all rows
      page.Width / 4, // left offset
      // ** bottom** yPos of the table
      page.Height - document.TopMargin + head.TotalHeight,
      writer.DirectContent
    );
  }
}

Then in the button's event handler, where you're creating/writing the PDF document, Steps 3 & 4

protected void BUTTON_EVENT_HANDER(object sender, EventArgs e) {
  // instantiate Document
  // get PdfWriter instance/Stream object

  // Step 3 - create an instance of our class inheriting
  // from PdfPageEventHelper and attach the listener to the
  // PdfWriter. the OnEndPage() override code we wrote
  // above will write the image header on each page.
  _events e = new _events();
  // 'pw' is a reference to the PdfWriter.
  pw.PageEvent = e;

  // Step 4
  // open document
  // add content
  // close document
  // write Stream to Response object
}

Notes

  • Again, get ready to dig into the source code and spend some time searching the iText mailing list. This is a really cool library, but expect getting your hands dirty spending some time to get semi-proficient.

More kuujinbo.info iText examples

iText References