Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

show RTF string in silverlight 4

Posted on 2010-08-12
3
1,573 Views
Last Modified: 2013-11-12
i have an RTF string coming from a web service into a silverlight 4 application, RTF example begins like:
{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fo......

I want to show this information in a richtextbox or some silverlight control , and i would like the information to look readable like the rendered RTF should be (not looking like above!)

keep in mind silverlight cannot access the system.windows.documents API

if it's not possible then i could consider solutions where the web service code which is ASP.NET fw4 converts the rtf string to xaml?  which i also dont know how to do.

thanks
josh

0
Comment
Question by:joshmiller
  • 2
3 Comments
 
LVL 25

Accepted Solution

by:
apeter earned 500 total points
ID: 33436584
0
 

Author Comment

by:joshmiller
ID: 33447690
yes, that got me on the right track, thanks...

i ended up making a web service / service ref that uses the code below to return an xaml string.  The xaml string is used by the silverlight app to populate a richtextbox. its sort of a hack, but it works for me.
 note: i did have to change the replace("margin function to work with the default margins for the RTF docs.  In this way my solution is weak, it will break if you have different margins, still it works for my needs.


Private Function RTFtoXAML() As String

        Dim xaml As String = ""
        Dim doc As New FlowDocument()
        Dim range As New TextRange(doc.ContentStart, doc.ContentEnd)

        Using ms As New MemoryStream()
            Using sw As New StreamWriter(ms)
                sw.Write(sw)
                sw.Flush()
                ms.Seek(0, SeekOrigin.Begin)
                range.Load(ms, System.Windows.DataFormats.Rtf)
            End Using
        End Using


        Using ms As New MemoryStream()
            range = New TextRange(doc.ContentStart, doc.ContentEnd)

            range.Save(ms, System.Windows.DataFormats.Xaml)
            ms.Seek(0, SeekOrigin.Begin)
            Using sr As New StreamReader(ms)
                xaml = sr.ReadToEnd()
            End Using
        End Using

        ' remove all attribuites in section and remove attribute margin  

        Dim start As Integer = xaml.IndexOf("<Section")
        Dim [stop] As Integer = xaml.IndexOf(">") + 1

        Dim section As String = xaml.Substring(start, [stop])

        xaml = xaml.Replace(section, "<Section xml:space=""preserve"" HasTrailingParagraphBreakOnPaste=""False"" xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"">")
        xaml = xaml.Replace("Margin=""0,0,0,0""", [String].Empty)

        Return xaml

    End Function

Open in new window

0
 

Author Closing Comment

by:joshmiller
ID: 33447696
thanks
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article shows how to make a Windows 7 gadget that accepts files dropped from the Windows Explorer.  It also illustrates how to give your gadget a non-rectangular shape and how to add some nifty visual effects to text displayed in a your gadget.…
If you have ever found yourself doing a repetitive action with the mouse and keyboard, and if you have even a little programming experience, there is a good chance that you can use a text editor to whip together a sort of macro to automate the proce…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question