title: How to add a Pdfptable little by little to a document when generating a Pdf from a Html
body: <p>The application I'm working on generates pdf reports from html files using itextsharp library. Apparently, when large tables are generated, itextsharp allocates a lot of memory (~300 MB for a 200KB file). 
  A solution to this problem is adding the table little by little to the document (so all existing data in table will be flushed), as described in the following links:</p>
  
  <ul>
  <li><a href="https://developers.itextpdf.com/examples/tables-itext5/large-tables" rel="nofollow noreferrer">IText Large Tables</a></li>
  <li><a href="https://stackoverflow.com/questions/15482142/how-to-reduce-memory-consumption-of-pdfptable-with-many-cells">How to reduce memory consumption of PdfPTable with many cells</a></li>
  </ul>
  
  <p><strong>Question</strong>: How can I add a pdfptable in steps when generating the pdf from an existing html file?</p>
  
  <p>Here is my code:</p>
  
  <pre><code>public byte[] GetReportPdf(string template, string cssString)
      {
          byte[] result;
  
          using (var stream = new MemoryStream())
          {
              using (
                  var doc = new Document(
                      this.Settings.Size,
                      this.Settings.Margins.Left,
                      this.Settings.Margins.Right,
                      this.Settings.Margins.Top,
                      this.Settings.Margins.Bottom))
              {
                  using (var writer = PdfWriter.GetInstance(doc, stream))
                  {
                      // adding the page event, or null
                      writer.PageEvent = this.Settings.PageEvent;
  
                      doc.Open();
  
                      // CSS
                      var cssResolver = new StyleAttrCSSResolver();
  
                      using (var cssStream = new MemoryStream(Encoding.UTF8.GetBytes(cssString)))
                      {
                          var cssFile = XMLWorkerHelper.GetCSS(cssStream);
                          cssResolver.AddCss(cssFile);
                      }
  
                      // HTML
                      var fontProvider = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);
                      var cssAppliers = new CssAppliersImpl(fontProvider);
                      var htmlContext = new HtmlPipelineContext(cssAppliers);
                      htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory());
  
                      // pipelines
                      var pdf = new PdfWriterPipeline(doc, writer);
                      var html = new HtmlPipeline(htmlContext, pdf);
                      var css = new CssResolverPipeline(cssResolver, html);
  
                      // XML worker
                      var worker = new XMLWorker(css, true);
                      var parser = new XMLParser(worker);
                      using (var stringReader = new StringReader(template))
                      {
                          parser.Parse(stringReader);
                      }
  
  
                      doc.Close();
                  }
              }
  
              result = stream.ToArray();
          }
  
          return result;
  </code></pre>
  
  <p><strong>Notes</strong>:</p>
  
  <ol>
  <li>The solution in the previous links are not using an html to create
  the pdf</li>
  <li>The steps described are: set table complete property to false, add every 50 table rows to the document, set the table complete property to true.  </li>
  <li>Using an AbstractTagProcessor, I managed to set the table complete property when the html is parsed, <strong>but found no option on how to trigger table adding while it's generated.</strong> </li>
  <li>itextsharp version 5.5.10.0</li>
  <li>itextsharp.xmlworker version 5.5.10.0</li>
  </ol>
  
  <p>var tagFactory = Tags.GetHtmlTagProcessorFactory(); </p>
  
  <pre><code>tagFactory.AddProcessor(new TableTagProcessor(doc), new string[]{"table"});
  
  public class TableTagProcessor : iTextSharp.tool.xml.html.table.Table{
  
  public override IList&lt;IElement&gt; Start(IWorkerContext ctx, Tag tag)
  
  {
      var result = base.Start(ctx, tag);
      foreach (PdfPTable table in result.OfType&lt;PdfPTable&gt;())
      {
          table.Complete = false;
      }
  
      return result;
  }
  
  public override IList&lt;IElement&gt; End(IWorkerContext ctx, Tag tag, IList&lt;IElement&gt; currentContent)
  {
      var result = base.End(ctx, tag, currentContent);
      foreach (PdfPTable table in result.OfType&lt;PdfPTable&gt;())
      {
          table.Complete = true;
      }
  
      return result;
  }}
  </code></pre>