Easily Convert RTF to HTML by Using C#, VB.NET

Format Conversion is always the hot issue. As is known, there are specified tools used to open different kind of document. For example, Word document is opened with Microsoft Word, while PDF document is opened with PDF viewer. So sometimes, people may choose to convert document format to make sure that the document can be opened with tools which has been installed on system. In this post, I will share a method about how to convert RTF to HTML with C#/VB.NET.

When talking about RTF, we can remind of Word. Although RTF is similar to Word, it uses plain text to describe contents and can be opened by many tools, for example, Microsoft Word, Notepad etc.

In my example, I prepare a RTF document and set format for the contents. And, I use a .NET Word component, Spire.Doc for .NET to realize the conversion more quickly and easily. So, I have added its dll file as reference in project.

The following screenshot presents parts of contents of my RTF document.

STEPS:

Just with two steps, we can convert RTF file to HTML.

  • Declare a document and load it by using document.LoadFromFile(filename, fileformat) method. Please note that the file forma must be chosen as Rtf.
  • Use document.SaveToFile(filename, fileformat) method to convert.  In this method, file format must be selected as HTML.

Use the Code:

C#

using Spire.Doc;

namespace RTF2HTML

{

    class Program

    {

        static void Main(string[] args)

        {

            //Load Document

            Document document = new Document();

            document.LoadFromFile(@”E:\Test.rtf”, FileFormat.Rtf);

 

            //Convert to HTML

            document.SaveToFile(“RTF2HTML.html”, FileFormat.Html);

 

            //Launch File

            System.Diagnostics.Process.Start(“RTF2HTML.html”);

        }

    }

}

VB

Imports Spire.Doc

Namespace RTF2HTML

    Friend Class Program

        Shared Sub Main(ByVal args() As String)

            ‘Load Document

            Dim document As New Document()

            document.LoadFromFile(“E:\Test.rtf”, FileFormat.Rtf)

 

            ‘Convert to HTML

            document.SaveToFile(“RTF2HTML.html”, FileFormat.Html)

 

            ‘Launch File

            System.Diagnostics.Process.Start(“RTF2HTML.html”)

        End Sub

    End Class

End Namespace

RESULT

Freely Download Spire.Doc for .NET

Advertisements

Author: janewdaisy

.NET Program Beginner. Share methods about how to use C#/VB.NET to export data, operate Word, Excel, PDF and other useful skills.

4 thoughts on “Easily Convert RTF to HTML by Using C#, VB.NET”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s