EVO HTML to PDF Converter allows you to apply headers and footers on the external documents added before or after the conversion result. You can control the visibility of the header and footer in first page of the external PDF document using the parameters of the PdfDocumentOptionsAddStartDocument(String, Boolean, Boolean, Boolean) and PdfDocumentOptionsAddEndDocument(String, Boolean, Boolean, Boolean) methods. An object of PdfDocumentOptions type is exposed by the HtmlToPdfConverterPdfDocumentOptions property.
Code Sample - Add Header and Footer In External PDF Documents
C#
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Hosting; // Use EVO PDF Namespace using EvoPdfClient; namespace EvoHtmlToPdfDemo.Controllers.HTML_to_PDF.Headers_and_Footers { public class Header_Footer_In_External_PDFController : Controller { private readonly Microsoft.AspNetCore.Hosting.IWebHostEnvironment m_hostingEnvironment; public Header_Footer_In_External_PDFController(IWebHostEnvironment hostingEnvironment) { m_hostingEnvironment = hostingEnvironment; } [HttpPost] public ActionResult ConvertHtmlToPdf(IFormCollection collection) { // Get the server options string serverIP = collection["textBoxServerIP"]; uint serverPort = uint.Parse(collection["textBoxServerPort"]); string servicePassword = collection["textBoxServicePassword"]; bool useServicePassword = servicePassword.Length > 0; bool useTcpService = collection["ServerType"] == "radioButtonUseTcpService"; string webServiceUrl = collection["textBoxWebServiceUrl"]; // Create the HTML to PDF converter object HtmlToPdfConverter htmlToPdfConverter = null; if (useTcpService) htmlToPdfConverter = new HtmlToPdfConverter(serverIP, serverPort); else htmlToPdfConverter = new HtmlToPdfConverter(true, webServiceUrl); // Set optional service password if (useServicePassword) htmlToPdfConverter.ServicePassword = servicePassword; // Set license key received after purchase to use the converter in licensed mode // Leave it not set to use the converter in demo mode htmlToPdfConverter.LicenseKey = "4W9+bn19bn5ue2B+bn1/YH98YHd3d3c="; // Set an adddional delay in seconds to wait for JavaScript or AJAX calls after page load completed // Set this property to 0 if you don't need to wait for such asynchcronous operations to finish htmlToPdfConverter.ConversionDelay = 2; // Set the PDF file to be inserted before conversion result string pdfFileBefore = m_hostingEnvironment.ContentRootPath + "/wwwroot" + "/DemoAppFiles/Input/PDF_Files/Merge_Before_Conversion.pdf"; htmlToPdfConverter.PdfDocumentOptions.AddStartDocument(pdfFileBefore, collection["addHeaderFooterInInsertedPdfCheckBox"].Count > 0, collection["showHeaderInFirstPageCheckBox"].Count > 0, collection["showFooterInFirstPageCheckBox"].Count > 0); // Set the PDF file to be added after conversion result string pdfFileAfter = m_hostingEnvironment.ContentRootPath + "/wwwroot" + "/DemoAppFiles/Input/PDF_Files/Merge_After_Conversion.pdf"; htmlToPdfConverter.PdfDocumentOptions.AddEndDocument(pdfFileAfter, collection["addHeaderFooterInAppendedPdfCheckBox"].Count > 0, true, true); // Enable header in the generated PDF document htmlToPdfConverter.PdfDocumentOptions.ShowHeader = true; // Draw header elements if (htmlToPdfConverter.PdfDocumentOptions.ShowHeader) DrawHeader(htmlToPdfConverter, true); // Enable footer in the generated PDF document htmlToPdfConverter.PdfDocumentOptions.ShowFooter = true; // Draw footer elements if (htmlToPdfConverter.PdfDocumentOptions.ShowFooter) DrawFooter(htmlToPdfConverter, true, true); string url = collection["urlTextBox"]; // Convert the HTML page to a PDF document and add the external PDF documents byte[] outPdfBuffer = htmlToPdfConverter.ConvertUrl(url); // Send the PDF file to browser FileResult fileResult = new FileContentResult(outPdfBuffer, "application/pdf"); fileResult.FileDownloadName = "Header_Footer_in_External_PDF.pdf"; return fileResult; } /// <summary> /// Draw the header elements /// </summary> /// <param name="htmlToPdfConverter">The HTML to PDF Converter object</param> /// <param name="drawHeaderLine">A flag indicating if a line should be drawn at the bottom of the header</param> private void DrawHeader(HtmlToPdfConverter htmlToPdfConverter, bool drawHeaderLine) { string headerHtmlString = System.IO.File.ReadAllText(m_hostingEnvironment.ContentRootPath + "/wwwroot" + "/DemoAppFiles/Input/HTML_Files/Header_HTML.html"); string headerBaseUrl = "http://www.evopdf.com/demo/DemoAppFiles/Input/HTML_Files/"; // Set the header height in points htmlToPdfConverter.PdfHeaderOptions.HeaderHeight = 60; // Set header background color htmlToPdfConverter.PdfHeaderOptions.HeaderBackColor = RgbColor.White; // Create a HTML element to be added in header HtmlToPdfElement headerHtml = new HtmlToPdfElement(headerHtmlString, headerBaseUrl); // Set the HTML element to fit the container height headerHtml.FitHeight = true; // Add HTML element to header htmlToPdfConverter.PdfHeaderOptions.AddElement(headerHtml); if (drawHeaderLine) { // Calculate the header width based on PDF page size and margins float headerWidth = htmlToPdfConverter.PdfDocumentOptions.PdfPageSize.Width - htmlToPdfConverter.PdfDocumentOptions.LeftMargin - htmlToPdfConverter.PdfDocumentOptions.RightMargin; // Calculate header height float headerHeight = htmlToPdfConverter.PdfHeaderOptions.HeaderHeight; // Create a line element for the bottom of the header LineElement headerLine = new LineElement(0, headerHeight - 1, headerWidth, headerHeight - 1); // Set line color headerLine.ForeColor = RgbColor.Gray; // Add line element to the bottom of the header htmlToPdfConverter.PdfHeaderOptions.AddElement(headerLine); } } /// <summary> /// Draw the footer elements /// </summary> /// <param name="htmlToPdfConverter">The HTML to PDF Converter object</param> /// <param name="addPageNumbers">A flag indicating if the page numbering is present in footer</param> /// <param name="drawFooterLine">A flag indicating if a line should be drawn at the top of the footer</param> private void DrawFooter(HtmlToPdfConverter htmlToPdfConverter, bool addPageNumbers, bool drawFooterLine) { string footerHtmlString = System.IO.File.ReadAllText(m_hostingEnvironment.ContentRootPath + "/wwwroot" + "/DemoAppFiles/Input/HTML_Files/Footer_HTML.html"); string baseUrl = "http://www.evopdf.com/demo/DemoAppFiles/Input/HTML_Files/"; // Set the footer height in points htmlToPdfConverter.PdfFooterOptions.FooterHeight = 60; // Set footer background color htmlToPdfConverter.PdfFooterOptions.FooterBackColor = RgbColor.WhiteSmoke; // Create a HTML element to be added in footer HtmlToPdfElement footerHtml = new HtmlToPdfElement(footerHtmlString, baseUrl); // Set the HTML element to fit the container height footerHtml.FitHeight = true; // Add HTML element to footer htmlToPdfConverter.PdfFooterOptions.AddElement(footerHtml); // Add page numbering if (addPageNumbers) { // Create a text element with page numbering place holders &p; and & P; TextElement footerText = new TextElement(0, 30, "Page &p; of &P; ", new PdfFont("Times New Roman", 10, true)); // Align the text at the right of the footer footerText.TextAlign = HorizontalTextAlign.Right; // Set page numbering text color footerText.ForeColor = RgbColor.Navy; // Embed the text element font in PDF footerText.EmbedSysFont = true; // Add the text element to footer htmlToPdfConverter.PdfFooterOptions.AddElement(footerText); } if (drawFooterLine) { // Calculate the footer width based on PDF page size and margins float footerWidth = htmlToPdfConverter.PdfDocumentOptions.PdfPageSize.Width - htmlToPdfConverter.PdfDocumentOptions.LeftMargin - htmlToPdfConverter.PdfDocumentOptions.RightMargin; // Create a line element for the top of the footer LineElement footerLine = new LineElement(0, 0, footerWidth, 0); // Set line color footerLine.ForeColor = RgbColor.Gray; // Add line element to the bottom of the footer htmlToPdfConverter.PdfFooterOptions.AddElement(footerLine); } } } }