Knowledge Base > RichTextBox > Features Knowledge Base Home | Login
Search the Knowledge Base
 
Start Search in the Following Category

Using RichTextBox with RTF Documents/Strings

Using RichTextBox with RTF Documents/Strings

 

The Professional RichTextBox product can load an RTF file or RTF stream (LoadRTF method passing a file path or stream object as a parameter) however it's converted into HTML as the RichTextBox editor is an IFrame element. You can then edit the content client side which modifies the Html, but what you can't do is save it back/convert to and RTF format again using RichTextBox directly.

 

If you save your edited Html document down using the SaveHtml method, you'll then need to convert this back to a .RTF file which you can do by manipulating the Word COM to convert an HTML document into an RTF document on the Server, or by using another .NET tool such as Active RTF

 

To use the LoadRtf method with a RTF string you would code the following -

 

Add the following namespace references -

 

using System.IO;

using System.Text;

 

Then code like the following will achieve what you require by passing a stream object to the LoadRtf method. Obviously the rtf string will be picked up from a Database or other Data store.

 

private void Page_Load(object sender, System.EventArgs e)

{

  string firstString = @"{\rtf1\ansi\ansicpg1252\deff0\deflang2057{\fonttbl {\f0\fswiss\fcharset0 Arial;}}{\colortbl ;\red255\green0\blue0;}{\*\generator Msftedit 5.41.15.1503;}\viewkind4\uc1\pard\cf1\b\f0\fs20 Rich\cf0 TextBox\b0  from RTF/stream\par}";

                       

  Stream memStream = new   

  MemoryStream(UTF8Encoding.UTF8.GetBytes(firstString));

  this.RichTextBox1.LoadRtf(memStream);

}

 

 

Below is an example of a simple MS Word macro (below) to show how this is easy to do with VBA. This could be better done using a VB .exe and programming the MS Word COM (Component Object Model).

 

Sub Macro2()

 

 Documents.Open FileName:="Test2.htm", ConfirmConversions:=False, ReadOnly _

        :=False, AddToRecentFiles:=False, PasswordDocument:="", PasswordTemplate _

        :="", Revert:=False, WritePasswordDocument:="", WritePasswordTemplate:="" _

        , Format:=wdOpenFormatAuto

    ActiveDocument.SaveAs FileName:="Test2.rtf", FileFormat:=wdFormatRTF, _

        LockComments:=False, Password:="", AddToRecentFiles:=True, WritePassword _

        :="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, _

        SaveNativePictureFormat:=False, SaveFormsData:=False, SaveAsAOCELetter:= _

        False

 

End Sub

 




Did this page answer your question? If not, try the Forums or contact us.

Copyright Richer Components 2005. All rights reserved. No part of this article may be reproduced or transmitted in any form, by any means (electronic, photocopying, recording or otherwise) without the prior written permission of Richer Components. All brand names and product names used in this article are trade names, service marks, trademarks, or registered trademarks of their respective owners.


Knowledge Base Software - myKB.com