1
votes

when using this code to load data in qpf

public static void LoadRTF(string rtf, RichTextBox richTextBox)
    {

        if (string.IsNullOrEmpty(rtf))
        {
            throw new ArgumentNullException();
        }

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

        //Create a MemoryStream of the Rtf content

        using (MemoryStream rtfMemoryStream = new MemoryStream())
        {
            using (StreamWriter rtfStreamWriter = new StreamWriter(rtfMemoryStream))
            {
                rtfStreamWriter.Write(rtf);
                rtfStreamWriter.Flush();
                rtfMemoryStream.Seek(0, SeekOrigin.Begin);

                //Load the MemoryStream into TextRange ranging from start to end of RichTextBox.
                try
                {
                    textRange.Load(rtfMemoryStream, DataFormats.Rtf);
                }
                catch { Exception ex; }
            }
        }
    }

the application raise an exception called

Unrecognized structure in data format 'Rich Text Format'.\r\nParameter name: stream

the data is

<span dir="LTR" style="font-size: 9pt; background: none repeat scroll 0% 0% transparent; font-family: &quot;verdana&quot;,&quot;sans-serif&quot;; color: #444444;">Abu Dhabi Retirement Pensions &amp; Benefits Fund has announced that the Fund has fully enabled 37 electronic services provided via its online portal that was launched on June 2012. This is an indication of the Fund&rsquo;s concern to convoy Abu Dhabi&rsquo;s Government Vision and the Fund&rsquo;s strategy. There are 17 services which are allocated for entities. 775 governmental, semi-governmental and private entities are using these services and 2716 transactions have been accomplished last July. 13 other electronic services were categorized as general services targeted the active members, pensioners, and entities in which these services were used 2056 times since they were launched.&nbsp;</span><span dir="LTR" style="font-size: 9pt; font-family: &quot;verdana&quot;,&quot;sans-serif&quot;; color: #444444;"><br /> <span style="background: none repeat scroll 0% 0% transparent;">Moreover, there are seven electronic services presented to the partners from governmental and semi-governmental entities in order to ease the exchanging of information and transactions among the entities and the Fund and to support Abu Dhabi&rsquo;s government strategy to develop services.&nbsp;</span><br /> <span style="background: none repeat scroll 0% 0% transparent;">12 electronic services are expected to be launched for pensioners and beneficiaries and active members in 2014 which will enhance the level of the provided services.</span></span> 

this is the rtf format what is the error {\rtf1\ansi\ansicpg1252\uc1\htmautsp\deff2{\fonttbl{\f0\fcharset0 Times New Roman;}{\f2\fcharset0 Segoe UI;}{\f3\fcharset0 verdana";}}{\colortbl\red0\green0\blue0;\red255\green255\blue255;\red68\green68\blue68;}\loch\hich\dbch\pard\plain\ltrpar\itap0{\lang1033\fs18\f2\cf0 \cf0\ql{\f2 {\loch\f3\cf2\ltrch Abu Dhabi Retirement Pensions & Benefits Fund has announced that the Fund has fully enabled 37 electronic services provided via its online portal that was launched on June 2012. This is an indication of the Fund\rquote s concern to convoy Abu Dhabi\rquote s Government Vision and the Fund\rquote s strategy. There are 17 services which are allocated for entities. 775 governmental, semi-governmental and private entities are using these services and 2716 transactions have been accomplished last July. 13 other electronic services were categorized as general services targeted the active members, pensioners, and entities in which these services were used 2056 times since they were launched. }\line {\loch\f3\cf2\ltrch Moreover, there are seven electronic services presented to the partners from governmental and semi-governmental entities in order to ease the exchanging of information and transactions among the entities and the Fund and to support Abu Dhabi\rquote s government strategy to develop services. \line 12 electronic services are expected to be launched for pensioners and beneficiaries and active members in 2014 which will enhance the level of the provided services.}\li0\ri0\sa0\sb0\fi0\ql\par} } }

1
textRange.Load(rtfMemoryStream, DataFormats.Rtf); - Badr Hussien
The reason is because the data is not RTF, its HTML. It cannot find RTF style tags. - crthompson

1 Answers

1
votes

span is not a rtf tag, it's html, hence the "Unrecognized structure" exception message

try using a tag instead, or put your html source trough a html > RTF converter first.

edit:

okay, so the Span is not a valid rtf tag, but it is a valid wpf class, that goes well into a flowdocument, so I guess it can work in this situation.

there is one issue with your code though: the span class does not seem to have any "dir" property : http://msdn.microsoft.com/en-us/library/system.windows.documents.span%28v=vs.110%29.aspx

so I would suggest removing the dir="ltr" (which is useless in this case anyway if I'm not mistaken)