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.
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);
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)
After debugging, we can the converted image from RTF below:
P.S. The .NET Office component, Spire.Office for .NET can be used to realize this function as well.