How to add a page header and footer using C# and .NET


Complete code

using System;
using System.IO;
using SautinSoft;
using static SautinSoft.HtmlToRtf;

namespace Sample
{
    class Test
    {
        static void Main(string[] args)
        {
			// Get your free 100-day key here:   
            // https://sautinsoft.com/start-for-free/
	
            // Add a page header and footer during the conversion of HTML to RTF or DOCX.
            // If you need more information about "HTML to RTF .Net" email us at:
            // support@sautinsoft.com.
            AddHeaderAndFooter();
        }

        public static void AddHeaderAndFooter()
        {
            SautinSoft.HtmlToRtf h = new SautinSoft.HtmlToRtf();
            HtmlConvertOptions opt = new HtmlConvertOptions();
            opt.OutputFormat = HtmlToRtf.OutputFormat.Docx;

            string inpFile = @"..\..\..\Sample.html";
            string outFile = "Result.docx";

            // Set page header and footer.
            string headerFromHtml = File.ReadAllText(@"..\..\..\header.html");
            string footerFromRtf = File.ReadAllText(@"..\..\..\footer.rtf");

            // Add page header.
            opt.PageSetup.PageHeader.Html(headerFromHtml);

            // Add extra space between header and page contents.
            opt.PageSetup.PageHeader.MarginBottom.Mm(10);

            // Add page footer.
            opt.PageSetup.PageFooter.Rtf(footerFromRtf);

            if (h.Convert(inpFile, outFile, opt))
            {
                // Open the result for demonstration purposes.
                System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(outFile) { UseShellExecute = true });
            }
        }
    }
}

Download

Imports System
Imports System.IO
Imports SautinSoft
Imports SautinSoft.HtmlToRtf

Namespace Sample
	Friend Class Test
		Shared Sub Main(ByVal args() As String)
			' Get your free 100-day key here:   
            ' https://sautinsoft.com/start-for-free/
	
			' Add a page header and footer during the conversion of HTML to RTF or DOCX.
			' If you need more information about "HTML to RTF .Net" email us at:
			' support@sautinsoft.com.
			AddHeaderAndFooter()
		End Sub

		Public Shared Sub AddHeaderAndFooter()
			Dim h As New SautinSoft.HtmlToRtf()
			Dim opt As New HtmlConvertOptions()
			opt.OutputFormat = HtmlToRtf.OutputFormat.Docx

			Dim inpFile As String = "..\..\..\Sample.html"
			Dim outFile As String = "Result.docx"

			' Set page header and footer.
			Dim headerFromHtml As String = File.ReadAllText("..\..\..\header.html")
			Dim footerFromRtf As String = File.ReadAllText("..\..\..\footer.rtf")

			' Add page header.
			opt.PageSetup.PageHeader.Html(headerFromHtml)

			' Add extra space between header and page contents.
			opt.PageSetup.PageHeader.MarginBottom.Mm(10)

			' Add page footer.
			opt.PageSetup.PageFooter.Rtf(footerFromRtf)

			If h.Convert(inpFile, outFile, opt) Then
				' Open the result for demonstration purposes.
				System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(outFile) With {.UseShellExecute = True})
			End If
		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:



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.