Guilherme Cardoso, Blog EN

.NET geek

  Home  |   Contact  |   Syndication    |   Login
  21 Posts | 0 Stories | 21 Comments | 0 Trackbacks

News

Name: Guilherme Cardoso
Age: 19
Country: Portugal


pontonetpt

NetPonto

xaml pt

Twitter





Archives

Tuesday, October 19, 2010 #

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..