【问题标题】:How to replace a text in RichTextBox that contains also an image - WPF如何替换包含图像的 RichTextBox 中的文本 - WPF
【发布时间】:2020-11-23 11:58:46
【问题描述】:

我正在使用以下方法来替换 rtf 文本中的文本。

            rtBox = new Lazy<RichTextBox>();
            MemoryStream stream = new MemoryStream(ASCIIEncoding.Default.GetBytes(rtfTextToBeReplaced));
            TextRange tr = new TextRange(rtBox.Value.Document.ContentStart, rtBox.Value.Document.ContentEnd);
            tr.Load(stream, DataFormats.Rtf);

            string rtf;
            using (var memoryStream = new MemoryStream())
            {
                tr.Save(memoryStream, DataFormats.Rtf);
                rtf = ASCIIEncoding.Default.GetString(memoryStream.ToArray());
            }

            rtf = rtf.Replace("a", "B");

结果是:

{\rtf1\Bnsi\Bnsicpg1252\uc1\htmButsp\deff2{\fonttbl{\f0\fchBrset0 Times New RomBn;}{\f2\fchBrset0 Segoe UI;}}{\colortbl\red0\green0\blue0;\ red255\green255\blue255;}\loch\hich\dbch\pBrd\plBin\ltrpBr\itBp0{\lBng1033\fs18\f2\cf0 \cf0\ql{\f2 {\ltrch B}\li0\ri0\sB0\sb0 \fi0\ql\pBr} } }

是否可以只替换richtextBox 中的文本而不是标签,但当richtextBox 包含文本和图像时保留图像?

如果我用过

                tr.Load(new MemoryStream(Encoding.Default.GetBytes(rtfTextToBeReplaced)), DataFormats.Rtf);
                temp = tr.Text.Trim();
                temp = temp.Replace("a", "B"); 

然后它只是用文本替换了richText,而不是用图像和文本替换了richText。

【问题讨论】:

    标签: wpf image text replace richtextbox


    【解决方案1】:

    在包含图像的RichTextBox 中替换文本的正确方法不是解析原始数据,而是分析inlines 并考虑可能的文本格式化。

    将数据加载到RichTextBox 控件后,使用以下方法将所有出现的指定文本替换为新文本:

    public void ReplaceText(RichTextBox rtb, string prevText, string newText)
    {
        TextPointer start = rtb.Document.ContentStart;
        while (true)
        {
            var searchRange = new TextRange(start, rtb.Document.ContentEnd);
            TextRange foundRange = searchRange.FindTextInRange(prevText);
            if (foundRange == null) break;
    
            foundRange.Text = newText;
            start = foundRange.End; // Continue the searching 
        }
    }  
    
    public static class RichTextBoxParser
    {
        public static TextRange FindTextInRange(this TextRange searchRange, string searchText)
        {
            TextRange result = null;
            int offset = searchRange.Text.IndexOf(searchText, StringComparison.OrdinalIgnoreCase);
            if (offset >= 0)
            {
                var start = searchRange.Start.GetTextPositionAtOffset(offset);
                result = new TextRange(start, start.GetTextPositionAtOffset(searchText.Length));
            }
            return result;
        }
    
        private static TextPointer GetTextPositionAtOffset(this TextPointer position, int offset)
        {
            for (TextPointer current = position; current != null; current = position.GetNextContextPosition(LogicalDirection.Forward))
            {
                position = current;
                var adjacent = position.GetAdjacentElement(LogicalDirection.Forward);
                var context = position.GetPointerContext(LogicalDirection.Forward);
                switch (context)
                {
                    case TextPointerContext.Text:
                        int count = position.GetTextRunLength(LogicalDirection.Forward);
                        if (offset <= count)
                        {
                            return position.GetPositionAtOffset(offset);
                        }
                        offset -= count;
                        break;
                    case TextPointerContext.ElementStart:
                        if (adjacent is InlineUIContainer)
                            offset--;
                        break;
                    case TextPointerContext.ElementEnd:
                        if (adjacent is Paragraph)
                            offset -= 2;
                        break;
                }
            }
            return position;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-10
      • 2020-11-08
      • 1970-01-01
      • 2017-08-10
      • 1970-01-01
      相关资源
      最近更新 更多