Shourya Verdhan

Shourya Verdhan

  • NA
  • 88
  • 56.3k

How to Add Header and footer in all pages in existing pdf

Dec 2 2015 4:03 AM
Hello All,
Any one can tell me how can add Header and footer as image in existing PDF file.
Even i did that but it is working for two pages only, for first and last page.
My Code is.
 
var header = iTextSharp.text.Image.GetInstance(Path.Combine(HttpRuntime.AppDomainAppPath + "images\\letter-head-top.jpg"));
header.ScalePercent(60f);
header.SetAbsolutePosition(0, 760); // X and Y Accroding to need
var footer = iTextSharp.text.Image.GetInstance(Path.Combine(HttpRuntime.AppDomainAppPath + "images\\letter-head-bottom.jpg"));
footer.ScalePercent(60f);
footer.SetAbsolutePosition(0, 0); // X and Y Accroding to need
FileStream file = new FileStream(pathname, FileMode.Create, System.IO.FileAccess.Write);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
hw.Write(htmlpdf); // htmlpdf = Some HTML code render to create pdf
StringReader sr = new StringReader(sw.ToString());

iTextSharp.text.Document pdfDoc = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 10f, 10f, 100f, 50f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
MemoryStream ms = new MemoryStream();
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, file);
pdfDoc.Open();
pdfDoc.Add(header); //Adding Header
pdfDoc.Add(footer); //Adding Footer
htmlparser.Parse(sr);
pdfDoc.Add(header); //Adding Header
pdfDoc.Add(footer); //Adding Footer
htmlparser.Parse(sr);
pdfDoc.Close();
file.Close();

 Result is good for two pages because i use
pdfDoc.Add(header); //Adding Header
pdfDoc.Add(footer);
htmlparser.Parse(sr);
pdfDoc.Add(header); //Adding Header
pdfDoc.Add(footer);
htmlparser.Parse(sr);
 
as you can see this is adding header and footer in first and last page ,
 
Now any one can tell me how can add in other pages as well,or let me provide process to add in existing PDF file.
 
Regards,
Shourya V Sengar
 
 
 
 

Answers (1)