title: How to set full table width on landscape document with iTextSharp XMLWorkerHelper.GetInstance().ParseXHtml in C# ASP.NET MVC?
body: <p>I am working with ASP.NET MVC and I am using iTextSharp to render an HTML page to PDF, in detail iTextSharp XMLWorkerHelper tool.
  It works fine with all the pages I have to parse and render into PDF when they have a portrait orientation, but <strong>it fails when they have a landscape</strong> one.</p>
  
  <p>Here is a very simple sample of the HTML I would like to render in landscape:</p>
  
  <pre><code>&lt;table style="border:1px solid red; width:280mm;"&gt;
      &lt;tr&gt;
          &lt;td&gt;
              In this cell there is a content that can be very large...
          &lt;/td&gt;
      &lt;/tr&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p>Note that the table width should be of 280mm (a little less than the full A4 width in landscape mode).</p>
  
  <p>And here is the core of the HTML to PDF conversion:</p>
  
  <pre><code>public override void ExecuteResult(ControllerContext context)
  {
      IView viewEngineResult;
      ViewContext viewContext;
  
      if (string.IsNullOrEmpty(ViewName))
          ViewName = context.RouteData.GetRequiredString("action");
  
      context.Controller.ViewData.Model = Model;
  
      var workStream = new MemoryStream();
      var sb = new StringBuilder();
      Document document = new Document(new Rectangle(842f, 595f));
      document.SetPageSize(PageSize.A4.Rotate());
  
      TextWriter tr = new StringWriter(sb);
      viewEngineResult = ViewEngines.Engines.FindView(context, ViewName, null).View;
      viewContext = new ViewContext(context, viewEngineResult,       context.Controller.ViewData,
      context.Controller.TempData, tr);
      viewEngineResult.Render(viewContext, tr);
  
      Stream stream = new MemoryStream(Encoding.UTF8.GetBytes(sb.ToString()));
      PdfWriter pdfWriter = PdfWriter.GetInstance(document, workStream);
      pdfWriter.CloseStream = false; 
  
      document.Open();    
      XMLWorkerHelper.GetInstance().ParseXHtml(pdfWriter, document, stream, (Stream)null);
      document.Close();
  
      new FileContentResult(workStream.ToArray(), "application/pdf").ExecuteResult(context);
  }
  </code></pre>
  
  <p>The result is a landscape document, with a table with a red border, but with a wrong width. It's easy to understand that <strong>the rendered width</strong>, on the PDF, <strong>is</strong> not 280mm, but <strong>the max width the table may have on a portrait A4 document</strong>.</p>
  
  <p>I tried different ways:</p>
  
  <ul>
  <li>I created a PdfPTable, setting to 100% its width. Then I created a PdfPCell inside the table. Finally I put the ParseXHtml result in that cell. The result was a full "A4 landscape width" cell, with my 280mm red border table inside, but with the usual wrong width (the max of a portrait A4 document)</li>
  <li>I tried to insert document.NewPage() before to execute ParseXHtml, because I found on different posts that a "NewPage" invoke is needed due to let the document have right dimensions...but no good results also from this operation.</li>
  </ul>
  
  <p>Maybe I am trying to follow wrong ways, and there is a very simple and stupid way to solve this issue?</p>
  
  <p>Thanks in advance for your suggestions.</p>
  
  <p><em><strong>UPDATED</em></strong></p>
  
  <p>I tried to solve out this problema with a workaround.
  Between the <code>document.Open()</code> and the <code>document.Close()</code> methods I substituted the:
  <code>XMLWorkerHelper.GetInstance().ParseXHtml(pdfWriter, document, stream, (Stream)null);</code></p>
  
  <p>with the following one:</p>
  
  <pre><code>var handler = new SampleHandler();
  using (TextReader sr = new StringReader(sb.ToString()))
  {
     //Parse
     XMLWorkerHelper.GetInstance().ParseXHtml(handler , sr);
  }
  foreach (var element in handler.elements)
  {
     document.Add(element);
  }
  </code></pre>
  
  <p>I created a new class:</p>
  
  <pre><code>public class SampleHandler : IElementHandler
  {
     public List&lt;IElement&gt; elements = new List&lt;IElement&gt;();
  
     public void Add(IWritable w)
     {
        if (w is WritableElement)
        {
           // Here all the logic useful to catch the PDFPTable and redefine
           // its width or other tricks (such as nested tables redefining)
        }
     }
  }
  </code></pre>
  
  <p>As I mentioned with the very first issue, I don't know if there is a more simple way to do it. And to be honest, eve if it works, I don't know if this workaround is good or not...</p>