Saturday, November 30, 2013

iTextSharp–Add header/footer to PDF

It’s a common requirement to have header/footer on PDF and it could be achieved using PageEvents in iTextSharp. It depends on the requirement, but in general header may contain:
  • Header Title
  • Header Subtitle
  • Logo
  • Page Number/Datetime
And footer may contain:
  • Page Number
  • Copyright information
Check out below function that creates sample PDF having five pages with header and footer:
private void CreatePDF()
    string fileName = string.Empty;

    DateTime fileCreationDatetime = DateTime.Now;

    fileName = string.Format("{0}.pdf", fileCreationDatetime.ToString(@"yyyyMMdd") + "_" + fileCreationDatetime.ToString(@"HHmmss"));

    string pdfPath = Server.MapPath(@"~\PDF\") +   fileName;

    using (FileStream msReport = new FileStream(pdfPath, FileMode.Create))
        //step 1
        using (Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 140f, 10f))
                // step 2
                PdfWriter pdfWriter = PdfWriter.GetInstance(pdfDoc, msReport);
                pdfWriter.PageEvent = new Common.ITextEvents();

                //open the stream 

                for (int i = 0; i < 5; i++)
                    Paragraph para = new Paragraph("Hello world. Checking Header Footer", new Font(Font.FontFamily.HELVETICA, 22));

                    para.Alignment = Element.ALIGN_CENTER;




            catch (Exception ex)
                //handle exception




Below line assigns object of ItextEvent class that extends PdfPageEventHelper, which contains various methods as shown below that you can override to do various thing like add header/footer, watermark etc. In next post we’ll add watermark using similar class that extends PdfPageEventHelper.


Check out the ITextEvents class that extends PdfPageEventHelper to add header/footer.

public class ITextEvents : PdfPageEventHelper

        // This is the contentbyte object of the writer
        PdfContentByte cb;

        // we will put the final number of pages in a template
        PdfTemplate headerTemplate, footerTemplate;

        // this is the BaseFont we are going to use for the header / footer
        BaseFont bf = null;

        // This keeps track of the creation time
        DateTime PrintTime = DateTime.Now;

        #region Fields
        private string _header;

        #region Properties
        public string Header
            get { return _header; }
            set { _header = value; }

        public override void OnOpenDocument(PdfWriter writer, Document document)
                PrintTime = DateTime.Now;
                bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
                cb = writer.DirectContent;
                headerTemplate = cb.CreateTemplate(100, 100);
                footerTemplate = cb.CreateTemplate(50, 50);
            catch (DocumentException de)
                //handle exception here
            catch (System.IO.IOException ioe)
                //handle exception here

        public override void OnEndPage(iTextSharp.text.pdf.PdfWriter writer, iTextSharp.text.Document document)
            base.OnEndPage(writer, document);

            iTextSharp.text.Font baseFontNormal = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 12f, iTextSharp.text.Font.NORMAL, iTextSharp.text.BaseColor.BLACK);

            iTextSharp.text.Font baseFontBig = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 12f, iTextSharp.text.Font.BOLD, iTextSharp.text.BaseColor.BLACK);

            Phrase p1Header = new Phrase("Sample Header Here", baseFontNormal);

            //Create PdfTable object
            PdfPTable pdfTab = new PdfPTable(3);

            //We will have to create separate cells to include image logo and 2 separate strings
            //Row 1
            PdfPCell pdfCell1 = new PdfPCell();
            PdfPCell pdfCell2 = new PdfPCell(p1Header);
            PdfPCell pdfCell3 = new PdfPCell();
            String text = "Page " + writer.PageNumber + " of ";

            //Add paging to header
                cb.SetFontAndSize(bf, 12);
                cb.SetTextMatrix(document.PageSize.GetRight(200), document.PageSize.GetTop(45));
                float len = bf.GetWidthPoint(text, 12);
                //Adds "12" in Page 1 of 12
                cb.AddTemplate(headerTemplate, document.PageSize.GetRight(200) + len, document.PageSize.GetTop(45));
            //Add paging to footer
                cb.SetFontAndSize(bf, 12);
                cb.SetTextMatrix(document.PageSize.GetRight(180), document.PageSize.GetBottom(30));
                float len = bf.GetWidthPoint(text, 12);
                cb.AddTemplate(footerTemplate, document.PageSize.GetRight(180) + len, document.PageSize.GetBottom(30));
            //Row 2
            PdfPCell pdfCell4 = new PdfPCell(new Phrase("Sub Header Description", baseFontNormal));
            //Row 3

            PdfPCell pdfCell5 = new PdfPCell(new Phrase("Date:" + PrintTime.ToShortDateString(), baseFontBig));
            PdfPCell pdfCell6 = new PdfPCell();
            PdfPCell pdfCell7 = new PdfPCell(new Phrase("TIME:" + string.Format("{0:t}", DateTime.Now), baseFontBig));

            //set the alignment of all three cells and set border to 0
            pdfCell1.HorizontalAlignment = Element.ALIGN_CENTER;
            pdfCell2.HorizontalAlignment = Element.ALIGN_CENTER;
            pdfCell3.HorizontalAlignment = Element.ALIGN_CENTER;
            pdfCell4.HorizontalAlignment = Element.ALIGN_CENTER;
            pdfCell5.HorizontalAlignment = Element.ALIGN_CENTER;
            pdfCell6.HorizontalAlignment = Element.ALIGN_CENTER;
            pdfCell7.HorizontalAlignment = Element.ALIGN_CENTER;

            pdfCell2.VerticalAlignment = Element.ALIGN_BOTTOM;
            pdfCell3.VerticalAlignment = Element.ALIGN_MIDDLE;
            pdfCell4.VerticalAlignment = Element.ALIGN_TOP;
            pdfCell5.VerticalAlignment = Element.ALIGN_MIDDLE;
            pdfCell6.VerticalAlignment = Element.ALIGN_MIDDLE;
            pdfCell7.VerticalAlignment = Element.ALIGN_MIDDLE;

            pdfCell4.Colspan = 3;

            pdfCell1.Border = 0;
            pdfCell2.Border = 0;
            pdfCell3.Border = 0;
            pdfCell4.Border = 0;
            pdfCell5.Border = 0;
            pdfCell6.Border = 0;
            pdfCell7.Border = 0;

            //add all three cells into PdfTable

            pdfTab.TotalWidth = document.PageSize.Width - 80f;
            pdfTab.WidthPercentage = 70;

            //call WriteSelectedRows of PdfTable. This writes rows from PdfWriter in PdfTable
            //first param is start row. -1 indicates there is no end row and all the rows to be included to write
            //Third and fourth param is x and y position to start writing
            pdfTab.WriteSelectedRows(0, -1, 40, document.PageSize.Height - 30, writer.DirectContent);
            //Move the pointer and draw line to separate header section from rest of page
            cb.MoveTo(40, document.PageSize.Height - 100);
            cb.LineTo(document.PageSize.Width - 40, document.PageSize.Height - 100);

            //Move the pointer and draw line to separate footer section from rest of page
            cb.MoveTo(40, document.PageSize.GetBottom(50));
            cb.LineTo(document.PageSize.Width - 40, document.PageSize.GetBottom(50));

        public override void OnCloseDocument(PdfWriter writer, Document document)
            base.OnCloseDocument(writer, document);

            headerTemplate.SetFontAndSize(bf, 12);
            headerTemplate.SetTextMatrix(0, 0);
            headerTemplate.ShowText((writer.PageNumber - 1).ToString());

            footerTemplate.SetFontAndSize(bf, 12);
            footerTemplate.SetTextMatrix(0, 0);
            footerTemplate.ShowText((writer.PageNumber - 1).ToString());


As you can see that above class has Header property that you might want to use if you want your header title to be assigned dynamic. Add as many properties as you want, to display dynamic values in your header/footer.

It will generate sample PDF that will look like below:


I hope it helps!


Danish Merchant said...

how to get total number of pages dynamically on OnEndPage

Danish Merchant said...

how to get Total number of pages in pdf on OnEndPage dynamically

Nilesh Thakker said...

What do you want to do by having "Total no of pages" in OnEndPage?

It seems there's no way you can have count of total no of pages in OnEndPage event except knowing beforehand how many pages will be there. You can have it OnEndPage event by declaring global variable in PageEvent Class and assign it value from your code.

I hope it makes sense!

Manoj Verma said...

Thankx bro.Its really help me.

please continue blogging.

Joël-Marie KREILOS said...

Thanks for your great post.
I'm new to iText and looked over the net quite a while before finding your post.

Just one question :
I've implemented your solution which woks fine, but added instead of a dummy Paragraph a big PDFPTable of Data, which is splitted on 5 pages

How can I set the position and size of the table to fit between the 2 horizontal separation lines of the header and the footer ?

Actually it looks like :

tableDatatableData HEADER tableData
tableDatatableData FOOTER

Instead of



Nilesh Thakker said...


If you follow the article completely, you'll have the output as you want. Then just replace it with your data.

IWantToLearn said...

do you or anyone have a version of this? please I need it badly

Sesha Giri said...

the type or name space for common is missing

i have added like new system.config

new Common.ITextEvents();

Aman Thakur said...

@sesha giri: Common is another folder which author has created in his solution and added the Interface ITextEvents class inheriting the PdfPageEventHelper. You can either create a Folder in your solution and place the class there in that folder. OR you can create the class in your namespace only.....It's your choice....

@IWantToLearn: If you really want to learn, then at-least convert this code to VB.NET yourself. It's not a big deal. :P :)

Gourav Chowdhury said...

how to add different footer in last page of pdf...
All pages should have same footer only last page should have some other footer...
Or all pages should have same footer but last page should have no footer... how to achieve any one of these two..

Unknown said...

code converter
c# to VB

Deathly said...

THANKS!!!!! :D!

Kiran said...

How do I pass a value to the Header Property and use it as a dynamic header data

Nilesh Thakker said...


I hope you've gone through current article. If not, please go through it and refer below link:

I hope it helps!

Mani kandan said...

Server.MapPath(@"~\PDF\") + fileName;

In this line "Server" not exists in current context what can i do??

buyi wen said...
This comment has been removed by a blog administrator.
AdeleB said...

PDF to image SDK for .NET project - how to change pdf to jpeg image in

sabata.mereeotlhe said...

Brilliant stuff

Nilesh Thakker said...

Thanks @Sabata

Grant Mchingula said...
This comment has been removed by the author.
Grant Mchingula said...

Great work , Nilesh. THis has been helpful. How do you restrict the header to first page oly?

vijay said...

How to add the three column table in the bottom of the table with the page no in the middle of the cell and other custom footer text in other cells.

Zuhair Ali said...

I'm using ASP.NET Repeater and want to show the sub header description dynamically and the data of the repeater dynamically. How to do this?

Maruti Pathare said...

How to call methods of ITextEvents class?

Thakur said...

Than you so much it help me a lot

Tuna Flakes said...

When i run this code, there is an error.
Object reference not set to an instance.
The error points on cb and template.

Unknown said...

@Joël-Marie KREILOS
i have same problem
if you used same code but add a table of lot rows you will get that issue !
please help if you have a recommendation to solve this issue.

Ameer Mansour said...

@Joël-Marie KREILOS
just increase your bottom margin and top margin and it will be fixed
@ Nilesh Thakker
thanks but i have document of increased your loop to 50 it will count wrongly
as if it count from zero

Ghazanfar Khan said...

I want to add background color to middle content as shown in your example how will I do that for every page

Girish Iche said...
This comment has been removed by the author.
Derek Walters said...

ZetPDF Library for .NET SDK offers you a modern, simple, fast, flexible and powerful tool to create complex and stylish PDF documents in your applications with just a few lines of code. Using the high quality PDF conversion engine you can easily design a document & modify the document.