1

I searched a lot to retain RichTextBox formatting in winform WITHOUT using any Third party library but everywhere people are answering to use Third party.

Please, anyone, suggest me to retain the formatting of RichTextBox on my HTML page without any third party library.Image1 is a RichTextBox Formatting and Image2 is HTML formatting (formatted using ).

Image1 (RichTextBox)

Image2 (HTML Page)

I have tried SimpleTextConverion to HTML but it does not work for me. If anyone can even give me an option to convert RTF to HTML that is also fine for me.

Tilak Dewangan
  • 351
  • 1
  • 2
  • 19
  • Possible duplicate of [Simple text to HTML conversion](https://stackoverflow.com/questions/3991840/simple-text-to-html-conversion) – TAHA SULTAN TEMURI Dec 06 '18 at 10:35
  • This involves that you study the RTF format, which is a text-based one, just like HTML, then create mapping between RTF and HTML. RTF is complex, and you will have to support many, **many** cases - fonts, font sizes, colors, paragraphs, spacings, alignment, tables, images... It is all too complicated and too verbose to be explained here. You don't want to do that. :-) – Nick Dec 06 '18 at 10:36
  • [Writing Your Own RTF Converter](https://www.codeproject.com/Articles/27431/Writing-Your-Own-RTF-Converter) -- [Converting between RTF and HTML](https://code.msdn.microsoft.com/windowsdesktop/Converting-between-RTF-and-aaa02a6e) – Jimi Dec 06 '18 at 11:26
  • @Jimi I converted it using online tools but the formatting is still same as I have shown on Image. – Tilak Dewangan Dec 07 '18 at 06:42
  • @TilakDewangan Did you find solution for that? – Alok Gupta Mar 29 '19 at 09:39

1 Answers1

0

Although your inquiry was posted over three years, I came across it today. So I played around with my visual studio 2022 to find out possibilities.

You may be correct that it is not necessary to use third-party converters. You can do it all yourself using the DataFormat module the IDE provides.

Please, see the self-explanatory code snippet below. If it helps, please, mark it as answered.

Many regards.

Ola

    /// <summary>
    /// =====Written 10.08.2022=========
    /// This 5 steps appraoch converts a richtextbox content to an html file
    /// </summary>
    /// <param name="The_richtextbox">the richtextbox</param>
    /// <param name="the_full_path_filename">
    /// the full path and name you want to call the file plu .html extension <para/>
    /// For instance 1: "C:\\the_name_you_want.html" <para/>
    /// For instance 2: "C:\\your_folder\\the_name_you_want.html" <para/>
    /// </param>
    private static void Convert_RTB_To_HTML(RichTextBox The_richtextbox, string the_full_path_filename)
    {
        try
        {
            //#1. hold a reference to the the_full_path_filename eg: "C:\\the_name_you_want.html"
            string file_path = the_full_path_filename;

            //#2. read the entire richttextbox content to range
            TextRange range = new TextRange(The_richtextbox.Document.ContentStart, The_richtextbox.Document.ContentEnd);

            //#3. Open the IO file stream to create the file. If it exists it will replace it
            FileStream fStream = new FileStream(file_path, FileMode.Create);

            //#4. Now save the file to the format you want to dataformat module
            range.Save(fStream, DataFormats.Html); // use .Rtf preferably

            //#5. Finally, close the stream
            fStream.Close();

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

    /// <summary>
    /// =====Written 10.08.2022=========
    /// This 5 steps appraoch converts an html file to a richtextbox content 
    /// </summary>
    /// <param name="The_richtextbox">the richtextbox</param>
    /// <param name="the_full_path_filename">
    /// the full path and name of the html file <para/>
    /// For instance 1: "C:\\the_name_of_the_html_file.html" <para/>
    /// For instance 2: "C:\\your_folder\\the_name_of_the_html_file.html" <para/>
    /// </param>
    private static void Convert_HTML_To_RTB(RichTextBox The_richtextbox, string the_full_path_filename)
    {
        try
        {
            //#1. hold a reference to the the_full_path_filename eg: "C:\\the_name_of_the_html_file.html"
            string file_path = the_full_path_filename;

            //#2. read the entire richttextbox content to range
            TextRange range = new TextRange(The_richtextbox.Document.ContentStart, The_richtextbox.Document.ContentEnd);

            //#3. Open the IO file stream to read the file.
            FileStream fStream = new FileStream(file_path, FileMode.Create);

            //#4. Now load the file to the range using the dataformat module. You can also try DataFormats.Xaml
            range.Load(fStream, DataFormats.Html); //use .Rtf preferably

            //#5. Finally, close the stream
            fStream.Close();

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
Ola_leke
  • 61
  • 1
  • 6