C#: Convert RTF to HTML, Image

RTF, or Rich Text Format, is a file format designed for storing text and formatting information. While processing RTF files, sometimes you might need to convert them into a more web-friendly format such as HTML, or convert to images for better sharing and archiving purposes. In this article, you will learn how to convert RTF to HTML or images in C# using Spire.Doc for .NET.

Install Spire.Doc for .NET

To begin with, you need to add the DLL files included in the Spire.Doc for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.Doc

Convert RTF to HTML in C#

Converting RTF to HTML ensures that the document can be easily viewed and edited in any modern web browser without requiring any additional software.

With Spire.Doc for .NET, you can achieve RTF to HTML conversion through the Document.SaveToFile(string fileName, FileFormat.Html) method. The following are the detailed steps.

  • Create a Document instance.
  • Load an RTF document using Document.LoadFromFile() method.
  • Save the RTF document in HTML format using Document.SaveToFile(string fileName, FileFormat.Html) method.
  • C#
using Spire.Doc;

namespace ConvertRtfToHtml
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Document instance
            Document document = new Document();

            //Load an RTF document 
            document.LoadFromFile("input.rtf");

            //Save as HTML format
            document.SaveToFile("RtfToHtml.html", FileFormat.Html);
        }
    }
}

C#: Convert RTF to HTML, Image

Convert RTF to Image in C#

To convert RTF to images, we can use the Document.SaveToImages() method to convert an RTF file into individual Bitmap or Metafile images. Then, the Bitmap or Metafile images can be saved as a BMP, EMF, JPEG, PNG, GIF, or WMF format files. The following are the detailed steps.

  • Create a Document object.
  • Load an RTF document using Document.LoadFromFile() method.
  • Convert the document to images using Document.SaveToImages() method.
  • Iterate through the converted image, and then save each as a PNG file using Image[].Save(string fileName, ImageFormat format) method.
  • C#
using Spire.Doc;
using System.Drawing.Imaging;
using System.Drawing;
using Spire.Doc.Documents;

namespace ConvertRtfToImage

{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Document instance
            Document document = new Document();

            //Load an RTF document 
            document.LoadFromFile("input.rtf");

            //Convert the RTF document to images
            Image[] images = document.SaveToImages(ImageType.Bitmap);

            // Iterate through the image collection
            for (int i = 0; i < images.Length; i++)
            {
                //Save the image as png format
                string outputfile = string.Format("image-{0}.png", i);
                images[i].Save(outputfile, ImageFormat.Png);
            }
        }
    }
}

C#: Convert RTF to HTML, Image

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.