Convert to HTML specifying the headers and footers export mode in C# and .NET


When working with documents in business applications, it's often necessary to convert formats, such as RTF or Word, to HTML. This is necessary for publishing content online, integrating with web applications, or exchanging data between systems. One common need is to control how the document structure is displayed — especially correctly handling headers and footers, as well as selecting the most appropriate export mode. In this article, we'll take a detailed look at how to implement conversion using the component RTF TO HTML .NET from SautinSoft SDK, with the ability to specify an export mode to control the display of headers and footers in the resulting HTML in C# and .NET.

The export mode is a setting that determines how the resulting HTML file is rendered when converted from another format (such as RTF or DOCX). Selecting an export mode is important to maintain desired formatting or to minimize file size.
Particular attention is paid to how elements such as:

  • Headers and footers. These parts of the document are typically used to display page numbers, dates, authors, or additional information.
  • Document style and structure. The ability to preserve or ignore internal styles, tables, images, etc.

Benefits of using this code example:

  • Display flexibility: choose whether to preserve elements such as headings and grids or ignore them.
  • Content optimization: eliminate unnecessary elements to improve loading speed or compatibility.
  • Compliance: prepare HTML for publishing to various systems or platforms that require specific formatting.

Main parameters for configuring the mode:

  • ExportMode: parameter that allows you to select the export mode.
  • Full: preserve everything in the document, including headers and footers.
  • IgnoreHeadersFooters: exclude headers and footers from the resulting HTML.
  • StripFormatting: remove all styles and leave only plain text.
  • ExportPageSetup: configure page settings based on requirements.

Input file:

HeadersFootersExportMode html input

Output result:

HeadersFootersExportMode html output

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)
        {
            HeadersFootersExportMode();
        }
        /// <summary>
        /// This sample shows how to export headers and footers.
        /// </summary>
        static void HeadersFootersExportMode()
        {
			// 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.
			
            string inpFile = @"..\..\..\doc with header and footer.docx";
            string outFile = @"Result.html";

            RtfToHtml r = new RtfToHtml();

            RtfToHtml.HtmlFlowingSaveOptions opt = new RtfToHtml.HtmlFlowingSaveOptions()
            {
                HeadersFootersExportMode = RtfToHtml.HtmlHeadersFootersExportMode.FirstSectionHeaderLastSectionFooter
            };

            try
            {
                r.Convert(inpFile, outFile, opt);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Conversion failed! {ex.Message}");
            }

            // Open the result.
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(outFile) { 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)
			HeadersFootersExportMode()
		End Sub
		''' <summary>
		''' This sample shows how to export headers and footers.
		''' </summary>
		Private Shared Sub HeadersFootersExportMode()
				    ' 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.
			Dim inpFile As String = "..\..\..\doc with header and footer.docx"
			Dim outFile As String = "Result.html"

			Dim r As New RtfToHtml()

			Dim opt As New RtfToHtml.HtmlFlowingSaveOptions() With {.HeadersFootersExportMode = RtfToHtml.HtmlHeadersFootersExportMode.FirstSectionHeaderLastSectionFooter}

			Try
				r.Convert(inpFile, outFile, opt)
			Catch ex As Exception
				Console.WriteLine($"Conversion failed! {ex.Message}")
			End Try

			' Open the result.
			System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(outFile) 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.