代码之家  ›  专栏  ›  技术社区  ›  Derek Meyer

将rtf字符串加载到TextRange中不包括换行符

  •  0
  • Derek Meyer  · 技术社区  · 6 年前

    我有一个方法可以设置文本范围from和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的调用类

    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\0e\0l\0l\0o\0\r\0\n\02\0n\0d\0\0l\0i\0n\0e\0\r\0” 也许这有什么问题?

    2 回复  |  直到 6 年前
        1
  •  0
  •   Kevin    6 年前

    我尝试了您的代码,只做了如下轻微的修改,它对我起到了作用。也许这就是你初始化文本范围的方式?

            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);
    
        2
  •  0
  •   Derek Meyer    6 年前

    我改变了范围。加载以使用数据格式。文本,现在考虑换行符。