The Document property of the HiQPdf.HtmlToPdf class having the PdfDocumentControl type is used to control most of the properties of the generated PDF document like page size, page orientation, page margins, header and footer, outlines, font embedding, the document standard and the document security.
The Header and Footer properties of the HiQPdf.PdfDocumentControl class control whether the header and footer are visible and also control their content. The PdfHeader and PdfFooter classes have a PdfHeaderLayout(PdfObject) method which can be used to layout various PDF objects in header or footer like text, images, HTML and graphics.
The PdfText objects laid out in header and footer can use two page numbering place holders: {CrtPage} which will be replaced by the current page number and the {PageCount} which will be replaced by the total number of pages in the generated PDF document.
In this demo you can see how to enable the header and footer and how to add HTML, text, images and page numbering to header and footer.
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;
using HiQPdf;
namespace HiQPdf_Demo.Controllers
{
public class PdfHeadersAndFootersController : Controller
{
IFormCollection m_formCollection;
IHostingEnvironment m_hostingEnvironment;
public PdfHeadersAndFootersController(IHostingEnvironment hostingEnvironment)
{
m_hostingEnvironment = hostingEnvironment;
}
// GET: PdfHeadersAndFooters
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult ConvertToPdf(IFormCollection collection)
{
m_formCollection = collection;
// create the HTML to PDF converter
HtmlToPdf htmlToPdfConverter = new HtmlToPdf();
// set a demo serial number
htmlToPdfConverter.SerialNumber = "YCgJMTAE-BiwJAhIB-EhlWTlBA-UEBRQFBA-U1FOUVJO-WVlZWQ==";
// set the default header and footer of the document
SetHeader(htmlToPdfConverter.Document);
SetFooter(htmlToPdfConverter.Document);
try
{
byte[] pdfBuffer = htmlToPdfConverter.ConvertUrlToMemory(collection["textBoxUrl"]);
FileResult fileResult = new FileContentResult(pdfBuffer, "application/pdf");
fileResult.FileDownloadName = "PdfHeadersAndFooters.pdf";
return fileResult;
}
finally
{
}
}
private void SetHeader(PdfDocumentControl htmlToPdfDocument)
{
// enable header display
htmlToPdfDocument.Header.Enabled = true;
// set header height
htmlToPdfDocument.Header.Height = 50;
float pdfPageWidth = htmlToPdfDocument.PageOrientation == PdfPageOrientation.Portrait ?
htmlToPdfDocument.PageSize.Width : htmlToPdfDocument.PageSize.Height;
float headerWidth = pdfPageWidth - htmlToPdfDocument.Margins.Left - htmlToPdfDocument.Margins.Right;
float headerHeight = htmlToPdfDocument.Header.Height;
// set header background color
htmlToPdfDocument.Header.BackgroundColor = System.Drawing.Color.WhiteSmoke;
string headerImageFile = m_hostingEnvironment.ContentRootPath + @"\wwwroot" + @"\DemoFiles\Images\HiQPdfLogo.png";
PdfImage logoHeaderImage = new PdfImage(5, 5, 40, System.Drawing.Image.FromFile(headerImageFile));
htmlToPdfDocument.Header.Layout(logoHeaderImage);
// layout HTML in header
PdfHtml headerHtml = new PdfHtml(50, 5, @"<span style=""color:Navy; font-family:Times New Roman; font-style:italic"">
Quickly Create High Quality PDFs with </span><a href=""http://www.hiqpdf.com"">HiQPdf</a>", null);
headerHtml.FitDestHeight = true;
htmlToPdfDocument.Header.Layout(headerHtml);
// create a border for header
PdfRectangle borderRectangle = new PdfRectangle(1, 1, headerWidth - 2, headerHeight - 2);
borderRectangle.LineStyle.LineWidth = 0.5f;
borderRectangle.ForeColor = System.Drawing.Color.Navy;
htmlToPdfDocument.Header.Layout(borderRectangle);
}
private void SetFooter(PdfDocumentControl htmlToPdfDocument)
{
// enable footer display
htmlToPdfDocument.Footer.Enabled = true;
// set footer height
htmlToPdfDocument.Footer.Height = 50;
// set footer background color
htmlToPdfDocument.Footer.BackgroundColor = System.Drawing.Color.WhiteSmoke;
float pdfPageWidth = htmlToPdfDocument.PageOrientation == PdfPageOrientation.Portrait ?
htmlToPdfDocument.PageSize.Width : htmlToPdfDocument.PageSize.Height;
float footerWidth = pdfPageWidth - htmlToPdfDocument.Margins.Left - htmlToPdfDocument.Margins.Right;
float footerHeight = htmlToPdfDocument.Footer.Height;
// layout HTML in footer
PdfHtml footerHtml = new PdfHtml(5, 5, @"<span style=""color:Navy; font-family:Times New Roman; font-style:italic"">
Quickly Create High Quality PDFs with </span><a href=""http://www.hiqpdf.com"">HiQPdf</a>", null);
footerHtml.FitDestHeight = true;
htmlToPdfDocument.Footer.Layout(footerHtml);
if (m_formCollection["checkBoxDisplayPageNumbersInFooter"].Count > 0)
{
if (m_formCollection["checkBoxPageNumbersInHtml"].Count == 0)
{
// add page numbering in a text element
System.Drawing.Font pageNumberingFont = new System.Drawing.Font(new System.Drawing.FontFamily("Times New Roman"),
8, System.Drawing.GraphicsUnit.Point);
PdfText pageNumberingText = new PdfText(5, footerHeight - 12, "Page {CrtPage} of {PageCount}", pageNumberingFont);
pageNumberingText.HorizontalAlign = PdfTextHAlign.Center;
pageNumberingText.EmbedSystemFont = true;
pageNumberingText.ForeColor = System.Drawing.Color.DarkGreen;
htmlToPdfDocument.Footer.Layout(pageNumberingText);
}
else
{
// add page numbers in HTML - more flexible but less efficient than text version
PdfHtmlWithPlaceHolders htmlWithPageNumbers = new PdfHtmlWithPlaceHolders(5, footerHeight - 20,
"Page <span style=\"font-size: 16px; color: blue; font-style: italic; font-weight: bold\">{CrtPage}</span> of <span style=\"font-size: 16px; color: green; font-weight: bold\">{PageCount}</span>", null);
htmlToPdfDocument.Footer.Layout(htmlWithPageNumbers);
}
}
string footerImageFile = m_hostingEnvironment.ContentRootPath + @"\wwwroot" + @"\DemoFiles\Images\HiQPdfLogo.png";
PdfImage logoFooterImage = new PdfImage(footerWidth - 40 - 5, 5, 40, System.Drawing.Image.FromFile(footerImageFile));
htmlToPdfDocument.Footer.Layout(logoFooterImage);
// create a border for footer
PdfRectangle borderRectangle = new PdfRectangle(1, 1, footerWidth - 2, footerHeight - 2);
borderRectangle.LineStyle.LineWidth = 0.5f;
borderRectangle.ForeColor = System.Drawing.Color.DarkGreen;
htmlToPdfDocument.Footer.Layout(borderRectangle);
}
}
}
By default the header and footer is the same in all the PDF pages of a document but it is possible to override the header and footer in some or all the PDF pages with another header or footer.
The HiQPdf HTML to PDF Converter offers a great flexibility in setting the PDF document headers and footers. Per PDF page customization of header and footer can be done in HtmlToPdfPageCreatingEvent event handler. Basically you can add anything in the header and footer from plain text to full HTML documents, override the default document header and footer in any page with a customized header and footer, hide the header and footer in any PDF page.
In the code sample below, the default header on the second page will be replaced with a bigger header containing the rendering of a whole website like www.google.com. There also options to hide the header or footer on the first or on the second page of the generated PDF document.
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;
using HiQPdf;
namespace HiQPdf_Demo.Controllers
{
public class PdfHeadersAndFootersController : Controller
{
IFormCollection m_formCollection;
IHostingEnvironment m_hostingEnvironment;
public PdfHeadersAndFootersController(IHostingEnvironment hostingEnvironment)
{
m_hostingEnvironment = hostingEnvironment;
}
// GET: PdfHeadersAndFooters
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult ConvertToPdf(IFormCollection collection)
{
m_formCollection = collection;
// create the HTML to PDF converter
HtmlToPdf htmlToPdfConverter = new HtmlToPdf();
// set a demo serial number
htmlToPdfConverter.SerialNumber = "YCgJMTAE-BiwJAhIB-EhlWTlBA-UEBRQFBA-U1FOUVJO-WVlZWQ==";
// set the default header and footer of the document
SetHeader(htmlToPdfConverter.Document);
SetFooter(htmlToPdfConverter.Document);
// set a handler for PageCreatingEvent where to configure the PDF document pages
htmlToPdfConverter.PageCreatingEvent += new PdfPageCreatingDelegate(htmlToPdfConverter_PageCreatingEvent);
try
{
byte[] pdfBuffer = htmlToPdfConverter.ConvertUrlToMemory(collection["textBoxUrl"]);
FileResult fileResult = new FileContentResult(pdfBuffer, "application/pdf");
fileResult.FileDownloadName = "PdfHeadersAndFooters.pdf";
return fileResult;
}
finally
{
// dettach from PageCreatingEvent event
htmlToPdfConverter.PageCreatingEvent -= new PdfPageCreatingDelegate(htmlToPdfConverter_PageCreatingEvent);
}
}
void htmlToPdfConverter_PageCreatingEvent(PdfPageCreatingParams eventParams)
{
PdfPage pdfPage = eventParams.PdfPage;
int pdfPageNumber = eventParams.PdfPageNumber;
if (pdfPageNumber == 1)
{
// set the header and footer visibility in first page
pdfPage.DisplayHeader = m_formCollection["checkBoxDisplayHeaderInFirstPage"].Count > 0;
pdfPage.DisplayFooter = m_formCollection["checkBoxDisplayFooterInFirstPage"].Count > 0;
}
else if (pdfPageNumber == 2)
{
// set the header and footer visibility in second page
pdfPage.DisplayHeader = m_formCollection["checkBoxDisplayHeaderInSecondPage"].Count > 0;
pdfPage.DisplayFooter = m_formCollection["checkBoxDisplayFooterInSecondPage"].Count > 0;
if (pdfPage.DisplayHeader && m_formCollection["checkBoxCustomizedHeaderInSecondPage"].Count > 0)
{
// override the default document header in this page
// with a customized header of 200 points in height
pdfPage.CreateHeaderCanvas(200);
// layout a HTML document in header
PdfHtml htmlInHeader = new PdfHtml("http://www.hiqpdf.com");
htmlInHeader.FitDestHeight = true;
pdfPage.Header.Layout(htmlInHeader);
// create a border for the customized header
PdfRectangle borderRectangle = new PdfRectangle(0, 0, pdfPage.Header.Width - 1, pdfPage.Header.Height - 1);
borderRectangle.LineStyle.LineWidth = 0.5f;
borderRectangle.ForeColor = System.Drawing.Color.Navy;
pdfPage.Header.Layout(borderRectangle);
}
}
}
private void SetHeader(PdfDocumentControl htmlToPdfDocument)
{
// enable header display
htmlToPdfDocument.Header.Enabled = true;
// set header height
htmlToPdfDocument.Header.Height = 50;
float pdfPageWidth = htmlToPdfDocument.PageOrientation == PdfPageOrientation.Portrait ?
htmlToPdfDocument.PageSize.Width : htmlToPdfDocument.PageSize.Height;
float headerWidth = pdfPageWidth - htmlToPdfDocument.Margins.Left - htmlToPdfDocument.Margins.Right;
float headerHeight = htmlToPdfDocument.Header.Height;
// set header background color
htmlToPdfDocument.Header.BackgroundColor = System.Drawing.Color.WhiteSmoke;
string headerImageFile = m_hostingEnvironment.ContentRootPath + @"\wwwroot" + @"\DemoFiles\Images\HiQPdfLogo.png";
PdfImage logoHeaderImage = new PdfImage(5, 5, 40, System.Drawing.Image.FromFile(headerImageFile));
htmlToPdfDocument.Header.Layout(logoHeaderImage);
// layout HTML in header
PdfHtml headerHtml = new PdfHtml(50, 5, @"<span style=""color:Navy; font-family:Times New Roman; font-style:italic"">
Quickly Create High Quality PDFs with </span><a href=""http://www.hiqpdf.com"">HiQPdf</a>", null);
headerHtml.FitDestHeight = true;
htmlToPdfDocument.Header.Layout(headerHtml);
// create a border for header
PdfRectangle borderRectangle = new PdfRectangle(1, 1, headerWidth - 2, headerHeight - 2);
borderRectangle.LineStyle.LineWidth = 0.5f;
borderRectangle.ForeColor = System.Drawing.Color.Navy;
htmlToPdfDocument.Header.Layout(borderRectangle);
}
private void SetFooter(PdfDocumentControl htmlToPdfDocument)
{
// enable footer display
htmlToPdfDocument.Footer.Enabled = true;
// set footer height
htmlToPdfDocument.Footer.Height = 50;
// set footer background color
htmlToPdfDocument.Footer.BackgroundColor = System.Drawing.Color.WhiteSmoke;
float pdfPageWidth = htmlToPdfDocument.PageOrientation == PdfPageOrientation.Portrait ?
htmlToPdfDocument.PageSize.Width : htmlToPdfDocument.PageSize.Height;
float footerWidth = pdfPageWidth - htmlToPdfDocument.Margins.Left - htmlToPdfDocument.Margins.Right;
float footerHeight = htmlToPdfDocument.Footer.Height;
// layout HTML in footer
PdfHtml footerHtml = new PdfHtml(5, 5, @"<span style=""color:Navy; font-family:Times New Roman; font-style:italic"">
Quickly Create High Quality PDFs with </span><a href=""http://www.hiqpdf.com"">HiQPdf</a>", null);
footerHtml.FitDestHeight = true;
htmlToPdfDocument.Footer.Layout(footerHtml);
if (m_formCollection["checkBoxDisplayPageNumbersInFooter"].Count > 0)
{
if (m_formCollection["checkBoxPageNumbersInHtml"].Count == 0)
{
// add page numbering in a text element
System.Drawing.Font pageNumberingFont = new System.Drawing.Font(new System.Drawing.FontFamily("Times New Roman"),
8, System.Drawing.GraphicsUnit.Point);
PdfText pageNumberingText = new PdfText(5, footerHeight - 12, "Page {CrtPage} of {PageCount}", pageNumberingFont);
pageNumberingText.HorizontalAlign = PdfTextHAlign.Center;
pageNumberingText.EmbedSystemFont = true;
pageNumberingText.ForeColor = System.Drawing.Color.DarkGreen;
htmlToPdfDocument.Footer.Layout(pageNumberingText);
}
else
{
// add page numbers in HTML - more flexible but less efficient than text version
PdfHtmlWithPlaceHolders htmlWithPageNumbers = new PdfHtmlWithPlaceHolders(5, footerHeight - 20,
"Page <span style=\"font-size: 16px; color: blue; font-style: italic; font-weight: bold\">{CrtPage}</span> of <span style=\"font-size: 16px; color: green; font-weight: bold\">{PageCount}</span>", null);
htmlToPdfDocument.Footer.Layout(htmlWithPageNumbers);
}
}
string footerImageFile = m_hostingEnvironment.ContentRootPath + @"\wwwroot" + @"\DemoFiles\Images\HiQPdfLogo.png";
PdfImage logoFooterImage = new PdfImage(footerWidth - 40 - 5, 5, 40, System.Drawing.Image.FromFile(footerImageFile));
htmlToPdfDocument.Footer.Layout(logoFooterImage);
// create a border for footer
PdfRectangle borderRectangle = new PdfRectangle(1, 1, footerWidth - 2, footerHeight - 2);
borderRectangle.LineStyle.LineWidth = 0.5f;
borderRectangle.ForeColor = System.Drawing.Color.DarkGreen;
htmlToPdfDocument.Footer.Layout(borderRectangle);
}
}
}