本文介绍了如何将rtf转换为HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将rtf转换为html而不会在线之间出现差距?

以下是我的代码。但是这将消除线之间的额外差距。


转换前


-----





你好吗?



谢谢

----- - 转换后






你好吗?

Thannks









私有const字符串FlowDocumentFormat =< flowdocument> ; {0};



公共静态字符串ConvertRtfToHtml(字符串rtfText)

{

var xamlText = string.Format(FlowDocumentFormat,ConvertRtfToXaml(rtfText));



返回HtmlFromXamlConverter.ConvertXamlToHtml(xamlText,false);

}



私有静态字符串ConvertRtfToXaml(字符串rtfText)

{

var richTextBox = new RichTextBox();

if(string.IsNullOrEmpty(rtfText))return;



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



//创建一个Rtf内容的MemoryStream



使用(var rtfMemoryStream = new MemoryStream())

{

using(var rtfStreamWriter = new StreamWriter(rtfMemoryStream))

{

rtfStreamWriter.Write(rtfText);

rtfStreamWriter.Flush( );

rtfMemoryStream.Seek(0,SeekOrigin.Begin);



//将MemoryStream加载到TextRange中,从开始到结束RichTextBox。

textRange.Load(rtfMemoryStream,DataFormats.Rtf);

}

}



使用(var rtfMemoryStream = new MemoryStream())

{



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

textRange.Save(rtfMemoryStream,DataFormats .Xaml);

rtfMemoryStream.Seek(0,SeekOrigin.Begin);

使用(var rtfStreamReader = new StreamReader(rtfMemoryStream))

{

返回rtfStreamReader.ReadToEnd();

}

}



}

解决方案

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();
}
}

}

解决方案


这篇关于如何将rtf转换为HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 05:51