Convert RTF to PDF with Document Headers and Footers

The RTF to PDF Converter renders the running header and footer of the RTF 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 RtfToPdfDocumentOptionsUseHeaderFooterFromRtf property, which is true by default.

The RtfToPdfDocumentOptions object is exposed through the RtfToPdfConverterPdfDocumentOptions property.

Use the Header and Footer of the RTF Document

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

Use the Header and Footer of the RTF Document
rtfToPdfConverter.PdfDocumentOptions.UseHeaderFooterFromRtf = true;

Replace the Document Header or Footer

A header set in RtfToPdfDocumentOptionsPdfHtmlHeader takes the place of the document header and a footer set in RtfToPdfDocumentOptionsPdfHtmlFooter 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
rtfToPdfConverter.PdfDocumentOptions.PdfHtmlFooter.Html = footerHtml;
rtfToPdfConverter.PdfDocumentOptions.PdfHtmlFooter.HtmlBaseUrl = footerHtmlBaseUrl;
rtfToPdfConverter.PdfDocumentOptions.PdfHtmlFooter.AutoSizeContentHeight = true;

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

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

public byte[] ConvertRtfToPdfWithDocumentHeaders(byte[] inputRtfBytes, bool useHeaderFooterFromRtf,
    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 RTF to PDF converter object with default settings
    RtfToPdfConverter rtfToPdfConverter = new RtfToPdfConverter();

    // Render the header and the footer of the RTF 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
    rtfToPdfConverter.PdfDocumentOptions.UseHeaderFooterFromRtf = useHeaderFooterFromRtf;

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

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

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

    return outPdfBuffer;
}

See Also