HiQPdf Documentation

Control HTML Content Scaling in PDF Page

Quickly Create High Quality PDFs

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.

HTML Content Location and Size in PDF

The DestX, DestY, DestWidth, DestWidth properties of the HiQPdf.PdfDocumentControl class control the location and the size of the HTML content in PDF document. By default the HTML content is rendered in top left corner of the PDF page and uses the entire available PDF page width.

Fit and Resize Control

There are a few properties controlling how the rendered HTML is resized to fit the PDF page dimensions or how the PDF pages are automatically resized to display the HTML content.

Fit PDF Page Width

The FitPageWidth property of the HiQPdf.PdfDocumentControl class controls if the content is scaled down to fit the PDF page width. This property is true by default.

The HTML width is given by the BrowserWidth property of the HiQPdf.HtmlToPdf class which is 1200 pixels by default. At a default DPI of 96 the HTML width is 12.5 inch which is larger than the default A4 portrait page width and the HTML is scaled down to fit the PDF page which means that the text and images might appear smaller than they are in HTML.

If the FitPageWidth property is set to false the HTML is not scaled down and by default only a part of the 1200 pixels of HTML page would be displayed in a A4 portrait page. But when FitPageWidth is false another property ResizePageWidth comes into play. By default the ResizePageWidth is true and the PDF page width can be resized to display the whole HTML without scaling.

Fit PDF Page Height

The FitPageHeight property of the HiQPdf.PdfDocumentControl class controls if the content is scaled down to fit the PDF page height. This property is false by default.

Postcard Mode

If the FitPageWidth property is false, the PostCardMode property of the HiQPdf.PdfDocumentControl class controls if the content will be rendered in one PDF page resized to display the entire HTML. This property is false by default.

There are also a few properties of the HTML converters controlling the internal browser behavior. The most important are described in the sections below.

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.

Trim to Browser Width

The TrimToBrowserWidth controls if the browser window width is forced to have the width given by the BrowserWidth property. If the HTML document cannot be entirely displayed in BrowserWidth pixels and the TrimToBrowserWidth is false then the browser window width will be automatically set to display the entire HTML document. If the HTML document cannot be entirely displayed in BrowserWidth pixels and the TrimToBrowserWidth is true then the browser window width will not be automatically set to display the entire HTML document and the HTML document will be trimmed. The default value of this property is false.

HTML Fitting and Scaling Options Demo

In this demo you can see how to convert an URL, a local file or a HTML code to PDF. You can control the PDF page size and orientation, PDF page margins, browser width and height, HTML content location, HTML content fitting and scaling in PDF page.

Demo Source Code

C#
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);

    // optionally wait an additional time before starting the conversion
    // it is not necessary to set this property if the HTML page is not updated after initial load
    htmlToPdfConverter.WaitBeforeConvert = 2;

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

    // 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));

    // set HTML location and size in PDF page
    if (textBoxHtmlLeftLocation.Text.Length > 0)
        htmlToPdfConverter.Document.DestX = float.Parse(textBoxHtmlLeftLocation.Text);
    if (textBoxHtmlTopLocation.Text.Length > 0)
        htmlToPdfConverter.Document.DestY = float.Parse(textBoxHtmlTopLocation.Text);
    if (textBoxHtmlWidth.Text.Length > 0)
        htmlToPdfConverter.Document.DestWidth = float.Parse(textBoxHtmlWidth.Text);
    if (textBoxHtmlHeight.Text.Length > 0)
        htmlToPdfConverter.Document.DestHeight = float.Parse(textBoxHtmlHeight.Text);

    // control if the HTML can be scaled to fit the PDF page width
    htmlToPdfConverter.Document.FitPageWidth = checkBoxFitPageWidth.Checked;

    // control if the HTML can be enlarged to fit the PDF page width when FitPageWidth is true
    htmlToPdfConverter.Document.ForceFitPageWidth = checkBoxForceFitPageWidth.Checked;

    // control if the PDF page can be resized to display the whole HTML content when FitPageWidth is false
    htmlToPdfConverter.Document.ResizePageWidth = checkBoxResizePdfPage.Checked;

    // control if the HTML content can be scaled to fit the PDF page height
    htmlToPdfConverter.Document.FitPageHeight = checkBoxFitPageHeight.Checked;

    // control if the whole HTML content will be rendered in one PDF page without scaling
    htmlToPdfConverter.Document.PostCardMode = checkBoxPostCardMode.Checked;         

    Cursor = Cursors.WaitCursor;

    // convert HTML to PDF
    string pdfFile = null;
    try
    {
        if (radioButtonConvertUrl.Checked)
        {
            // convert URL
            string url = textBoxUrl.Text;
            pdfFile = Application.StartupPath + @"\DemoOut\HtmlFittingAndScaling.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);
        }
        else
        {
            // convert HTML code
            string htmlCode = textBoxHtmlCode.Text;
            string baseUrl = textBoxBaseUrl.Text;
            pdfFile = Application.StartupPath + @"\DemoOut\HtmlFittingAndScaling.pdf";

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

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