Converting between RTF and HTML

This sample shows how to convert from HTML to RTF and from RTF to HTML. It makes use of the WPF RichTextBox to convert to XAML first and then to RTF or HTML.

C# (91.1 KB)
 
 
 
 
 
4.6 Star
(34)
29,321 times
Add to favorites
7/26/2012
E-mail Twitter del.icio.us Digg Facebook
Sign in to ask a question


  • 555-555-0199@example.com
    1 Posts | Last post October 13, 2017
    • 555-555-0199@example.com
  • hyperlink with umlaut character
    1 Posts | Last post June 28, 2017
    • Html hyperlink conversion to rtf does not work with umlaut character
      e.g. <a href="http://www.müller.com">www.müller.com</a>
      thanks
  • Great stuff!
    1 Posts | Last post June 27, 2017
    • I see you have created a Github repo: https://github.com/mmanela/MarkupConverter. Awesome.
  • Found a little bug
    2 Posts | Last post May 19, 2017
    • In htmlcssparser.cs, method MatchSelectorLevel, the if statements:  
      
      if (selectorId != null && HtmlToXamlConverter.GetAttribute(xmlElement, "id") != 
      selectorId) 
      
      and
      
       if (selectorClass != null && HtmlToXamlConverter.GetAttribute(xmlElement, "class") != selectorClass) 
                  { 
                      return false; 
                  } 
      
      Should convert the retrieved attribute to lowercase as you store css attributes in lowercase to _styleDefinitions. The bug results in css attributes not properly detected when named with capital letters.
      
      A possible fix would look like this:
      if (selectorId != null && (HtmlToModel.GetAttribute(xmlElement, "id")?? string.Empty).ToLower() != selectorId)
      ...
      if (selectorClass != null && (HtmlToModel.GetAttribute(xmlElement, "class")?? string.Empty).ToLower() != selectorClass)
      
    • eh.. I mean...
      
      A possible fix would look like this:
      
      if (selectorId != null && (HtmlToXamlConver.GetAttribute(xmlElement, "id")?? string.Empty).ToLower() != selectorId)
      
      ...
      
      if (selectorClass != null && (HtmlToXamlConver.GetAttribute(xmlElement, "class")?? string.Empty).ToLower() != selectorClass)
      
  • Error Converting RTF to HTML
    1 Posts | Last post July 08, 2016
    • While converting RTF to HTML I have bulleted list of 4 items in RTF. For 2 and 3 items I increased indent. But after conversion i am getting list inside a list instead of Indent. I am missing the indent when I do reverse conversion(HTML to RTF)
      
      Thanks...
      
      
      
      
      
  • Is there a way to convert to VB.NET
    1 Posts | Last post January 12, 2016
    • I tried to use but I don't have C#, is there a way to have dll or in VB.NET ..?
      it's exactelly what I need to send a signature in email message because there is no way to send it in rtf format.. only HTML ...
      
      Thanks .. 
  • Windows Universal Version
    1 Posts | Last post May 14, 2015
    • I know this code is a few years old, but was curious if you had any plans on making a Windows Universal version of this code available.
      
      Thanks
  • Is This Samle available in vb.net ?
    1 Posts | Last post March 17, 2015
    • Hi ,
      I want to make sure if this "converting between RTF and HTML" sample has a vb.net version ??
  • Problem with \par
    1 Posts | Last post February 19, 2015
    • When I try to convert the following text...
      \par\parLet\rquotes get started!
      
      It just deletes "Let" giving only "'s get started"... I noticed the problem came from the XAML conversion. How can I fix that?
  • Solving the problem with no line breaks between lines with text in them
    1 Posts | Last post January 08, 2015
    • Sorry for all of the posts. I found the simplest way to solve the problem of the RTF to HTML converter leaving no line breaks between lines with text in them, even though there should be line breaks. What needs to be done is editing RtfToHtmlConverter.cs, specifically the function ConvertRtfToHtml. After the line
      
      var xamlText = string.Format(FlowDocumentFormat, ConvertRtfToXaml(rtfText));
      
      , add the following line
      
      xamlText = xamlText.Replace("<Span><Run> </Run></Span>", "");
      
      . This line is before
      
      return HtmlFromXamlConverter.ConvertXamlToHtml(xamlText, false);
      
      . This should take care of most line breaks not being there in HTML when they should. After calling the function ConvertRtfToHtml in your own application and storing the result in a variable, e.g., "answer", add the following lines:
      
      answer = answer.Replace("'", "&#39;");
      answer = answer.Replace("<P />", "<BR />");
      
      The first line solves the problem of the RTF file having apostrophes. The second line solves the problem of the existence of more than one "<P />" in a row, since if there is more than one "<P />" in a row, HTML won't allow more than one line break in a row. Hope this helps!
1 - 10 of 31 Items