How to convert DOCX, RTF to HTML (Fixed and Flowing modes) in C# and .NET


In today's world of document processing, there is often a need to convert DOCX and RTF files to HTML for display in browsers, web applications, or for further processing. In this article, we'll explain how to accomplish this task using the popular component RTF TO HTML .NET from SautinSoft library, and explore the key concepts, benefits, and application considerations of this solution.

Converting documents from DOCX (Microsoft Word) and RTF (Rich Text Format) to HTML is the process of converting structured, richly formatted text files into a universal markup language that can be displayed in a browser or other HTML-compatible application.

When would this solution be needed?

  • Web publishing: presenting documents on a website in a browser-compatible format.
  • Content migration: transferring documents from Word or RTF to an online format without losing formatting.
  • Text processing and analysis: internal content processes, where HTML offers greater flexibility.
  • Report automation: create dynamic reports and presentations from standard documents.

This approach allows you to automate and simplify workflows related to preparing documents for web publishing or storage.
For example:

  • Create an online system that automatically converts reports to HTML.
  • Ensure compatibility of internal documentation with web applications.
  • Implement a tool for mass conversion of document libraries without loss of quality.

    This sample shows difference between Fixed-HTML and Flowing-HTML.

Complete code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using SautinSoft;

namespace Example
{
    class Program
    {
        static void Main(string[] args)
        {
            FixedAndFlowingHtml();
        }
        /// <summary>
        /// This sample shows difference between Fixed-HTML and Flowing-HTML.
        /// </summary>
        static void FixedAndFlowingHtml()
        {
			// Get your free key here:   
            // https://sautinsoft.com/start-for-free/
			
            // If you need more information about "RTF to HTML .Net" 
            // Email us at: support@sautinsoft.com.
			
            // This file is necessary to get DOCX content as byte array.
            string inpFile = @"..\..\..\example.docx";
            string htmlFixedFile = @"Fixed.html";
            string htmlFlowingFile = @"Flowing.html";

            RtfToHtml r = new RtfToHtml();

            // 1. Convert to HTML-fixed.
            // The HTML in the fixed mode represents HTML document with pages and elements positioned by (x,y).
            try
            {
                r.Convert(inpFile, htmlFixedFile, new RtfToHtml.HtmlFixedSaveOptions() { Title = "Fixed", PageMargins = 20f });
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Conversion failed! {ex.Message}");
            }

            // 2. Convert to HTML-Flowing.
            // The HTML in the flowing mode represents HTML document like a created by a human
            // extended by a whole browser width.
            try
            {
                r.Convert(inpFile, htmlFlowingFile, new RtfToHtml.HtmlFlowingSaveOptions() { Title = "Flowing"});
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Conversion failed! {ex.Message}");
            }


            // Open the results.
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(htmlFixedFile) { UseShellExecute = true });
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(htmlFlowingFile) { UseShellExecute = true });
        }
    }
}

Download

Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.IO
Imports SautinSoft

Namespace Example
	Friend Class Program
		Shared Sub Main(ByVal args() As String)
			FixedAndFlowingHtml()
		End Sub
		''' <summary>
		''' This sample shows difference between Fixed-HTML and Flowing-HTML.
		''' </summary>
		Private Shared Sub FixedAndFlowingHtml()
				    ' Get your free key here:   
            ' https://sautinsoft.com/start-for-free/
			
            ' If you need more information about "RTF to HTML .Net" 
            ' Email us at: support@sautinsoft.com.
			' This file is necessary to get DOCX content as byte array.
			Dim inpFile As String = "..\..\..\example.docx"
			Dim htmlFixedFile As String = "Fixed.html"
			Dim htmlFlowingFile As String = "Flowing.html"

			Dim r As New RtfToHtml()

			' 1. Convert to HTML-fixed.
			' The HTML in the fixed mode represents HTML document with pages and elements positioned by (x,y).
			Try
				r.Convert(inpFile, htmlFixedFile, new RtfToHtml.HtmlFixedSaveOptions() With {
					.Title = "Fixed",
					.PageMargins = 20F
				})
			Catch ex As Exception
				Console.WriteLine($"Conversion failed! {ex.Message}")
			End Try

			' 2. Convert to HTML-Flowing.
			' The HTML in the flowing mode represents HTML document like a created by a human
			' extended by a whole browser width.
			Try
				r.Convert(inpFile, htmlFlowingFile, New RtfToHtml.HtmlFlowingSaveOptions() With {.Title = "Flowing"})
			Catch ex As Exception
				Console.WriteLine($"Conversion failed! {ex.Message}")
			End Try


			' Open the results.
			System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(htmlFixedFile) With {.UseShellExecute = True})
			System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(htmlFlowingFile) With {.UseShellExecute = True})
		End Sub
	End Class
End Namespace

Download


If you need a new code example or have a question: email us at support@sautinsoft.com or ask at Online Chat (right-bottom corner of this page) or use the Form below:


Captcha

Questions and suggestions from you are always welcome!

We are developing .Net components since 2002. We know PDF, DOCX, RTF, HTML, XLSX and Images formats. If you need any assistance with creating, modifying or converting documents in various formats, we can help you. We will write any code example for you absolutely free.