HiQPdf Documentation

Add HTML Objects to PDF

HiQPdf Client for .NET Core

A HTML Object is represented by the PdfHtml class. An object of this class can be added in any position in a PDF document to render HTML content in place. Multiple objects of this type can be laid out in the same PDF page and even overlapped. If there is no background color or image defined in the HTML document then the background of the rendered content in PDF will be transparent, making visible the existing content under it.

Add HTML Objects to PDF

In this demo you can learn how to add a HTML object to a PDF document. You can construct the HTML object from an URL, a local file or from a HTML code. When you create the HTML object from a HTML code you can also set a base URL to resolve the external resources from HTML document having relative paths. You can control the PDF page size and orientation, the PDF page margins, the HTML object location, width and height in PDF, the HTML browser width and height.

Demo Source Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;

using HiQPdfClient;

namespace HiQPdf_Demo.Controllers
    public class PdfHtmlObjectsDemoController : Controller
        IFormCollection m_formCollection;

        // GET: PdfHtmlObjectsDemo
        public ActionResult Index()
            return View();

        public ActionResult CreatePdf(IFormCollection collection)
            m_formCollection = collection;

            string serverIP = collection["textBoxServerIP"];
            uint serverPort = uint.Parse(collection["textBoxServerPort"]);
            string serverPassword = collection["textBoxServerPassword"];

            // create an empty PDF document
            PdfDocument document = new PdfDocument(serverIP, serverPort);

            // use server password if necessary
            if (serverPassword.Length > 0)
                document.ServerPassword = serverPassword;

            // set a demo serial number
            document.SerialNumber = "YCgJMTAE-BiwJAhIB-EhlWTlBA-UEBRQFBA-U1FOUVJO-WVlZWQ==";

            // add a page to document
            PdfPage page1 = document.AddPage(GetSelectedPageSize(), new PdfMargins(0), GetSelectedPageOrientation());

                // create the HTML object from URL or HTML code
                PdfHtml htmlObject = null;
                if (collection["UrlOrHtmlCode"] == "radioButtonConvertUrl")
                    // create from URL
                    htmlObject = new PdfHtml(collection["textBoxUrl"]);
                    // create from HTML code
                    string htmlCode = collection["textBoxHtmlCode"];
                    string baseUrl = collection["textBoxBaseUrl"];

                    htmlObject = new PdfHtml(htmlCode, baseUrl);

                // set the HTML object start location in PDF page
                htmlObject.DestX = float.Parse(collection["textBoxDestX"]);
                htmlObject.DestY = float.Parse(collection["textBoxDestY"]);

                // set the HTML object width in PDF
                if (collection["textBoxDestWidth"][0].Length > 0)
                    htmlObject.DestWidth = float.Parse(collection["textBoxDestWidth"]);

                // set the HTML object height in PDF
                if (collection["textBoxDestHeight"][0].Length > 0)
                    htmlObject.DestHeight = float.Parse(collection["textBoxDestHeight"]);

                // optionally wait an additional time before starting the conversion
                htmlObject.WaitBeforeConvert = 2;

                // set browser width
                htmlObject.BrowserWidth = int.Parse(collection["textBoxBrowserWidth"]);

                // set browser height if specified, otherwise use the default
                if (collection["textBoxBrowserHeight"][0].Length > 0)
                    htmlObject.BrowserHeight = int.Parse(collection["textBoxBrowserHeight"]);

                // set HTML load timeout
                htmlObject.HtmlLoadedTimeout = int.Parse(collection["textBoxLoadHtmlTimeout"]);

                // layout the HTML object in PDF

                // write the PDF document to a memory buffer
                byte[] pdfBuffer = document.WriteToMemory();

                FileResult fileResult = new FileContentResult(pdfBuffer, "application/pdf");
                fileResult.FileDownloadName = "PdfHtmlObjects.pdf";

                return fileResult;

        private PdfPageSize GetSelectedPageSize()
            switch (m_formCollection["dropDownListPageSizes"])
                case "A0":
                    return PdfPageSize.A0;
                case "A1":
                    return PdfPageSize.A1;
                case "A10":
                    return PdfPageSize.A10;
                case "A2":
                    return PdfPageSize.A2;
                case "A3":
                    return PdfPageSize.A3;
                case "A4":
                    return PdfPageSize.A4;
                case "A5":
                    return PdfPageSize.A5;
                case "A6":
                    return PdfPageSize.A6;
                case "A7":
                    return PdfPageSize.A7;
                case "A8":
                    return PdfPageSize.A8;
                case "A9":
                    return PdfPageSize.A9;
                case "ArchA":
                    return PdfPageSize.ArchA;
                case "ArchB":
                    return PdfPageSize.ArchB;
                case "ArchC":
                    return PdfPageSize.ArchC;
                case "ArchD":
                    return PdfPageSize.ArchD;
                case "ArchE":
                    return PdfPageSize.ArchE;
                case "B0":
                    return PdfPageSize.B0;
                case "B1":
                    return PdfPageSize.B1;
                case "B2":
                    return PdfPageSize.B2;
                case "B3":
                    return PdfPageSize.B3;
                case "B4":
                    return PdfPageSize.B4;
                case "B5":
                    return PdfPageSize.B5;
                case "Flsa":
                    return PdfPageSize.Flsa;
                case "HalfLetter":
                    return PdfPageSize.HalfLetter;
                case "Ledger":
                    return PdfPageSize.Ledger;
                case "Legal":
                    return PdfPageSize.Legal;
                case "Letter":
                    return PdfPageSize.Letter;
                case "Letter11x17":
                    return PdfPageSize.Letter11x17;
                case "Note":
                    return PdfPageSize.Note;
                    return PdfPageSize.A4;

        private PdfPageOrientation GetSelectedPageOrientation()
            return (m_formCollection["dropDownListPageOrientations"] == "Portrait") ?
                PdfPageOrientation.Portrait : PdfPageOrientation.Landscape;
See Also

Other Resources