title: Convert arabic"unicode" content html or xml to pdf using itextsharp
body: <p>I am trying to create reports in my asp.net MVC3 application after a lot of search I found many blog posts talks about <code>ITextSharp</code> to convert my <code>Html/Razor</code> to <code>Pdf</code> I am trying to parse razor view to get PDf as follows</p>
  
  <pre><code> public void Render(ViewContext viewContext, TextWriter writer)
      {
          var doc = new Document();
  
          // associate output with response stream
          var pdfWriter = PdfWriter.GetInstance(doc, viewContext.HttpContext.Response.OutputStream);
          pdfWriter.CloseStream = false;
  
          viewContext.HttpContext.Response.ContentType = "application/pdf";
          viewContext.HttpContext.Response.ContentEncoding = System.Text.Encoding.UTF8;
  
          // generate view into string
          var sb = new System.Text.StringBuilder();
          TextWriter tw = new System.IO.StringWriter(sb);
          _result.View.Render(viewContext, tw);
          var resultCache = sb.ToString();
  
          //Path to our font
          string arialuniTff = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "ARIALUNI.TTF");
          //Register the font with iTextSharp
          iTextSharp.text.FontFactory.Register(arialuniTff);
  
          //Create a new stylesheet
          iTextSharp.text.html.simpleparser.StyleSheet ST = new iTextSharp.text.html.simpleparser.StyleSheet();
          //Set the default body font to our registered font's internal name
          ST.LoadTagStyle(HtmlTags.BODY, HtmlTags.FACE, "Arial Unicode MS");
          //Set the default encoding to support Unicode characters
          ST.LoadTagStyle(HtmlTags.BODY, HtmlTags.ENCODING, BaseFont.IDENTITY_H);
  
  
          //Parse our HTML using the stylesheet created above
          List&lt;IElement&gt; list = HTMLWorker.ParseToList(new StringReader(resultCache), ST);
          doc.Open();
          //Loop through each element, don't bother wrapping in P tags
          foreach (var element in list)
          {
              doc.Add(element);
          }
  
          doc.Close();
          pdfWriter.Close();
      }
  </code></pre>
  
  <p>the result of that code is 
  <img src="https://i.stack.imgur.com/CW7ml.png" alt="enter image description here"></p>
  
  <p>which is not correct, the arabic word should be "محمد". so what I need is to set document direction to be from right to left </p>
  
  <p><strong>EDIT</strong>
  Thanks to @Romulus</p>
  
  <p>I made a little changes to his code i just replaced adding element to <code>PdfPCell</code> to looping on my Html and set some attributes</p>
  
  <pre><code> //Loop through each element, don't bother wrapping in P tags
          foreach (var element in list)
          {
              //Create a cell and add text to it
              //PdfPCell text = new PdfPCell(new Phrase(element.ToString(), f));
  
              //Ensure that wrapping is on, otherwise Right to Left text will not display
              //text.NoWrap = false;
  
              //Add the cell to the table
              //table.AddCell(text);
              if (element is iTextSharp.text.pdf.PdfPTable)
              {
                  table = (iTextSharp.text.pdf.PdfPTable)element;
                  table.DefaultCell.NoWrap = false;
                  table.RunDirection = PdfWriter.RUN_DIRECTION_RTL;
                  foreach (PdfPRow row in table.Rows)
                  {
                      foreach (PdfPCell cell in row.GetCells())
                      {
                          cell.RunDirection = PdfWriter.RUN_DIRECTION_RTL;
                          cell.NoWrap = false;
                      }
                  }
              }
  
          }
  </code></pre>
  
  <p>That's working for me now well Thanks :)</p>