Click here to Skip to main content
Click here to Skip to main content

RTF TO HTML

, 14 Nov 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
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.

//
// 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)

Share

About the Author

chanakamax

United States United States
No Biography provided

Comments and Discussions

 
QuestionKeep justification PinmemberFergal Twomey19-Apr-13 1:36 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.141022.2 | Last Updated 14 Nov 2012
Article Copyright 2012 by chanakamax
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid