Click here to Skip to main content
16,015,351 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
How do I convert rtf to html with out losing gap between the lines?
Below is my code. but this will remove extragap between the lines.

before convert
-----
Hi,

How are you?

Thannks
-------
after converting

Hi,
How are you?
Thannks




private const string FlowDocumentFormat = "<flowdocument>{0}";

public static string ConvertRtfToHtml(string rtfText)
{
var xamlText = string.Format(FlowDocumentFormat, ConvertRtfToXaml(rtfText));

return HtmlFromXamlConverter.ConvertXamlToHtml(xamlText, false);
}

private static string ConvertRtfToXaml(string rtfText)
{
var richTextBox = new RichTextBox();
if (string.IsNullOrEmpty(rtfText)) return "";

var textRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);

//Create a MemoryStream of the Rtf content

using (var rtfMemoryStream = new MemoryStream())
{
using (var rtfStreamWriter = new StreamWriter(rtfMemoryStream))
{
rtfStreamWriter.Write(rtfText);
rtfStreamWriter.Flush();
rtfMemoryStream.Seek(0, SeekOrigin.Begin);

//Load the MemoryStream into TextRange ranging from start to end of RichTextBox.
textRange.Load(rtfMemoryStream, DataFormats.Rtf);
}
}

using (var rtfMemoryStream = new MemoryStream())
{

textRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
textRange.Save(rtfMemoryStream, DataFormats.Xaml);
rtfMemoryStream.Seek(0, SeekOrigin.Begin);
using (var rtfStreamReader = new StreamReader(rtfMemoryStream))
{
return rtfStreamReader.ReadToEnd();
}
}

}
Posted
Updated 1-Dec-13 20:44pm
v2
Comments
Maciej Los 2-Dec-13 2:25am    
No details, no answers ;(
vivektp 2-Dec-13 3:35am    
Added the detail.. :)

1 solution

You did not specify any details... In a future you should be more precisely.

Converting between RTF and HTML[^]
Converting RTF to HTML[^]
Writing an RTF to HTML converter, posting code in blogs.[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900