title: Rich Text to Plain Text via C#?
body: <p>I have a program that reads through a Microsoft Word 2010 document and puts all text read from the first column of every table into a datatable.  However, the resulting text also includes special formatting characters (that are usually invisible in the original Word document).</p>
  
  <p>Is there a way that I can take the string of text that I've read and strip all the formatting characters from it?</p>
  
  <p>The program is pretty simple, and uses the Microsoft.Office.Interop.Word assemblies.  Here is the main loop where I'm grabbing the text from the document:</p>
  
  <pre><code>        // Loop through each table in the document, 
          // grab only text from cells in the first column
          // in each table.
          foreach (Table tb in docs.Tables)
          {
              for (int row = 1; row &lt;= tb.Rows.Count; row++)
              {
                  var cell = tb.Cell(row, 1);
                  var listNumber = cell.Range.ListFormat.ListString;
                  var text = listNumber + " " + cell.Range.Text;
  
                  dt.Rows.Add(text);
              }
          }
  </code></pre>
  
  <p>EDIT: Here is what the text ("1. Introduction") looks like in the Word document:
  <img src="https://i.stack.imgur.com/D1d5A.png" alt="enter image description here"></p>
  
  <p>This is what it looks like before being put into my datatable:
  <img src="https://i.stack.imgur.com/K1R6e.png" alt="enter image description here"></p>
  
  <p>And this is what it looks like when put into the datatable:</p>
  
  <p><img src="https://i.stack.imgur.com/Mxtiq.png" alt="enter image description here"></p>
  
  <p>So, I'm trying to figure out a simple way to get rid of the control characters that seem to be appearing (\r, \a, \n, etc).</p>
  
  <p>EDIT:  Here is the code I'm trying to use.  I created a new method to convert the string:</p>
  
  <pre><code>    private string ConvertToText(string rtf)
      {
          using (RichTextBox rtb = new RichTextBox())
          {
              rtb.Rtf = rtf;
              return rtb.Text;
          }
      }
  </code></pre>
  
  <p>When I run the program, it bombs with the following error:
  <img src="https://i.stack.imgur.com/b9z1O.png" alt="enter image description here"></p>
  
  <p>The variable rtf, at this point, looks like this:
  <img src="https://i.stack.imgur.com/o62FQ.png" alt="enter image description here"></p>
  
  <p>RESOLUTION:  I trimmed the unneeded characters before writing them to the datatable.</p>
  
  <pre><code>        // Loop through each table in the document, 
          // grab only text from cells in the first column
          // in each table.
          foreach (Table tb in docs.Tables)
          {
              for (int row = 1; row &lt;= tb.Rows.Count; row++)
              {
                  var charsToTrim = new[] { '\r', '\a', ' ' };
                  var cell = tb.Cell(row, 1);
                  var listNumber = cell.Range.ListFormat.ListString;
                  var text = listNumber + " " + cell.Range.Text;
                  text = text.TrimEnd(charsToTrim);
                  dt.Rows.Add(text);
              }
          }
  </code></pre>