HiQPdf DocumentationAdd HTML Objects to PDFQuickly Create High Quality PDFs

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

protected void buttonConvertToPdf_Click(object sender, EventArgs e)
    // create an empty PDF document
    PdfDocument document = new PdfDocument();

    // add a page to document
    PdfPage page1 = document.AddPage(PdfPageSize.A4, new PdfDocumentMargins(0), PdfPageOrientation.Portrait);

    // an object to be set with HTML layout info after conversion
    PdfLayoutInfo htmlLayoutInfo = null;
        // create the HTML object from URL or HTML code
        PdfHtml htmlObject = null;
        if (radioButtonConvertUrl.Checked)
            // create from URL
            htmlObject = new PdfHtml(textBoxUrl.Text);
            // create from HTML code
            string htmlCode = textBoxHtmlCode.Text;
            string baseUrl = textBoxBaseUrl.Text;

            htmlObject = new PdfHtml(htmlCode, baseUrl);

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

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

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

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

        // set browser width
        htmlObject.BrowserWidth = int.Parse(textBoxBrowserWidth.Text);

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

        // set HTML load timeout
        htmlObject.HtmlLoadedTimeout = int.Parse(textBoxLoadHtmlTimeout.Text);

        // layout the HTML object in PDF
        htmlLayoutInfo = page1.Layout(htmlObject);

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

        // inform the browser about the binary data format
        HttpContext.Current.Response.AddHeader("Content-Type", "application/pdf");

        // let the browser know how to open the PDF document and the file name
        HttpContext.Current.Response.AddHeader("Content-Disposition", String.Format("attachment; filename=PdfHtmlObjects.pdf; size={0}",

        // write the PDF buffer to HTTP response

        // call End() method of HTTP response to stop ASP.NET page processing
See Also

Other Resources