When the HTML to PDF Converter renders the HTML it will automatically create new PDF pages and will layout the HTML content in PDF. Right before creating a new page the converter will raise the the HtmlToPdfPageCreatingEvent event and before layouting the content in the PDF page the converter will raise the HtmlToPdfPageLayoutingEvent event.
The PageLayoutingEvent event has a HiQPdfPdfPage object in its parameters and this PdfPage object can be used to layout PDF objects in page before the main HTML content is laid out. You can add text, images, HTML and graphics in the background of the page. In the demo below a colored rectangle is added in the background.
In this demo you learn how to add a custom background content to the PDF pages created by the converter in the PageLayoutingEvent event handler called right before rendering the main HTML content in PDF page. The PageLayoutingEvent event handler parameter contains the PdfPage object being rendered and the rectangle inside the page that will be rendered. The PDF objects added to the PdfPage in this event handler will be rendered in the background of the main HTML content.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using HiQPdf;
namespace HiQPdf_Demo.Controllers
{
public class SetPdfBackgroundLayerController : Controller
{
IFormCollection m_formCollection;
// GET: SetPdfBackgroundLayer
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==";
// attach to PageLayoutingEvent event raised right before layouting the HTML content in a PDF page
htmlToPdfConverter.PageLayoutingEvent += new PdfPageLayoutingDelegate(htmlToPdfConverter_PageLayoutingEvent);
// set PDF page margins
htmlToPdfConverter.Document.Margins = new PdfMargins(
int.Parse(collection["textBoxLeftMargin"]), int.Parse(collection["textBoxRightMargin"]),
int.Parse(collection["textBoxTopMargin"]), int.Parse(collection["textBoxBottomMargin"]));
try
{
byte[] pdfBuffer = htmlToPdfConverter.ConvertUrlToMemory(collection["textBoxUrl"]);
FileResult fileResult = new FileContentResult(pdfBuffer, "application/pdf");
fileResult.FileDownloadName = "SetPdfBackground.pdf";
return fileResult;
}
finally
{
// dettach from PageLayoutingEvent event
htmlToPdfConverter.PageLayoutingEvent -= new PdfPageLayoutingDelegate(htmlToPdfConverter_PageLayoutingEvent);
}
}
/// <summary>
/// The PageLayoutingEvent event handler called before each PDF page is rendered by the converter
/// </summary>
/// <param name="eventParams">The event handler parameter giving information about the PDF page being rendered
/// and the rectangle in page that will be rendered</param>
void htmlToPdfConverter_PageLayoutingEvent(PdfPageLayoutingParams eventParams)
{
// The PDF page being rendered
PdfPage crtPage = eventParams.PdfPage;
// draw a colored rectangle in the background of the PDF page
PdfRectangle backColorRect = new PdfRectangle(0, 0, crtPage.DrawableRectangle.Width, crtPage.DrawableRectangle.Height);
backColorRect.BackColor = System.Drawing.Color.FromArgb(255, int.Parse(m_formCollection["textBoxR"]), int.Parse(m_formCollection["textBoxG"]), int.Parse(m_formCollection["textBoxB"]));
crtPage.Layout(backColorRect);
// draw a 2 points orange line under the rendered content in page
System.Drawing.PointF leftBottom = new System.Drawing.PointF(eventParams.LayoutingBounds.Left, eventParams.LayoutingBounds.Bottom + 1);
System.Drawing.PointF rightBottom = new System.Drawing.PointF(eventParams.LayoutingBounds.Right, eventParams.LayoutingBounds.Bottom + 1);
PdfLine bottomLine = new PdfLine(leftBottom, rightBottom);
bottomLine.LineStyle.LineWidth = 2.0f;
bottomLine.ForeColor = System.Drawing.Color.OrangeRed;
crtPage.Layout(bottomLine);
}
}
}