Geeks With Blogs
Guilherme Cardoso, Blog EN .NET geek

A few time ago i've write an article explaining how to use iTextSharp. Check it here:

http://geekswithblogs.net/guilhermecardoso/archive/2010/09/22/itextsharp---create-a-.pdf-document-with-a-gridview-table.aspx

In this article, i'll show how to use headers.

You just should follow this article if you're using iTextSharp 5.x or above. Other versions uses the object HeaderFooter.
The last releases, instead of using an object to create the header, uses events.

There's a list of existing events in iTextSharp: OnChapter, OnChapterEnd, OnCloseDocument, OnEndPage, OnGenericTag, OnOpenDocument, OnParagraph, OnParapraphEnd, OnSection, OnSectionEnd e OnStartPage.
If you want, read more about PdfPageEventHelper  class here: http://api.itextpdf.com/com/itextpdf/text/pdf/PdfPageEventHelper.html

To create the headers in all pages, we'll use the event OnStartPage

public class PdfHandlerEvents : PdfPageEventHelper
    {
        public override void OnStartPage(iTextSharp.text.pdf.PdfWriter writer, iTextSharp.text.Document document)
        { 
                Image logo = Image.GetInstance(HttpContext.Current.Server.MapPath("~/Images/logo.gif"));
                document.Add(logo);
                document.Add(new Phrase(Environment.NewLine));
                document.Add(new Phrase(Environment.NewLine)); 
        }
} 

I'm adding two new lines for the content have a little space between header.
We're creating the class PdfHandlerEvents, but it's important to use PdfPageEventHelper as parent..

Posted on Tuesday, October 19, 2010 4:56 AM | Back to top


Comments on this post: iTextSharp - Use header with events

# re: iTextSharp - Use header with events
Requesting Gravatar...
I used to use iTextSharp but the API is so congested that I switched to HtmlToPdf Converter about 4 years ago. It's quite pricey ($1200) but worth every penny if you don't want to deal with chunks, PTables, pixels, etc. I figure that it paid for itself on the first project we did and we've used it on dozens more.

http://www.html-to-pdf.net/Features.aspx

I suppose there's probably other solutions out there now that I haven't researched, but I just wanted you to know there are other options.
Left by Ryan on Mar 30, 2011 7:43 PM

# re: iTextSharp - Use header with events
Requesting Gravatar...
It's a nice article, I hope it will help me in my job. I would also like to sugest you to make your source available for download, so we can understand better your aproach. Thank you very much.
Left by .NET Dev on May 18, 2012 1:18 PM

# re: iTextSharp - Use header with events
Requesting Gravatar...
Except the documentation itself says you shouldn't use onPageStart for injecting headers. You should use onPageEnd, otherwise you risk creating empty pages due to the ordering of events.
Left by Harold on Nov 14, 2012 1:28 PM

# re: iTextSharp - Use header with events
Requesting Gravatar...
Hi,
Link
http://api.itextpdf.com/com/itextpdf/text/pdf/PdfPageEventHelper.html
don´t work anymore
Left by Jose on Feb 05, 2017 11:49 PM

Your comment:
 (will show your gravatar)


Copyright © Guilherme Cardoso | Powered by: GeeksWithBlogs.net