将 rtf 字符串加载到 TextRange 中不包括换行符
Loading rtf string into TextRange doesnt include line breaks
我有一个方法可以设置文本范围和 rtf 字符串。我的问题是将它加载到 TextRange 时,它会丢失换行符。当我从内存流加载 rtf 字符串时,如何确保我的换行符存在?目前我的方法如下:
public static void LoadRtf(string rtf, TextRange range)
{
if (string.IsNullOrEmpty(rtf))
return;
using (MemoryStream rtfMemoryStream = new MemoryStream())
{
using (StreamWriter rtfStreamWriter = new StreamWriter(rtfMemoryStream))
{
rtfStreamWriter.Write(rtf);
rtfStreamWriter.Flush();
rtfMemoryStream.Seek(0, SeekOrigin.Begin);
range.Load(rtfMemoryStream, DataFormats.Rtf);
}
}
}
扩展 RichTextBox 的调用class
public class RtfBox : RichTextBox
{
public object RtfContent
{
get { return GetValue(RtfContentProperty); }
set { SetValue(RtfContentProperty, value); }
}
public static readonly DependencyProperty RtfContentProperty =
DependencyProperty.RegisterAttached("RtfContent", typeof(object), typeof(RtfBox),
new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender, RtfContentChanged));
static void RtfContentChanged(DependencyObject depObj, DependencyPropertyChangedEventArgs evt)
{
RtfBox box = depObj as RtfBox;
if (box == null)
return;
string sContent = evt.NewValue as string;
if (sContent == null)
{
byte[] bContent = evt.NewValue as byte[];
if (bContent != null)
sContent = Encoding.UTF8.GetString(bContent);
}
box.Document.Blocks.Clear();
try
{
TextRange range = new TextRange(box.Document.ContentStart, box.Document.ContentEnd);
FlowDocumentRtfConverter.LoadRtf(sContent, range);
if (box.Document.FontStyle == FontStyles.Normal)
box.Document.FontStyle = box.FontStyle;
if (box.Document.FontWeight == FontWeights.Normal)
box.Document.FontWeight = box.FontWeight;
if (box.Document.FontStretch == FontStretches.Normal)
box.Document.FontStretch = box.FontStretch;
}
catch (Exception e)
{
}
}
public RtfBox()
: base()
{
IsDocumentEnabled = false;
}
}
我目前使用的 rtf 字符串值是这个 "H[=18=]e[=18=]l[=18=]l[=18=]o[=18=]\r[=18=]\n[=18=]n[=18=]d[=18=] [=18=]l[=18=]i[=18=]n[=18=]e[=18=]\r[=18=]"
也许这有什么问题?
我试过你的代码,稍作修改如下,它对我有用。也许这是您初始化文本范围的方式?
public static string LoadRtf(string rtf, TextRange range)
{
if (string.IsNullOrEmpty(rtf))
return "";
using (MemoryStream rtfMemoryStream = new MemoryStream())
{
using (StreamWriter rtfStreamWriter = new StreamWriter(rtfMemoryStream))
{
rtfStreamWriter.Write(rtf);
rtfStreamWriter.Flush();
rtfMemoryStream.Seek(0, SeekOrigin.Begin);
range.Load(rtfMemoryStream, DataFormats.Rtf);
}
}
return range.Text;
}
然后我调用这段代码,返回的字符串有换行符,并在富文本框控件中正确显示。
string rtf = File.ReadAllText(@"c:\temp\document.rtf");
FlowDocument flowDocument = new FlowDocument();
TextRange range = new TextRange(flowDocument.ContentStart, flowDocument.ContentEnd);
this.richTextBox1.Text = LoadRtf(rtf, range);
我将 range.Load 更改为使用 DataFormats.Text,它现在考虑了换行符。
我有一个方法可以设置文本范围和 rtf 字符串。我的问题是将它加载到 TextRange 时,它会丢失换行符。当我从内存流加载 rtf 字符串时,如何确保我的换行符存在?目前我的方法如下:
public static void LoadRtf(string rtf, TextRange range)
{
if (string.IsNullOrEmpty(rtf))
return;
using (MemoryStream rtfMemoryStream = new MemoryStream())
{
using (StreamWriter rtfStreamWriter = new StreamWriter(rtfMemoryStream))
{
rtfStreamWriter.Write(rtf);
rtfStreamWriter.Flush();
rtfMemoryStream.Seek(0, SeekOrigin.Begin);
range.Load(rtfMemoryStream, DataFormats.Rtf);
}
}
}
扩展 RichTextBox 的调用class
public class RtfBox : RichTextBox
{
public object RtfContent
{
get { return GetValue(RtfContentProperty); }
set { SetValue(RtfContentProperty, value); }
}
public static readonly DependencyProperty RtfContentProperty =
DependencyProperty.RegisterAttached("RtfContent", typeof(object), typeof(RtfBox),
new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender, RtfContentChanged));
static void RtfContentChanged(DependencyObject depObj, DependencyPropertyChangedEventArgs evt)
{
RtfBox box = depObj as RtfBox;
if (box == null)
return;
string sContent = evt.NewValue as string;
if (sContent == null)
{
byte[] bContent = evt.NewValue as byte[];
if (bContent != null)
sContent = Encoding.UTF8.GetString(bContent);
}
box.Document.Blocks.Clear();
try
{
TextRange range = new TextRange(box.Document.ContentStart, box.Document.ContentEnd);
FlowDocumentRtfConverter.LoadRtf(sContent, range);
if (box.Document.FontStyle == FontStyles.Normal)
box.Document.FontStyle = box.FontStyle;
if (box.Document.FontWeight == FontWeights.Normal)
box.Document.FontWeight = box.FontWeight;
if (box.Document.FontStretch == FontStretches.Normal)
box.Document.FontStretch = box.FontStretch;
}
catch (Exception e)
{
}
}
public RtfBox()
: base()
{
IsDocumentEnabled = false;
}
}
我目前使用的 rtf 字符串值是这个 "H[=18=]e[=18=]l[=18=]l[=18=]o[=18=]\r[=18=]\n[=18=]n[=18=]d[=18=] [=18=]l[=18=]i[=18=]n[=18=]e[=18=]\r[=18=]" 也许这有什么问题?
我试过你的代码,稍作修改如下,它对我有用。也许这是您初始化文本范围的方式?
public static string LoadRtf(string rtf, TextRange range)
{
if (string.IsNullOrEmpty(rtf))
return "";
using (MemoryStream rtfMemoryStream = new MemoryStream())
{
using (StreamWriter rtfStreamWriter = new StreamWriter(rtfMemoryStream))
{
rtfStreamWriter.Write(rtf);
rtfStreamWriter.Flush();
rtfMemoryStream.Seek(0, SeekOrigin.Begin);
range.Load(rtfMemoryStream, DataFormats.Rtf);
}
}
return range.Text;
}
然后我调用这段代码,返回的字符串有换行符,并在富文本框控件中正确显示。
string rtf = File.ReadAllText(@"c:\temp\document.rtf");
FlowDocument flowDocument = new FlowDocument();
TextRange range = new TextRange(flowDocument.ContentStart, flowDocument.ContentEnd);
this.richTextBox1.Text = LoadRtf(rtf, range);
我将 range.Load 更改为使用 DataFormats.Text,它现在考虑了换行符。