title: iTextSharp does not render header/footer when using element generated by XmlWorkerHelper
body: <p>I am trying to add header/footer to a PDF whose content is otherwise generated by XMLWorkerHelper. Not sure if it's a placement issue but I can't see the header/footer.</p>
  
  <p>This is in an ASP.NET MVC app using iTextSharp and XmlWorker packages ver 5.4.4 from Nuget.</p>
  
  <p>Code to generate PDF is as follows:</p>
  
  <pre><code>private byte[] ParseXmlToPdf(string html)
  {
       XhtmlToListHelper xhtmlHelper = new XhtmlToListHelper();
       Document document = new Document(PageSize.A4, 30, 30, 90, 90);
       MemoryStream msOutput = new MemoryStream();
       PdfWriter writer = PdfWriter.GetInstance(document, msOutput);
       writer.PageEvent = new TextSharpPageEventHelper();
       document.Open();
       var htmlContext = new HtmlPipelineContext(null);
       htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory());
       var cssResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(false);
       cssResolver.AddCssFile(HttpContext.Server.MapPath("~/Content/themes/mytheme.css"), true);
       var pipeline = new CssResolverPipeline(cssResolver, new HtmlPipeline(htmlContext, new PdfWriterPipeline(document, writer)));
  
       var worker = new XMLWorker(pipeline, true);
       var parser = new XMLParser();
       parser.AddListener(worker);
       using (TextReader sr = new StringReader(html)) 
       {
           parser.Parse(sr);
       }
       //string text = "Some Random Text";
       //for (int k = 0; k &lt; 8; ++k)
       //{
       //    text += " " + text;
       //    Paragraph p = new Paragraph(text);
       //    p.SpacingBefore = 8f;
       //    document.Add(p);
       //}
       worker.Close();
       document.Close();
       return msOutput.ToArray();
      }
  </code></pre>
  
  <p>Now instead of using these three lines </p>
  
  <pre><code>     using (TextReader sr = new StringReader(html)) 
       {
           parser.Parse(sr);
       }
  </code></pre>
  
  <p>if I comment them out and uncomment the code to add a random Paragraph of text (commented in above sample), I see the header/footer along with the random text.</p>
  
  <p>What am I doing wrong?</p>
  
  <p>The EventHandler is as follows:</p>
  
  <pre><code>    public class TextSharpPageEventHelper : PdfPageEventHelper
      {
          public Image ImageHeader { get; set; }
  
          public override void OnEndPage(PdfWriter writer, Document document)
          {
              float cellHeight = document.TopMargin;
              Rectangle page = document.PageSize;
              PdfPTable head = new PdfPTable(2);
              head.TotalWidth = page.Width;
              PdfPCell c = new PdfPCell(ImageHeader, true);
              c.HorizontalAlignment = Element.ALIGN_RIGHT;
              c.FixedHeight = cellHeight;
              c.Border = PdfPCell.NO_BORDER;
              head.AddCell(c);
              c = new PdfPCell(new Phrase(
                DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss") + " GMT",
                new Font(Font.FontFamily.COURIER, 8)
              ));
              c.Border = PdfPCell.TOP_BORDER | PdfPCell.RIGHT_BORDER |   PdfPCell.BOTTOM_BORDER | PdfPCell.LEFT_BORDER;
              c.VerticalAlignment = Element.ALIGN_BOTTOM;
              c.FixedHeight = cellHeight;
              head.AddCell(c);
              head.WriteSelectedRows(
                0, -1,  // first/last row; -1 flags all write all rows
                0,      // left offset
                  // ** bottom** yPos of the table
                page.Height - cellHeight + head.TotalHeight,
                writer.DirectContent
              );
          }
      }
  </code></pre>