繁体   English   中英

将MemoryStream中的RTF内容附加到XamRichTextEditor的最简单方法是什么

[英]What's the easiest way to append RTF content from a MemoryStream into an XamRichTextEditor

我正在尝试从标准System.Windows.Controls.RichTextBox控件合并RTF上下文,并将其附加到XamRichTextEditor控件的当前RTF上下文的末尾。 当前,我的以下代码在第一行上引发了空引用异常,我尝试引用临时XamRichTextEditor的“ ActiveDocumentView”,即使我假设在调用Document.Load之后也应该对其进行初始化并填充一些东西。 我愿意以其他方法来解决这个问题,似乎复制/粘贴的想法将是最简单的。

码:

                byte[] byteSig = Encoding.ASCII.GetBytes(rtfContextText);
                using (MemoryStream ms = new MemoryStream(byteSig))
                {
                    XamRichTextEditor tmpRichTextBox = new XamRichTextEditor();
                    tmpRichTextBox.Document.Load(RtfSerializationProvider.Instance, ms); // put current email body into memory stream
                    tmpRichTextBox.ActiveDocumentView.Selection.SelectAll();    // select all content
                    tmpRichTextBox.ActiveDocumentView.Selection.Copy();         // copy content into Clipboard
                    txtTextEditor.ActiveDocumentView.Selection.Paste();         // append Clipboard content into main XamRichTextEditor control
                } 

我能够对自己的问题做出解答,但我始终无法获得复制和粘贴方法的全部功能(我将其中的一些内容粘贴到了另一种中,但是换行符和嵌入式图像似乎可以在结果中被忽略)。

我最终不得不直接手动处理2 RichTextBox上下文的原始数据。 为此,我位于目标上下文中要插入另一个的位置,然后从另一个上下文中剥离了除最外面的分组以外的所有分组(以避免重复根节点)。 然后,我使用此串联版本加载了Target RichTextBox的文档。 (下面的示例代码适用于遇到我遇到的相同问题的任何人。希望它最终可以对某人有所帮助。)

 // get string encoded version of body
                using (MemoryStream ms = new MemoryStream())
                {
                    txtTextEditor.Document.Save(RtfSerializationProvider.Instance, ms);
                    ms.Seek(0, SeekOrigin.Begin);
                    using (StreamReader sr = new StreamReader(ms))
                    {
                        strRtfCurrBody = sr.ReadToEnd();
                    }
                }

// find end and then the beginning of second-to-last grouping
                strRtfSignature = strRtfSignature.Remove(strRtfSignature.LastIndexOf('}'));
                int intCheck = PibsEmailer.RtfFindStartpoint(strRtfSignature, strRtfSignature.LastIndexOf('}'));

                if (intCheck < 0)
                {
                    // this should never happen unless the content of the signature is not in valid RTF format
                    throw new Exception("AddSignature Failed. Sig=\"" + Session.CurrentUser.EmailSignature + "\" , Body=\"" + strRtfCurrBody + "\"  IntCheck=" + intCheck);
                }

                strRtfSignature = strRtfSignature.Substring(intCheck);
                strRtfCurrBody = strRtfCurrBody.Insert(PibsEmailer.RtfFindLastParEnd(strRtfCurrBody), strRtfSignature);

                byte[] byteFullContent = Encoding.ASCII.GetBytes(strRtfCurrBody);
                using (MemoryStream ms = new MemoryStream(byteFullContent))
                {
                    ms.Seek(0, SeekOrigin.Begin);
                    this.txtTextEditor.Selection.SelectAll();
                    this.txtTextEditor.Selection.Document.Load(RtfSerializationProvider.Instance, ms);
                }

为了帮助处理RTF格式的数据,我编写了以下两个函数(在上面的代码中引用):

public static int RtfFindLastParEnd(string source)
    {
        int intLastPar = source.LastIndexOf("par}");
        if (intLastPar > 0)
        {
            intLastPar += 4; // add offset of "par}"
        }
        return intLastPar;
    }

    public static int RtfFindStartpoint(string source, int indexOfClosingBrace)
    {
        string workingString = source;
        int nestCount = 0;
        int currIndex = indexOfClosingBrace;

        while (currIndex > 0)
        {
            if (source[currIndex] == '}')
                nestCount++;
            else if (source[currIndex] == '{')
                nestCount--;

            if (nestCount == 0)
            {
                return currIndex;
            }
            currIndex--;
        }
        return -1;
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM