Convert Word to PDF with Document Headers and Footers

The Word to PDF Converter renders the running header and footer of the Word document on the PDF pages, with the page number and page count fields they contain and with a different first page when the document has one. The feature is controlled by the WordToPdfDocumentOptionsUseHeaderFooterFromWord property, which is true by default.

The WordToPdfDocumentOptions object is exposed through the WordToPdfConverterPdfDocumentOptions property.

Use the Header and Footer of the Word Document

When UseHeaderFooterFromWord is true the header and the footer defined in the Word document appear on the generated pages. When it is false the pages are generated without them.

Use the Header and Footer of the Word Document
wordToPdfConverter.PdfDocumentOptions.UseHeaderFooterFromWord = true;

Replace the Document Header or Footer

A header set in WordToPdfDocumentOptionsPdfHtmlHeader takes the place of the document header and a footer set in WordToPdfDocumentOptionsPdfHtmlFooter takes the place of the document footer. The document header can be kept while the footer is replaced, or the other way round. The header and footer options are described in the HTML Header and Footer with Page Numbers documentation section.

Replace the Document Footer
wordToPdfConverter.PdfDocumentOptions.PdfHtmlFooter.Html = footerHtml;
wordToPdfConverter.PdfDocumentOptions.PdfHtmlFooter.HtmlBaseUrl = footerHtmlBaseUrl;
wordToPdfConverter.PdfDocumentOptions.PdfHtmlFooter.AutoSizeContentHeight = true;

Code Sample - Convert Word to PDF with Document Headers and Footers

C#
// Use EVO PDF Namespace
using EvoPdf.Next;

public byte[] ConvertWordToPdfWithDocumentHeaders(byte[] inputWordBytes, bool useHeaderFooterFromWord,
    string headerHtml, string footerHtml, string htmlBaseUrl)
{
    // Set the license key received after purchase to use the library in licensed mode; leave it commented for demo mode
    // Licensing.LicenseKey = "your-license-key";

    // Create a Word to PDF converter object with default settings
    WordToPdfConverter wordToPdfConverter = new WordToPdfConverter();

    // Render the header and the footer of the Word document on the PDF pages, with the page
    // number and page count fields they contain and with a different first page when the
    // document has one. A HTML header or footer set in the converter takes the place of the
    // document one
    wordToPdfConverter.PdfDocumentOptions.UseHeaderFooterFromWord = useHeaderFooterFromWord;

    if (!string.IsNullOrEmpty(headerHtml))
    {
        // A HTML header set in the converter takes the place of the document header
        wordToPdfConverter.PdfDocumentOptions.PdfHtmlHeader.Html = headerHtml;
        wordToPdfConverter.PdfDocumentOptions.PdfHtmlHeader.HtmlBaseUrl = htmlBaseUrl;
        wordToPdfConverter.PdfDocumentOptions.PdfHtmlHeader.AutoSizeContentHeight = true;
    }

    if (!string.IsNullOrEmpty(footerHtml))
    {
        // A HTML footer set in the converter takes the place of the document footer
        wordToPdfConverter.PdfDocumentOptions.PdfHtmlFooter.Html = footerHtml;
        wordToPdfConverter.PdfDocumentOptions.PdfHtmlFooter.HtmlBaseUrl = htmlBaseUrl;
        wordToPdfConverter.PdfDocumentOptions.PdfHtmlFooter.AutoSizeContentHeight = true;
    }

    // The buffer to receive the generated PDF document
    byte[] outPdfBuffer = wordToPdfConverter.ConvertToPdf(inputWordBytes);

    return outPdfBuffer;
}

See Also