title: How to add html string a Header and footer on Every Page
body: <p>I am new to Itext and creating a pdf using a html string. Now I need to add footer and header on every page. I have a html string that i want to add as header and footer. </p>
  
  <p>I gone through many posts and documentation that is in java but that is confusing as no one using html string as footer or header. </p>
  
  <p><strong>Update Adding code</strong>  </p>
  
  <p>Here is the code that I am using. Now I am able to add header and Footer but the html is rendering as string rather then html UI.</p>
  
  <pre><code> public class TestPDF
  {
      public static void GeneratePDF(string html)
      {
          try
          {
              Guid random_guid;
              random_guid = Guid.NewGuid();
              string fileName = random_guid.ToString() + ".pdf";
              string filename_with_folder = @"Temp\" + fileName;
              string DEST = System.IO.Path.Combine(HttpContext.Current.Request.PhysicalApplicationPath, filename_with_folder);
              new TestPDF().CreatePdf(DEST, html);
          }
          catch (Exception ex)
          {
  
              throw;
          }
      }
      public void CreatePdf(String dest, string html)
      {
          //Initialize PDF document
          PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
          string header = "Header HTmL HERE";
          Header headerHandler = new Header(header);
          PageXofY footerHandler = new PageXofY(pdf);
          //Assign event-handlers
          pdf.AddEventHandler(PdfDocumentEvent.START_PAGE, headerHandler);
          pdf.AddEventHandler(PdfDocumentEvent.END_PAGE, footerHandler);
          // Initialize document
          Document document = new Document(pdf);
          Paragraph p = new Paragraph(html);
          document.Add(p);
          document.Close();
      }
  }
  //Header event handler
  public class Header : IEventHandler
  {
      string header;
      public Header(string header)
      {
          this.header = header;
      }
      public void HandleEvent(Event e)
      {
          //Retrieve document and
          PdfDocumentEvent docEvent = (PdfDocumentEvent)e;
          PdfDocument pdf = docEvent.GetDocument();
          PdfPage page = docEvent.GetPage();
          Rectangle pageSize = page.GetPageSize();
          PdfCanvas pdfCanvas = new PdfCanvas(
                  page.GetLastContentStream(), page.GetResources(), pdf);
          Canvas canvas = new Canvas(pdfCanvas, pdf, pageSize);
          canvas.SetFontSize(18f);
          //Write text at position
          canvas.ShowTextAligned(header,
                  pageSize.GetWidth() / 2,
                  pageSize.GetTop() - 30, TextAlignment.CENTER);
      }
  }
  
  //Footer handler
  public class PageXofY : IEventHandler
  {
      public PdfFormXObject placeholder;
      public float side = 20;
      public float x = 300;
      public float y = 25;
      public float space = 4.5f;
      public float descent = 3;
      public PageXofY(PdfDocument pdf)
      {
          placeholder =
                  new PdfFormXObject(new Rectangle(0, 0, side, side));
      }
      public void HandleEvent(Event e)
      {
          PdfDocumentEvent docEvent = (PdfDocumentEvent)e;
          PdfDocument pdf = docEvent.GetDocument();
          PdfPage page = docEvent.GetPage();
          int pageNumber = pdf.GetPageNumber(page);
          Rectangle pageSize = page.GetPageSize();
          PdfCanvas pdfCanvas = new PdfCanvas(
                  page.GetLastContentStream(), page.GetResources(), pdf);
          Canvas canvas = new Canvas(pdfCanvas, pdf, pageSize);
          //Paragraph p = new Paragraph()
          //        .Add("Page ").Add(pageNumber.ToString()).Add(" of");
          Paragraph p = new Paragraph()
                  .Add("Footer html will here");
          canvas.ShowTextAligned(p, x, y, TextAlignment.RIGHT);
          pdfCanvas.AddXObject(placeholder, x + space, y - descent);
          pdfCanvas.Release();
      }
      public void writeTotal(PdfDocument pdf)
      {
          Canvas canvas = new Canvas(placeholder, pdf);
          canvas.ShowTextAligned(pdf.GetNumberOfPages().ToString(),
                  0, descent, TextAlignment.LEFT);
      }
  }
  </code></pre>