Click here to Skip to main content
15,890,897 members
Articles / Programming Languages / C#
Tip/Trick

RTF TO HTML

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
14 Nov 2012CPOL 29.5K   5   1
Convert RTF TO HTML

Introduction

This is a simple trick for converting RTF to HTML.

Background 

All RTF are viewable in MS Word. Using Microsoft Word we can convert an RTF document to full HTML view. So after that we can extract the HTML source from the converted HTML file using a simple method. 

Using the code 

If you have MS Word, you can add the following code to do this task, just add Microsoft Word reference to your project.

C#
//
// Add Microsoft.Office.Interop.Word
//  
using Word = Microsoft.Office.Interop.Word;    

public string ExtractHtml(string rtfText)
{
    try
    {
        //Create word object
        Word.Application applicationObject = new Word.Application();
        //define path for save your temporary file.
        string userTemp = Path.GetTempPath();
        //Open and save your rtf as HTML in your temp path.
        object missing = Type.Missing;
        object fileName = rtfText;
        object False = false;
        applicationObject.DisplayAlerts = Microsoft.Office.Interop.Word.WdAlertLevel.wdAlertsNone;

        Word.Document documentObject = 
           applicationObject.Documents.Open(ref fileName, ref missing, ref missing, ref missing,
                 ref missing, ref missing, ref missing, ref missing, ref missing,
                 ref missing, ref missing, ref False, ref missing, ref missing,
                  ref missing, ref missing);

        object tempFileName = Path.Combine(userTemp, "tempHtm.html");
        object fileFormt = Word.WdSaveFormat.wdFormatHTML;
        object makeFalse = false;
        object makeTrue = true;
        string absolutePath = tempFileName.ToString();
        if (File.Exists(absolutePath))
        {
            try
            {
                File.Delete(absolutePath);
            }
            catch { }
        }

        documentObject.SaveAs(ref tempFileName, ref fileFormt, 
           ref makeFalse, ref missing, ref makeFalse,
           ref missing, ref missing, ref missing, ref makeFalse, ref missing, ref missing,
           ref missing, ref missing, ref missing, ref missing, ref missing);
        GC.Collect();
        GC.WaitForPendingFinalizers();
        documentObject.Close(ref makeFalse, ref missing, ref missing);
        GC.Collect();
        GC.WaitForPendingFinalizers();
        File.Delete(rtfText);

        String htmlCode = "";

        //Extract html source from the temporary html file.
        if (File.Exists(absolutePath))
        {
            WebClient client = new WebClient();
            htmlCode = client.DownloadString(absolutePath);
            GC.Collect();
            GC.WaitForPendingFinalizers();
            try
            {
                File.Delete(absolutePath);
            }
            catch { }   
        }

        else
        {
            htmlCode = "";
        }
        
        return htmlCode;
    }
    catch
    {
        return "";
    }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionKeep justification Pin
FTWO19-Apr-13 1:36
FTWO19-Apr-13 1:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.