Convenient Solution to Convert RTF to Image (.jpg) in C# and Visual Basic

Document conversion is always the hot topic among developers and there are lots of powerful and easy-to-use converters released. Why is document conversion so popular? It is very helpful for users to read one document in other applications if there is no viewer of original document format installed on users’ system. In this post, I will introduce a solution about document conversion: RTF to Image in C# and Visual Basic.

RTF (Rich Text Format) describe contents with plain text and can save variety of information with different formats. It can be created by many applications, such as MS Word, Wordpad etc. The feature of RTF is compatibility so that it can be a wonderful document converter. Image, great tool to decorate contents, has many formats, including .jpg, .png, .gif, .bmp etc. In this example, the RTF will be converted to .jpg file.

This solution is based on a .NET Word component, Spire.Doc for .NET. So I have installed it on my system and added its dll as reference in my project.

Firstly, load the RTF document through invoking Document.LoadFromFile method. The overloads passed are fileName string and FileFormat.

Secondly, convert RTF to image (.jpg format). Invoke Document.SaveToImages method to save specified page of RTF file to image. The overloads to this method are pageIndex and ImageType. Because I want to save the image as .jpg format, so the ImageType is set as Bitmap. The other image types provided are shown as the following screenshot.

Assign the method result as value for Image instance and then invoke Image.Save method to save this converted image. The overloads passed are string fileName and ImageFormat. Select ImageFormat as Jpeg.

CODE:

C#

using System.Drawing;

using System.Drawing.Imaging;

using Spire.Doc;

using Spire.Doc.Documents;

 

namespace RTF2Image

{

    class Program

    {

        static void Main(string[] args)

        {

            //Load RTF Document

            Document document = new Document();

            document.LoadFromFile(@”E:\Work\Documents\WordDocuments\Blues Introduction.rtf”, FileFormat.Rtf);

 

            //Save to Image

            Image image = document.SaveToImages(0, ImageType.Bitmap);

            image.Save(“RTF2Image.jpg”, ImageFormat.Jpeg);

            System.Diagnostics.Process.Start(“RTF2Image.jpg”);

        }

    }

}

 

Visual Basic

Imports System.Drawing

Imports System.Drawing.Imaging

Imports Spire.Doc

Imports Spire.Doc.Documents

 

Namespace RTF2Image

    Friend Class Program

        Shared Sub Main(ByVal args() As String)

            ‘Load RTF Document

            Dim document As New Document()

            document.LoadFromFile(“E:\Work\Documents\WordDocuments\Blues Introduction.rtf”, FileFormat.Rtf)

 

            ‘Save to Image

            Dim image As Image = document.SaveToImages(0, ImageType.Bitmap)

            image.Save(“RTF2Image.jpg”, ImageFormat.Jpeg)

            System.Diagnostics.Process.Start(“RTF2Image.jpg”)

        End Sub

    End Class

End Namespace

After debugging, we can the converted image from RTF below:

Download Spire.Doc for .NET Here

P.S. The .NET Office component, Spire.Office for .NET can be used to realize this function as well.

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.

1 thought on “Convenient Solution to Convert RTF to Image (.jpg) in C# and Visual Basic”

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