title: Set html content as header and footer in iTextPDF using Java
body: <p>In my application I've to generate a report from the html content. For this purpose I am using the iTextPDF library 5.5.0 with XMLWorker 5.5.0. End user can design the content of report (Header, Footer, Subject) using tinyMC editor. I am storing those HTML content in DB. Please note this HTML content may include images, tables or any thing that tinyMC allows. Now when user tries to generate the report, I am fetching the data from the database and tried to generate the header and footer from HTML using code given below.</p>
  
  <pre><code>       /**
           * Adds the header and the footer.
           * @see com.itextpdf.text.pdf.PdfPageEventHelper#onEndPage(
           *      com.itextpdf.text.pdf.PdfWriter, com.itextpdf.text.Document)
           */
          public void onEndPage(PdfWriter writer, Document document) {
              Phrase footerPhrase = new Phrase();
  
              DBFooter = "&lt;table width='200 border='1' cellpadding='1' cellspacing='1'&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;content1&lt;/td&gt;&lt;td&gt;content2&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt;
              &lt;/tr&gt;&lt;tr&gt;&lt;td&gt;jhvhjvh&lt;/td&gt;&lt;td&gt;&lt;img src='D:\DemoApp\images\User1.png' width='48' height='48' alt='' /&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
              &lt;p style='text-align: center;'&gt;This is the footer&lt;/p&gt;"
  
              CSSResolver cssResolver = new StyleAttrCSSResolver();
              CssFile cssFile = XMLWorkerHelper.getCSS(new ByteArrayInputStream(CSS.getBytes()));
              cssResolver.addCss(cssFile);
  
              // HTML
              HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
              htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());
  
              // Pipelines
              ElementList elements = new ElementList();
              ElementHandlerPipeline pdf = new ElementHandlerPipeline(elements, null);
              HtmlPipeline html = new HtmlPipeline(htmlContext, pdf);
              CssResolverPipeline css = new CssResolverPipeline(cssResolver, html);
  
              // XML Worker
              XMLWorker worker = new XMLWorker(css, true);
              XMLParser p = new XMLParser(worker);
              try {
                  p.parse(new ByteArrayInputStream(DBFooter.toString().getBytes()));
              } catch (IOException e) {
              }
              footerPhrase.addAll(elements);
  
              Rectangle rect = writer.getBoxSize("art");
              switch (writer.getPageNumber() % 2) {
              case 0:
                  ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_RIGHT, header[0], rect.getRight(),
                                             rect.getTop(), 0);
                  break;
              case 1:
                  ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_LEFT, header[1], rect.getLeft(),
                                             rect.getTop(), 0);
                  break;
              }
  
              ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_CENTER, footerPhrase,
                                         (rect.getLeft() + rect.getRight()) / 2, rect.getBottom() - 50, 0);
          }
  </code></pre>
  
  <p>I've followed <a href="http://itextpdf.com/examples/iia.php?id=103" rel="nofollow">this link</a> to add header footer. And changed the onEndPage function as above.</p>
  
  <p>Problem is the above code only prints the "<strong>This is the footer</strong>" from the content of footer. It's not showing the table. How can I bring the HTML content in Header / Footer.</p>
  
  <p>Please note that I can show html content in report body (including table / images etc.) but not in header / footer.</p>