title: Header and footer are being overwritten while creating PDF
body: <p>I have  included a cs class for creating the footer and header for a pdf file, so what am getting is the header and footer is overwriting the current contents on the file, what i want is the header to show and shift other cells in every page</p>
  
  <pre><code>public  class ITextEvents : PdfPageEventHelper  
      {
  
          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;
          #endregion
  
          #region Properties
          public string Header
          {
              get { return _header; }
              set { _header = value; }
          }
          #endregion
  
          public override void OnOpenDocument(PdfWriter writer, Document document)
          {
              try
              {
                  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)
              {
              }
              catch (System.IO.IOException ioe)
              {
              }
          }
  
          public override void OnEndPage(iTextSharp.text.pdf.PdfWriter writer, iTextSharp.text.Document document)
          {
              base.OnEndPage(writer, document);
              Font georgia = FontFactory.GetFont("georgia", 10f);
              //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", georgia);
  
              //Create PdfTable object  
              PdfPTable pdfTab = new PdfPTable(3);
  
              iTextSharp.text.Image myImage = iTextSharp.text.Image.GetInstance(AppDomain.CurrentDomain.BaseDirectory + @"Content\Images\ArgoGroup.png");
              PdfPCell cell = new PdfPCell(myImage);
              pdfTab.DefaultCell.Padding = 4;
              cell.Border = PdfPCell.NO_BORDER;
  
  
              String text = "Page " + writer.PageNumber + " of ";
              //Add paging to footer  
              {
                  cb.BeginText();
                  cb.SetFontAndSize(bf, 12);
                  cb.SetTextMatrix(document.PageSize.GetRight(180), document.PageSize.GetBottom(30));
                  cb.ShowText(text);
                  cb.EndText();
                  float len = bf.GetWidthPoint(text, 12);
                  cb.AddTemplate(footerTemplate, document.PageSize.GetRight(180) + len, document.PageSize.GetBottom(30));
              }
              cell.HorizontalAlignment = Element.ALIGN_RIGHT;
  
              cell.VerticalAlignment = Element.ALIGN_TOP;
  
              cell.Border = 0;
  
              pdfTab.AddCell(cell);
  
              pdfTab.TotalWidth = document.PageSize.Width - 80f;
              pdfTab.WidthPercentage = 70;
              //pdfTab.HorizontalAlignment = Element.ALIGN_CENTER;      
  
              //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);
              //set pdfContent value  
          }
  
          public override void OnCloseDocument(PdfWriter writer, Document document)
          {
              base.OnCloseDocument(writer, document);
  
              headerTemplate.BeginText();
              headerTemplate.SetFontAndSize(bf, 12);
              headerTemplate.SetTextMatrix(0, 0);
              headerTemplate.ShowText((writer.PageNumber - 1).ToString());
              headerTemplate.EndText();
  
              footerTemplate.BeginText();
              footerTemplate.SetFontAndSize(bf, 12);
              footerTemplate.SetTextMatrix(0, 0);
              footerTemplate.ShowText((writer.PageNumber - 1).ToString());
              footerTemplate.EndText();
          }
      }
  public void createpdf(){
  
  
  
   PdfWriter writer = PdfWriter.GetInstance(document, ms);
  
                      writer.PageEvent = new ITextEvents();
                      document.Open();
                      TextReader t = new StringReader(htmlcontent.ToString());
                      HTMLWorker hw = new HTMLWorker(document);
                      hw.Parse(t);
                      document.Close();
                      writer.Close();
  
  }
  </code></pre>