RichTextBox for WPF | ComponentOne
Working with WPF RichTextBox / Saving and Loading
In This Topic
    Saving and Loading
    In This Topic

    C1RichTextBox provides the Text and Html properties to persist the content in the C1RichTextBox control. To preserve the formatting in the content, you need to use Html property instead of Text property. Text property does not retain the formatting of the content
    The Html property gets or sets the formatted content of a C1RichTextBox as an HTML string. The HTML filter built into the C1RichTextBox is rich. It supports CSS styles, images, hyperlinks, lists, and so on. But the filter does not support all HTML; it is limited to features supported by the C1RichTextBox control itself.
    For example, the current version of C1RichTextBox does not support tables. Still, you can use the Html property to display simple HTML documents.
    If you type "Hello world." into a C1RichTextBox, the Html property will return the following markup:

    HTML
    Copy Code
        <html>
        <head> 
        <style type="text/css">  
        .c0 { font-family:Portable User Interface;font-size:9pt; }
        .c1 { margin-bottom:7.5pt; }  
         </style>
         </head>
         <body class="c0">
         <p class="c1">Hello world.</p>
         </body>
         </html>
                
    

    Note that the Html property is just a filter between HTML and the internal C1Document class. Any information in the HTML stream that is not supported by the C1RichTextBox (for example, comments and meta information) is discarded, and will not be preserved when you save the HTML document later.

    Load

    Once you set the content to the C1RichTextBox through Text or Html property, you can render the content in C1RichTextBox in html, RTF and Text formats. You can refer the below code for loading the content in the C1RichTextBox in various formats.
                                            
    //Set the Text  for C1RichTextBox
       richTextBox.Text = "C1RichTextBoxsupports importing and exporting to HTMLand RTF.";
    
    //Convert the C1RichTextBox text to the HTML format
       richTextBox.Text = new HtmlFilter().ConvertFromDocument(richTextBox.Document);
                                                                   
    
                                     
      //Set the Html content for C1RichTextBox
      richTextBox.Html = @"<html>
      <head>
      <style>
      td
      {
            border: solid 1px Red;
        padding: 2px;
      }
      </style>
      </head>
      <body style=""font-family: Verdana; font-size: medium;"">
      <p style='text-align:center; background-color:#FFFFCC; font-size:12pt'>
      <b>C1RichTextBox</b> supports importing and exporting to <span style='text-decoration: underline'>HTML</span>
      and <span style='text-decoration: underline'>RTF</span>.</p>
      </body>
      </html>";
                                                    
      //Convert the C1RichTextBox text to the RTF format
       richTextBox.Text = new RtfFilter().ConvertFromDocument(richTextBox.Document);
    
                     
    //Set the Text  for C1RichTextBox  
    richTextBox.Text = "C1RichTextBoxsupports importing and exporting to HTMLand RTF.";   
    
                                    
    

    Export To Word

    C1RichTextBox enables you to render the richtextbox document to Microsoft Word using the C1Word library. The rendered document contains all the set of properties that define the formatting of the content stored in the textbox.
    To implement this feature, you need to add reference of C1.WPF.Word API to the application. To save the C1RichTextbox document as a word document, first convert it into a byte array by using the MemoryStream class. Then you can use the LoadFromRtf and Save methods of the C1WordDocument class for loading and saving the content as a word document.  The below code snippet shows how to save the C1RichTextBox content as a word document.

     

    C#
    Copy Code
                          
    //Set the Html content for C1RichTextBox
    richTextBox.Html = @"<html>
    <head>
    <style>
    td
    {
     border: solid 1px Red;
     padding: 2px;
    }
    </style>
    </head>
    <body style=""font-family: Verdana; font-size: medium;"">
    <p style='text-align:center; background-color:#FFFFCC; font-size:12pt'>
    <b>C1RichTextBox</b> supports importing and exporting to <span style='text-decoration: underline'>HTML</span>
       and <span style='text-decoration: underline'>RTF</span>.</p>
    </body>
    </html>";
    //Convert the HTML content to the RTF format using RTF filter 
       var rtfText = new C1.WPF.RichTextBox.Documents.RtfFilter().ConvertFromDocument(richTextBox.Document);
    
    //Create an object of C1WordDocument
       C1WordDocument doc = new C1.WPF.Word.C1WordDocument();
    
    //Get the RTF text in bytes using MemoryStream class
       var stream = new MemoryStream(Encoding.UTF8.GetBytes(rtfText));
    
    //Load the RTF text in the C1WordDocument
       doc.LoadFromRtf(stream);
    
    //Save the  content in the Word document format 
       doc.Save("Document.docx");