none
Convert rtf text in XAML RRS feed

  • Question

  • I am trying to convert rtf text in XAML by following  code

                TextRange textRange = new TextRange(richTextBox1.Document.ContentStart, richTextBox1.Document.ContentEnd);
                MemoryStream ms = new MemoryStream();
                textRange.Save(ms, System.Windows.DataFormats.Xaml);
                string str = UTF32Encoding.Default.GetString(ms.ToArray());
                ms.Close();

    This code successfully converts rtf to XAML.

    But My problem is that XAML text  str  loosing image which is inside richTextBox1.
    Saturday, December 29, 2012 8:15 AM

All replies

  • If you want to include image then you can use DataFormats.XamlPackage instead of DataFormats.Xaml

    http://msdn.microsoft.com/en-us/library/system.windows.dataformats.xamlpackage.aspx

     TextRange textRange = new TextRange(richTextBox1.Document.ContentStart, richTextBox1.Document.ContentEnd);
     MemoryStream ms = new MemoryStream();
     textRange.Save(ms, System.Windows.DataFormats.XamlPackage);
     string str = UTF32Encoding.Default.GetString(ms.ToArray());
     ms.Close();



    Gaurav Khanna | Microsoft VB.NET MVP | Microsoft Community Contributor

    Saturday, December 29, 2012 10:12 AM
  • XamlPackage is Not giving me appropriate result. 
    Any body would like to tell me  is it doable or not to keep image in XAML text ?

    Monday, December 31, 2012 6:55 AM
  • Hi sanjay Kr. Gupta,

    is the converter understand STA thread, additional, is it console applicaton.

    If your issue persists, could you share a simple sample to reproduce your issue.

    Best regards,


    Sheldon _Xiao
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, January 1, 2013 9:27 AM
    Moderator
  • Well now this is interesting, thanks to Khanna my quest for solving this problem is over...

    This works:

    <Window x:Class="WpfApplication1.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="700" Width="525">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="25"></RowDefinition>
                <RowDefinition></RowDefinition>
                <RowDefinition></RowDefinition>
            </Grid.RowDefinitions>
            <Button Height="25"
                    Click="Button_Click_1">Click to save me!</Button>
          <RichTextBox Name="richTextBox1" Grid.Row="1"
                         TextChanged="richTextBox1_TextChanged">
              <FlowDocument>
                  <Paragraph>Now is the time for all good men to come to the aid of their party</Paragraph>
                  <BlockUIContainer>
                      <Image Width="30" Source="stop_sign.png"></Image>
                  </BlockUIContainer>
                  <Paragraph>And the stop sign above is saved from this code and inserted into RTB2 when the button is pressed</Paragraph>
    
                
              </FlowDocument>
          </RichTextBox>
            <RichTextBox Grid.Row="2" Name="richtextbox2">
                
            </RichTextBox>
        </Grid>
    </Window>

    Note replace the image with any image you import into your project. Now put this code into the code behind...for a demo...

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        var textRange = new TextRange(richTextBox1.Document.ContentStart, richTextBox1.Document.ContentEnd);
        MemoryStream ms = new MemoryStream();
        textRange.Save(ms, System.Windows.DataFormats.XamlPackage);
        string str = UTF32Encoding.Default.GetString(ms.ToArray());
        FlowDocument flowDocument = new FlowDocument();
        TextRange textRange2 = new TextRange(flowDocument.ContentStart, flowDocument.ContentEnd);
        textRange2.Load(ms, DataFormats.XamlPackage);
        ms.Close();
        richtextbox2.Document = flowDocument;
    }

    Guess what it works!


    JP Cowboy Coders Unite!

    Wednesday, January 2, 2013 3:48 AM
  • How to save Images in a RTB!

    1. Create a TextRange from ContentStart to ContentEnd of RTB containing image.
    2. Use the TextRange Save to send data to a Stream, using the DataFormts.XAMLPackage.
    3. You may convert it all to a string using UTF32Enconding (this embeds the image data into a string representation)
    4. To read it back in create a new FlowDocument
    5. Create a TextRange of the entire FlowDocument
    6. Use the TextRange.Load to load the Stream but make sure to use Dataformates.XAMLPackage
    7. Set the RTB.Document value to the Flowdocument.

    Sample Code:

    private MemoryStream SaveRTB()
    {
        var textRange = new TextRange(richTextBox1.Document.ContentStart, richTextBox1.Document.ContentEnd);
        var ms = new MemoryStream();
        textRange.Save(ms, System.Windows.DataFormats.XamlPackage);
        string str = UTF32Encoding.Default.GetString(ms.ToArray());
        ms.Close();
        return ms;
    }
    
    private void PopulateRTB(MemoryStream ms)
    {
        FlowDocument flowDocument = new FlowDocument();
        TextRange textRange2 = new TextRange(flowDocument.ContentStart, flowDocument.ContentEnd);
        textRange2.Load(ms, DataFormats.XamlPackage);
        richtextbox2.Document = flowDocument;
    }

     


    JP Cowboy Coders Unite!

    Wednesday, January 2, 2013 3:55 AM
  • Thanks for your reply

    Now i want to convert XAML to HTML , Please give me any suggestion , how it could be done.

    Actually  i had to convert rtf text to HTML , I gone throw many articles , They suggest me that directly

    it is not possible , First you need to convert rtf to XAML then XAML to HTML .

    its very urgent .

    Wednesday, January 2, 2013 12:35 PM
  • Try following link

    http://msdn.microsoft.com/en-us/library/vstudio/aa972129(v=vs.90).aspx


    Gaurav Khanna | Microsoft VB.NET MVP | Microsoft Community Contributor

    Wednesday, January 2, 2013 2:19 PM
  • Thanks for your reply

    I had tried this link, it convert only textual XAML to HTML ,   Given code is not converting  image content in 

    XamlPackage to HTML.

    If XamlPackage can contain image ,

    textRange.Save(ms, System.Windows.DataFormats.XamlPackage);
        string str = UTF32Encoding.Default.GetString(ms.ToArray());

    Then suggest me how to show this str in HTML text.

    Wednesday, January 2, 2013 3:57 PM