How to add header and footer on pdf file using iTextSharp 5.1

first we create a class that in inherited by  PdfPageEventHelper. and  i create table in this class and write footer content.

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);

}

}

after this

Document document = new Document(PageSize.A4, 50, 50, 25, 25);

var output = new FileStream(Server.MapPath(“Demo.pdf”), FileMode.Create);

PdfWriter writer = PdfWriter.GetInstance(document, output);

// Open the Document for writing

document.Open();

//using footer class

writer.PageEvent = new Footer();.

Paragraph welcomeParagraph = new Paragraph(“Hello, World!”);

document.Add(welcomeParagraph);

document.Close();

About these ads

7 thoughts on “How to add header and footer on pdf file using iTextSharp 5.1

  1. I am not getting header on first page of the pdf. Please help me. i am using the code below:

    using System;

    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.IO;
    using iTextSharp.text;
    using iTextSharp.text.pdf;
    using iTextSharp.text.html;
    using iTextSharp.text.html.simpleparser;

    ///
    /// Summary description for HeaderF
    ///
    public class HeaderF : PdfPageEventHelper
    {
    public override void OnStartPage(PdfWriter writer, Document document)
    {
    base.OnEndPage(writer, document);
    Rectangle pageSize = document.PageSize;

    string date = DateTime.Now.ToString(“MMMMM dd, yyyy”);
    Paragraph header = new Paragraph(date, FontFactory.GetFont(FontFactory.TIMES, 10, iTextSharp.text.Font.BOLD, BaseColor.WHITE));
    header.Alignment = Element.ALIGN_LEFT;
    PdfPTable headerTbl = new PdfPTable(1);
    headerTbl.TotalWidth = 600;
    headerTbl.HorizontalAlignment = Element.ALIGN_LEFT;
    PdfPCell cell = new PdfPCell(header);
    cell.Border = 0;
    cell.PaddingLeft = 10;
    cell.BackgroundColor = BaseColor.GRAY;
    headerTbl.AddCell(cell);
    headerTbl.WriteSelectedRows(0, -1, pageSize.GetLeft(5), pageSize.GetTop(5), writer.DirectContent);
    }
    }

    • Hi Tariq Mohammad,

      please check this code. your code are right. but please override OnEndPage Method

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Web;
      using System.Web.UI;
      using System.Web.UI.WebControls;
      using iTextSharp.text.pdf;
      using iTextSharp.text;
      using System.IO;

      public partial class _Default : System.Web.UI.Page
      {
      protected void Page_Load(object sender, EventArgs e)
      {

      }
      protected void Button1_Click(object sender, EventArgs e)
      {

      Document document = new Document(PageSize.A4, 50, 50, 25, 25);

      // Create a new PdfWriter object, specifying the output stream
      var output = new FileStream(Server.MapPath(“MyFirstPDF.pdf”), FileMode.Create);
      PdfWriter writer=PdfWriter.GetInstance(document, output);

      // Open the Document for writing
      document.Open();

      writer.PageEvent = new HeaderF();
      Paragraph welcomeParagraph = new Paragraph(“Hello, World!”);

      // Add the Paragraph object to the document
      document.Add(welcomeParagraph);

      // Close the Document – this saves the document contents to the output stream
      document.Close();
      }
      }

      ///
      /// Summary description for HeaderF
      ///
      public class HeaderF : PdfPageEventHelper
      {
      public override void OnEndPage(PdfWriter writer, Document document)
      {
      Rectangle pageSize = document.PageSize;
      string date = DateTime.Now.ToString(“MMMMM dd, yyyy”);
      Paragraph header = new Paragraph(date, FontFactory.GetFont(FontFactory.TIMES, 10, iTextSharp.text.Font.BOLD, BaseColor.WHITE));
      header.Alignment = Element.ALIGN_LEFT;
      PdfPTable headerTbl = new PdfPTable(1);
      headerTbl.TotalWidth = 600;
      headerTbl.HorizontalAlignment = Element.ALIGN_LEFT;
      PdfPCell cell = new PdfPCell(header);
      cell.Border = 0;
      cell.PaddingLeft = 10;
      cell.BackgroundColor = BaseColor.GRAY;
      headerTbl.AddCell(cell);
      headerTbl.WriteSelectedRows(0, -1, pageSize.GetLeft(5), pageSize.GetTop(5), writer.DirectContent);

      }
      }

  2. this code is not working for multiple pages,it’t only creating footer for last page.How can I create footer and header for multiple pages

    • I have tested this code for multiple pages . it’s working fine for me. please send me your code . I will check and let you know what is issue in your code.

      Thanks
      Gopal Singh

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s