Mamtora
Mamtora

Reputation: 21

How to add header and footer in all page of pdf using iTextsharp version 5.2.0 in C#

I am creating a pdf file using itextsharp. I want to add header and footer for each page in the pdf document. Can anyone tell me how can I do this?

I am using itext 5.2.0 In this, I am unable to find the option to use HeadeFooter class, which is available in the earlier versions.

Thanks in advance..

Upvotes: 2

Views: 24534

Answers (2)

G.S. Shekhawat
G.S. Shekhawat

Reputation: 339

Please use this code.

public partial class Footer : PdfPageEventHelper
{
    public override void OnEndPage(PdfWriter writer, Document doc)
    {
       Paragraph footer= new Paragraph("THANK YOU", FontFactory.GetFont(FontFactory.TIMES, 10, iTextSharp.text.Font.NORMAL));
       footer.Alignment = Element.ALIGN_RIGHT;
       PdfPTable footerTbl = new PdfPTable(1);
       footerTbl.TotalWidth = 300;
       footerTbl.HorizontalAlignment = Element.ALIGN_CENTER;

       PdfPCell cell = new PdfPCell(footer);
       cell.Border = 0;
       cell.PaddingLeft = 10;

       footerTbl.AddCell(cell);
       footerTbl.WriteSelectedRows(0, -1, 415, 30, writer.DirectContent);
    }
}

please check my blog for more details http://gopalkaroli.blogspot.in/2011/11/how-to-add-header-and-footer-on-pdf.html

https://gopalkaroli.wordpress.com/2011/11/12/how-to-add-header-and-footer-on-pdf-file-using-itextsharp-5-1/

Upvotes: 7

ABH
ABH

Reputation: 3439

For iTextSharp version 5+, Header/Footer property has been removed. Now this can be done by us PageEventHandler class. Though it's not strait forward now but the upside is that now you can add more than just plan text in header and footer. Please check this link for complete workout of header/footer and more in iTextSharp.

Upvotes: -1

Related Questions