first we create a class that in inherited by PdfPageEventHelper and i create a table in this class for 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();
Amit said:
nice code for convert pdf
Ajay kumar said:
Thanks sir for display code for pdf generator
Ajay kumar said:
Thanks sir……….. for display code for pdf generator
Tariq Mohammad said:
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);
}
}
gopalkaroli said:
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);
}
}
Shazana Salehudin said:
Thank you very much..it works well for my code…Thank ypu very much..may God bless u with ur kindness
arpita said:
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
gopalkaroli said:
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
Sathish kumar said:
i want to display header and footer in all pages, but image is displaying in header position and the header is coming in the position of Pdf contents.
The requirement is header table then pdf contents(Paragraph, image,paragraph,image) then footer,
The same thing need to be repeated for all pages if the pdf content needs more than one page.
i used the following code
public override void OnStartPage(PdfWriter writer, Document document)
{
string image2 = HttpContext.Current.Server.MapPath(“.”) + “/Images/developers_img.jpg”;
string image3 = HttpContext.Current.Server.MapPath(“.”) + “/Images/contact_logo.png”;
string imageFilePath = HttpContext.Current.Server.MapPath(“.”) + “/Images/contact_logo.png”;
float cellHeight = document.TopMargin;
iTextSharp.text.Image jpg2 = iTextSharp.text.Image.GetInstance(image2);
iTextSharp.text.Image jpg3 = iTextSharp.text.Image.GetInstance(image3);
iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(imageFilePath);
jpg.ScaleToFit(280f, 400f);
jpg.Alignment = Element.ALIGN_TOP;
PdfPTable Headertbl = new PdfPTable(3);
Headertbl.TotalWidth = document.PageSize.Width;
Headertbl.SpacingBefore = 2f;
Headertbl.SpacingBefore = 2f;
Headertbl.WidthPercentage = 90;
PdfPCell cell11 = new PdfPCell();
cell11.AddElement(jpg);
cell11.Border = 0;
cell11.VerticalAlignment = Element.ALIGN_LEFT;
PdfPCell cell12 = new PdfPCell();
cell12.Border = 0;
PdfPCell cell13 = new PdfPCell();
cell13.AddElement(new Paragraph(“”));
cell13.AddElement(new Paragraph(cellHeight + “Cell Height”));
cell13.AddElement(new Paragraph(” Last Updated”));
cell13.AddElement(new Paragraph(” ” + DateTime.Now.ToString()));
cell13.Border = 0;
Headertbl.AddCell(cell11);
Headertbl.AddCell(cell12);
Headertbl.AddCell(cell13);
document.Add(Headertbl);
base.OnStartPage(writer, document);
}
public override void OnEndPage(PdfWriter writer, Document doc)
{
PdfPTable footerTbl = new PdfPTable(3);
footerTbl.SpacingBefore = 2f;
footerTbl.TotalWidth = doc.PageSize.Width;
footerTbl.WidthPercentage = 90;
float cellHeight = doc.BottomMargin;
PdfPCell cell31 = new PdfPCell();
cell31.FixedHeight = cellHeight;
cell31.AddElement(new Paragraph(” “));
cell31.AddElement(new Paragraph(” “));
cell31.AddElement(new Paragraph(” “));
cell31.AddElement(new Paragraph(cellHeight + “Bottom margin “));
cell31.AddElement(new Paragraph(“CONFIDENTIAL”));
cell31.AddElement(new Paragraph(” “));
cell31.Border = 0;
cell31.PaddingLeft = 10;
cell31.MinimumHeight = 100f;
footerTbl.AddCell(cell31);
PdfPCell cell32 = new PdfPCell();
cell32.Border = 0;
footerTbl.AddCell(cell32);
PdfPCell cell33 = new PdfPCell();
cell33.Border = 0;
cell33.PaddingBottom = 50;
cell33.AddElement(new Paragraph(“Computer Solutions Inc.”));
cell33.AddElement(new Paragraph(“481 Main Street , Mangalore,10801”));
cell33.AddElement(new Paragraph(“Phone: 914 – 355 – 5800”));
cell33.AddElement(new Paragraph(“www.computersolutions.com”));
footerTbl.AddCell(cell33);
footerTbl.WriteSelectedRows(0, -1,0,footerTbl.TotalHeight, writer.DirectContent);
}
// Code to generate pdf on Button Click
namespace WebApplication2
{
public partial class CSE_Web : System.Web.UI.Page
{
protected Font ParaFont
{
get
{
// create a basecolor to use for the footer font, if needed.
BaseColor grey = new BaseColor(128, 128, 128);
Font font = FontFactory.GetFont(“Arial”, 9, Font.NORMAL,grey);
return font;
}
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
var doc1 = new Document(PageSize.A4, 50, 50, 25, 25);
string path = Server.MapPath(“PDF”);
string path1 = @”D:\ASP.NET\Cse website pdf\copy of 18\18\WebApplication2\WebApplication2\PDF\Doc1.pdf”;
string image2 = Server.MapPath(“.”) + “/Images/developers_img.jpg”;
string image3 = Server.MapPath(“.”) + “/Images/contact_logo.png”;
string imageFilePath = Server.MapPath(“.”) + “/Images/contact_logo.png”;
iTextSharp.text.Image jpg2 = iTextSharp.text.Image.GetInstance(image2);
iTextSharp.text.Image jpg3 = iTextSharp.text.Image.GetInstance(image3);
PdfPage page = new PdfPage();
PdfWriter writer = PdfWriter.GetInstance(doc1, new FileStream(path + “/Doc1.pdf”, FileMode.Create));
iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(imageFilePath);
jpg.ScaleToFit(280f,400f);
jpg2.ScaleToFit(400f,400f);
jpg2.SpacingBefore = 4f;
jpg2.SpacingAfter = 4f;
jpg.Alignment = Element.ALIGN_TOP;
var jpgheight = jpg.Height;
writer.PageEvent = page;
doc1.Open();
Paragraph p1 = new Paragraph( “\n Computer Solution the Tri-State area , Connecticut, and New Jersey, as the leading information technology (IT) company that offers innovative computer services and products to enable small and medium size enterprises to succeed in today’s digital-age. Our clients rely on our computer premium services, knowledgeable/certified consultants to lead them across the Information Technology highway so they can compete head to head against their competitors and larger organizations. Give us a call to learn more about how we can service your business or home.”, ParaFont);
p1.Alignment = Element.ALIGN_JUSTIFIED_ALL;
doc1.Add(p1);
doc1.Add(jpg2);
if (writer.PageNumber > 1)
{
doc1.NewPage();
}
doc1.Add(p1);
doc1.Add(jpg2);
doc1.Add(p1);
// p2.Add(Environment.NewLine);
//doc1.Add(p2);
doc1.Add(jpg2);
//doc1.NewPage();
//doc1.Add(p1);—
//p2.Add(Environment.NewLine);
//doc1.Add(p2);
//doc1.Add(jpg2);—
//doc1.NewPage();
// doc1.Add(p1);—-
doc1.Add(new Paragraph(“\nS Length”));
doc1.Add(new Paragraph(“\n”));
System.Diagnostics.Process.Start(“IExplore.exe”, path1);
doc1.Close();
}
}
}
vasantha kumar said:
nice code for convert pdf
Michael Kors Handbags said:
Everything is very open with a very clear description of the challenges.
It was truly informative. Your site is very
helpful. Thanks for sharing!
Fusuy Nidulawa said:
how to write this code using Asp vb.net ? please help
Savan Dounde said:
Thank!!! It Helps
gopalkaroli said:
Thanks for your comment . if you need any help then please contact me .
gopalsingh51@gmail.com
devenir riche said:
Normally I do not read article on blogs,
however I would like to say that this write-up very pressured me
to try and do so! Your writing taste has been surprised me.
Thanks, very great post.
Sugandhi ch said:
I have created 2 pages in PDF document. Header and footer showing only in the last page. i want display Header and footer in 2 pages. Please find my below code.
In Page_Load
iTextSharp.text.Document doc = new iTextSharp.text.Document(PageSize.A4);
string path = Server.MapPath(“PDF”);
PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(path + “\\doc20131.pdf”, FileMode.Create));
doc.Open();
doc.Add(new Paragraph(“Hello World!”));
doc.NewPage();
doc.Add(new Paragraph(“Hello World on a new page!”));
//using footer class
writer.PageEvent = new Footer();
writer.PageEvent = new HeaderF();
doc.Close();
string path1 = Server.MapPath(“PDF”) + “\\doc20131.pdf”;
WebClient client = new WebClient();
Byte[] buffer = client.DownloadData(path1);
if (buffer != null)
{
Response.ContentType = “application/pdf”;
Response.AddHeader(“content-length”, buffer.Length.ToString());
Response.BinaryWrite(buffer);
}
Created HeaderF class. Please find below code 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, Color.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 = Color.GRAY;
headerTbl.AddCell(cell);
headerTbl.WriteSelectedRows(0, -1, pageSize.GetLeft(5), pageSize.GetTop(5), writer.DirectContent);
}
}
Created Footerclass. Please find below code for Footer:
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);
}
}
gopalkaroli said:
Hi,
I checked your code and make changes . please check.
and let me know
iTextSharp.text.Document doc = new iTextSharp.text.Document(PageSize.A4);
string path = Server.MapPath(“PDF”);
PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(path + “\\doc20131.pdf”, FileMode.Create));
doc.Open();
writer.PageEvent = new Footer();
doc.Add(new Paragraph(“Hello World!”));
doc.NewPage();
doc.Add(new Paragraph(“Hello World on a new page!”));
//using footer class
doc.Close();
string path1 = Server.MapPath(“PDF”) + “\\doc20131.pdf”;
WebClient client = new WebClient();
Byte[] buffer = client.DownloadData(path1);
if (buffer != null)
{
Response.ContentType = “application/pdf”;
Response.AddHeader(“content-length”, buffer.Length.ToString());
Response.BinaryWrite(buffer);
}
Why you create two class for header and footer . please just create only single class and override onendpage and onstartpage event
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 iTextSharp.text.pdf.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);
}
public override void OnStartPage(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);
}
}
joffin said:
hi, i m joffin , how can i remove header and footer from selected pdf page. Example from last section (summary) of pdf.
gopalkaroli said:
yes ,you can remove header and footer from selected page . please add specific condition in event as per you need
Owais said:
I have also found a code in java to add header and footer in pdf file, check this out:
http://www.aspose.com/docs/display/pdfjava/Set%20Page%20Header%20and%20Footer
Jeremy said:
Thanks for this, it couldn’t have worked more perfectly, just what was needed.
Jaws said:
Still useful 11 years later. Thank you very much man!
gopalkaroli said:
yes I also forgot when i did that haha
Jaws said:
Hahahaha internet doesn’t forget! Have a great day!