HiQPdf Documentation

Set PDF Pages Background Layer

Quickly Create High Quality PDFs

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.

Set PDF Background Layer Demo

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.

Demo Source Code

private void buttonCreatePdf_Click(object sender, EventArgs e)
    // create the HTML to PDF converter
    HtmlToPdf htmlToPdfConverter = new HtmlToPdf();

    // 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(textBoxLeftMargin.Text), int.Parse(textBoxRightMargin.Text),
                                int.Parse(textBoxTopMargin.Text), int.Parse(textBoxBottomMargin.Text));

    Cursor = Cursors.WaitCursor;

    string pdfFile = Application.StartupPath + @"\DemoOutput\SetPdfBackground.pdf";
        htmlToPdfConverter.ConvertUrlToFile(textBoxUrl.Text, pdfFile);
    catch (Exception ex)
        MessageBox.Show(String.Format("Conversion failed. {0}", ex.Message));
        Cursor = Cursors.Arrow;
        // dettach from PageLayoutingEvent event
        htmlToPdfConverter.PageLayoutingEvent -= new PdfPageLayoutingDelegate(htmlToPdfConverter_PageLayoutingEvent);

    // open the PDF document
    catch (Exception ex)
        MessageBox.Show(String.Format("Conversion succeeded but cannot open '{0}'. {1}", pdfFile, ex.Message));

/// <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 = panelColorSample.BackColor;

    // draw a 2 points orange line under the rendered content in page
    PointF leftBottom = new PointF(eventParams.LayoutingBounds.Left, eventParams.LayoutingBounds.Bottom + 1);
    PointF rightBottom = new PointF(eventParams.LayoutingBounds.Right, eventParams.LayoutingBounds.Bottom +1);
    PdfLine bottomLine = new PdfLine(leftBottom, rightBottom);
    bottomLine.LineStyle.LineWidth = 2.0f;
    bottomLine.ForeColor = Color.OrangeRed;
See Also

Other Resources