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

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

Feedback

# re: iTextSharp - Use header with events 3/30/2011 7:43 PM Ryan
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.

# re: iTextSharp - Use header with events 5/18/2012 1:18 PM .NET Dev
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.

Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: