EVO HTML to PDF Converter offers a great flexibility in adding headers and footers to the generated PDF document. You can even have headers and footers when you to add multiple HTML documents into a single PDF document.
Code Sample - Merge Multiple HTML Pages into a Single PDF
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_on_Merged_HTMLController : Controller { IFormCollection formCollection; private readonly Microsoft.AspNetCore.Hosting.IWebHostEnvironment m_hostingEnvironment; public Header_Footer_on_Merged_HTMLController(IWebHostEnvironment hostingEnvironment) { m_hostingEnvironment = hostingEnvironment; } [HttpPost] public ActionResult ConvertHtmlToPdf(IFormCollection collection) { formCollection = 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 a PDF document Document pdfDocument = null; if (useTcpService) pdfDocument = new Document(serverIP, serverPort); else pdfDocument = new Document(true, webServiceUrl); // Set optional service password if (useServicePassword) pdfDocument.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 pdfDocument.LicenseKey = "4W9+bn19bn5ue2B+bn1/YH98YHd3d3c="; // Add a page to PDF document PdfPage firstPdfPage = pdfDocument.AddPage(); // Create Header if (collection["addHeaderCheckBox"].Count > 0) CreateHeader(pdfDocument, collection["drawHeaderLineCheckBox"].Count > 0); // Create Footer if (collection["addFooterCheckBox"].Count > 0) CreateFooter(pdfDocument, collection["drawFooterLineCheckBox"].Count > 0, collection["addPageNumbersInFooterCheckBox"].Count > 0); // Add First HTML // Create the first HTML to PDF element HtmlToPdfElement firstHtml = new HtmlToPdfElement(0, 0, collection["firstUrlTextBox"]); // Optionally set a delay before conversion to allow asynchonous scripts to finish firstHtml.ConversionDelay = 2; // Optionally add a space between header and the content generated by this HTML to PDF element // The spacing for first page and the subsequent pages can be set independently // Leave this option not set for no spacing firstHtml.Y = float.Parse(collection["firstPageSpacingTextBox"]); firstHtml.TopSpacing = float.Parse(collection["headerSpacingTextBox"]); // Optionally add a space between footer and the content generated by this HTML to PDF element // Leave this option not set for no spacing firstHtml.BottomSpacing = float.Parse(collection["footerSpacingTextBox"]); // Set the header visibility in first, odd and even pages if (collection["addHeaderCheckBox"].Count > 0) { firstPdfPage.ShowHeader = collection["showHeaderInFirstPageCheckBox"].Count > 0; firstHtml.ShowHeaderInEvenPages = collection["showHeaderInEvenPagesCheckBox"].Count > 0; firstHtml.ShowHeaderInOddPages = collection["showHeaderInOddPagesCheckBox"].Count > 0; } // Set the footer visibility in first, odd and even pages if (collection["addFooterCheckBox"].Count > 0) { firstPdfPage.ShowFooter = collection["showFooterInFirstPageCheckBox"].Count > 0; firstHtml.ShowFooterInEvenPages = collection["showFooterInEvenPagesCheckBox"].Count > 0; firstHtml.ShowFooterInOddPages = collection["showFooterInOddPagesCheckBox"].Count > 0; } // Add the first HTML to PDF element to PDF document firstPdfPage.AddElement(firstHtml); // Add Second HTML // Create the second HTML to PDF element HtmlToPdfElement secondHtml = new HtmlToPdfElement(0, 0, collection["secondUrlTextBox"]); // Optionally set a delay before conversion to allow asynchonous scripts to finish secondHtml.ConversionDelay = 2; // Optionally add a space between header and the content generated by this HTML to PDF element // Leave this option not set for no spacing secondHtml.TopSpacing = float.Parse(collection["headerSpacingTextBox"]); // Optionally add a space between footer and the content generated by this HTML to PDF element // Leave this option not set for no spacing secondHtml.BottomSpacing = float.Parse(collection["footerSpacingTextBox"]); // Set the header visibility in odd and even pages if (collection["addHeaderCheckBox"].Count > 0) { secondHtml.ShowHeaderInEvenPages = collection["showHeaderInEvenPagesCheckBox"].Count > 0; secondHtml.ShowHeaderInOddPages = collection["showHeaderInOddPagesCheckBox"].Count > 0; } // Set the footer visibility in odd and even pages if (collection["addFooterCheckBox"].Count > 0) { secondHtml.ShowFooterInEvenPages = collection["showFooterInEvenPagesCheckBox"].Count > 0; secondHtml.ShowFooterInOddPages = collection["showFooterInOddPagesCheckBox"].Count > 0; } if (collection["startNewPageCheckBox"].Count > 0) { // Create a PDF page where to add the second HTML PdfPage secondPdfPage = pdfDocument.AddPage(); // Add the second HTML to PDF element to PDF document at the beginnig of the new PDF page secondPdfPage.AddElement(secondHtml); } else { // Add the second HTML to PDF element to PDF document write after the last added element pdfDocument.AddElement(secondHtml); } // Save the PDF document in a memory buffer byte[] outPdfBuffer = pdfDocument.Save(); // Send the PDF file to browser FileResult fileResult = new FileContentResult(outPdfBuffer, "application/pdf"); fileResult.FileDownloadName = "Merge_Multipe_HTML.pdf"; return fileResult; } /// <summary> /// Creates the document header /// </summary> /// <param name="pdfDocument">The PDF document object</param> /// <param name="drawHeaderLine">A flag indicating if a line should be drawn at the bottom of the header</param> private void CreateHeader(Document pdfDocument, 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/"; // Add a header to document having 60 points in height pdfDocument.AddHeaderTemplate(60); // 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 pdfDocument.Header.AddElement(headerHtml); if (drawHeaderLine) { PdfPage firstPage = pdfDocument.GetPage(0); float headerWidth = firstPage.PageSize.Width - firstPage.Margins.Left - firstPage.Margins.Right; float headerHeight = pdfDocument.Header.Height; // 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 pdfDocument.Header.AddElement(headerLine); } } /// <summary> /// Creates the document footer /// </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 CreateFooter(Document pdfDocument, bool addPageNumbers, bool drawFooterLine) { string footerHtmlString = System.IO.File.ReadAllText(m_hostingEnvironment.ContentRootPath + "/wwwroot" + "/DemoAppFiles/Input/HTML_Files/Footer_HTML.html"); string footerBaseUrl = "http://www.evopdf.com/demo/DemoAppFiles/Input/HTML_Files/"; // Add a footer to document having 60 points in height pdfDocument.AddFooterTemplate(60); // Create a HTML element to be added in footer HtmlToPdfElement footerHtml = new HtmlToPdfElement(footerHtmlString, footerBaseUrl); // Set the HTML element to fit the container height footerHtml.FitHeight = true; // Add HTML element to footer pdfDocument.Footer.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 pdfDocument.Footer.AddElement(footerText); } if (drawFooterLine) { // Calculate the footer width based on PDF page size and margins PdfPage firstPage = pdfDocument.GetPage(0); float footerWidth = firstPage.PageSize.Width - firstPage.Margins.Left - firstPage.Margins.Right; // 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 pdfDocument.Footer.AddElement(footerLine); } } } }