HiQPdf Documentation

Convert URLs and HTML Code to PDF

Quickly Create High Quality PDFs

The HiQPdf Library offers all the methods you need to convert URLs and HTML code to a PDF buffer in memory, to a PDF file on disk, to a PDF stream or to a PdfDocument object that can be further modified and then saved.

Convert Methods

The Convert methods of the of the HiQPdf.HtmlToPdf class are finally called to convert HTML documents to PDF. Function of how the HTML document to be converted is given, by URL or by content, and function of where the output PDF document is written the following Convert methods are defined in the HiQPdf.HtmlToPdf class:

Methods to convert the HTML document from a given URL to PDF:

Methods to convert a HTML code to PDF:

The HTML to PDF Converter Demo sample from the HiQPdf software package exemplifies the most important settings of the HTML to PDF converter. Below there is a detailed description of each of these settings and the C# source code of the sample.

Serial Number

The SerialNumber property of the of the HiQPdf.HtmlToPdf class must be set with the purchased serial number as described in the License Purchasing section. In the sample code below is set with an evaluation serial number.

PDF Document Control

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.

PDF Page Size

The PageSize property of the HiQPdf.PdfDocumentControl class controls the generated PDF document page size. The default value of this property is A4. The final width and height of the PDF pages is also influenced by the PageOrientation property. For example, if the initial page orientation was portrait and then it is set to landscape then the width of and height sizes are swapped, the width becoming bigger than the height.

PDF Page Orientation

The PageOrientation property of the HiQPdf.PdfDocumentControl class controls the generated PDF document page orientation. The default value of this property is Portrait.

PDF Page Margins

The Margins property of the HiQPdf.PdfDocumentControl class controls the generated PDF document page margins. By default the generated document margins are all 0.

Font Embedding

The FontEmbedding property of the HiQPdf.PdfDocumentControl class controls if the true type fonts used in HTML document are embedded in the PDF document. By default the true type fonts are embedded only if it is necessary, for example when using Unicode characters in HTML document.

PDF Standard

The PdfStandard property of the HiQPdf.PdfDocumentControl class controls the PDF standard to which the generated document conforms. The possible standards are PDF without restrictions, PDF/A-1b and PDF/X-1a. By default the PDF standard without restrictions is used.

PDF Security

The Security property of the HiQPdf.PdfDocumentControl class controls the security of the generated PDF document. It is possible to password protect the generated PDF document with open and permission password, allow or forbid the document priting, content copying, content editing, forms filling, edit annotations and assembling. By default the generated PDF document does not have any security features.

Header and Footer

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.

Browser Width

The BrowserWidth property of the HiQPdf.HtmlToPdf class is a property having a very high influence against the HTML content rendering. Changing this property is basically the equivalent of resizing a web browser window when viewing a web page. By default the Browser Width is 1200 pixels and this is suitable for displaying most of the web pages.

Load HTML Timeout

The HtmlLoadedTimeout property of the HiQPdf.HtmlToPdf class controls the maximum time in seconds to wait for HTML document to be loaded. The default value is 120 seconds. An exception is thrown if the HTML document cannot be loaded in HtmlLoadedTimeout seconds.

HTML to PDF Converter Basic Features Demo

In this demo you can convert an URL, a local file or a custom HTML string to PDF. You can control the PDF page size and orientation, PDF page margins, browser width, add header and footer with page numbering, embed true type fonts in the generated PDF document.As a security option it is possible to set a password requested when the created PDF document document is opened and it is possible to disable the document printing when it is opened in a viewer.

Demo Source Code

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

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

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

    // set HTML Load timeout
    htmlToPdfConverter.HtmlLoadedTimeout = int.Parse(textBoxLoadHtmlTimeout.Text);

    // set PDF page size and orientation
    htmlToPdfConverter.Document.PageSize = GetSelectedPageSize();
    htmlToPdfConverter.Document.PageOrientation = GetSelectedPageOrientation();

    // set the PDF standard used by the document
    htmlToPdfConverter.Document.PdfStandard = checkBoxPdfA.Checked?PdfStandard.PdfA: PdfStandard.Pdf;

    // set PDF page margins
    htmlToPdfConverter.Document.Margins = new PdfMargins(5);

    // set whether to embed the true type font in PDF
    htmlToPdfConverter.Document.FontEmbedding = checkBoxFontEmbedding.Checked;

    // set triggering mode; for WaitTime mode set the wait time before convert
    switch (comboBoxTriggeringMode.SelectedItem.ToString())
        case "Auto":
            htmlToPdfConverter.TriggerMode = ConversionTriggerMode.Auto;
        case "WaitTime":
            htmlToPdfConverter.TriggerMode = ConversionTriggerMode.WaitTime;
            htmlToPdfConverter.WaitBeforeConvert = int.Parse(textBoxWaitTime.Text);
        case "Manual":
            htmlToPdfConverter.TriggerMode = ConversionTriggerMode.Manual;
            htmlToPdfConverter.TriggerMode = ConversionTriggerMode.Auto;

    // set header and footer

    // set the document security
    htmlToPdfConverter.Document.Security.OpenPassword = textBoxOpenPassword.Text;
    htmlToPdfConverter.Document.Security.AllowPrinting = checkBoxAllowPrinting.Checked;

    // set the permissions password too if an open password was set
    if (htmlToPdfConverter.Document.Security.OpenPassword != null && htmlToPdfConverter.Document.Security.OpenPassword != String.Empty)
        htmlToPdfConverter.Document.Security.PermissionsPassword = htmlToPdfConverter.Document.Security.OpenPassword + "_admin"; 

    Cursor = Cursors.WaitCursor;

    // convert HTML to PDF
    string pdfFile = null;
        if (radioButtonConvertUrl.Checked)
            // convert URL
            string url = textBoxUrl.Text;
            pdfFile = Application.StartupPath + @"\DemoOut\ConvertUrl.pdf";

            // ConvertUrlToFile() is called to convert the html document and save the resulted PDF into a file on disk
            // Alternatively, ConvertUrlToMemory() can be called to save the resulted PDF in a buffer in memory
            htmlToPdfConverter.ConvertUrlToFile(url, pdfFile);
            // convert HTML code
            string htmlCode = textBoxHtmlCode.Text;
            string baseUrl = textBoxBaseUrl.Text;
            pdfFile = Application.StartupPath + @"\DemoOut\ConvertHtml.pdf";

            htmlToPdfConverter.ConvertHtmlToFile(htmlCode, baseUrl, pdfFile);
    catch (Exception ex)
        MessageBox.Show(String.Format("Conversion failed. {0}", ex.Message));
        Cursor = Cursors.Arrow;

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

private void SetHeader(PdfDocumentControl htmlToPdfDocument)
    // enable header display
    htmlToPdfDocument.Header.Enabled = checkBoxAddHeader.Checked;

    if (!htmlToPdfDocument.Header.Enabled)

    // 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 = Color.WhiteSmoke;

    string headerImageFile = Application.StartupPath + @"\DemoFiles\Images\HiQPdfLogo.png";
    PdfImage logoHeaderImage = new PdfImage(5, 5, 40, Image.FromFile(headerImageFile));

    // 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;
    headerHtml.FontEmbedding = checkBoxFontEmbedding.Checked;

    // create a border for header

    PdfRectangle borderRectangle = new PdfRectangle(1, 1, headerWidth - 2, headerHeight - 2);
    borderRectangle.LineStyle.LineWidth = 0.5f;
    borderRectangle.ForeColor = Color.Navy;

private void SetFooter(PdfDocumentControl htmlToPdfDocument)
    // enable footer display
    htmlToPdfDocument.Footer.Enabled = checkBoxAddFooter.Checked;

    if (!htmlToPdfDocument.Footer.Enabled)

    // set footer height
    htmlToPdfDocument.Footer.Height = 50;

    // set footer background color
    htmlToPdfDocument.Footer.BackgroundColor = 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;
    footerHtml.FontEmbedding = checkBoxFontEmbedding.Checked;

    // add page numbering
    Font pageNumberingFont = new Font(new FontFamily("Times New Roman"), 8, GraphicsUnit.Point);
    PdfText pageNumberingText = new PdfText(5, footerHeight - 12, "Page {CrtPage} of {PageCount}", pageNumberingFont);
    pageNumberingText.HorizontalAlign = PdfTextHAlign.Center;
    pageNumberingText.EmbedSystemFont = true;
    pageNumberingText.ForeColor = Color.DarkGreen;

    string footerImageFile = Application.StartupPath + @"\DemoFiles\Images\HiQPdfLogo.png";
    PdfImage logoFooterImage = new PdfImage(footerWidth - 40 - 5, 5, 40, Image.FromFile(footerImageFile));

    // create a border for footer
    PdfRectangle borderRectangle = new PdfRectangle(1, 1, footerWidth - 2, footerHeight - 2);
    borderRectangle.LineStyle.LineWidth = 0.5f;
    borderRectangle.ForeColor = Color.DarkGreen;
See Also

Other Resources