EVO HTML to PDF Converter

Add HTML to PDF Elements to a PDF Document

EVO PDF Client for .NET Documentation

EVO HTML to PDF Converter allows you to convert a HTML page to PDF by adding a EvoPdf.HtmlToPdfClientHtmlToPdfElement object at a given location in a PDF document. You can add multiple HTML to PDF Elements in the same PDF document.

A HtmlToPdfElement allows you to set most of the HTML to PDF conversion options available when converting HTML to PDF using the HtmlToPdfConverter class. The most important options controlling the HTML to PDF conversion using a HtmlToPdfElement are enumerated below, grouped in a few categories.

HTML Viewer Options

  • HTML Viewer Width. This option is the equivalent in converter of the browser window width and represents the preferred width in which to render the HTML content. The property you can set in your code to control the browser window width is HtmlToPdfElementHtmlViewerWidth. When the browser window width is changed the HTML content displayed inside the window can have a different layout and something similar happens when you change the HTML Viewer width of the converter. The actual HTML content width can be larger in case the HTML page cannot be entirely displayed in the given viewer width. The HTML content can be further scaled to fit the PDF page based on the converter scaling options set in your application. The HTML content size is expressed in pixels and the PDF page size is in expressed in points, 1 point being 1/72 inches. The converter is using a 96 DPI resolution to transform pixels to points with the following formula: Points = Pixels/96 * 72

  • HTML Viewer Height. This option is the equivalent in converter of the browser window height and can be used to limit the conversion to the top part of the HTML page. If this property is not set the entire page will be converted. The property you can set in your code to control the browser window height is HtmlToPdfElementHtmlViewerHeight

  • Clip HTML Content Width. You can set this option to force the HTML content width to be exactly HtmlViewerWidth pixels. If this option is false then actual HTML content width can be larger than HtmlViewerWidth pixels in case the HTML page cannot be entirely displayed in the given viewer width. By default this option is false and the HTML content is not clipped. The property you can set in your code for this option is HtmlToPdfElementClipHtmlView

HTML Content Options

Code Sample - Add HTML to PDF Elements to a PDF Document

C#
protected void convertToPdf()
{
    // Get the server IP and port
    String serverIP = textBoxServerIP.Text;
    uint serverPort = uint.Parse(textBoxServerPort.Text);

    // Create a PDF document
    Document pdfDocument = null;
    if (radioButtonUseTcpService.Checked)
        pdfDocument = new Document(serverIP, serverPort);
    else
        pdfDocument = new Document(true, textBoxWebServiceUrl.Text);

    // Set optional service password
    if (textBoxServicePassword.Text.Length > 0)
        pdfDocument.ServicePassword = textBoxServicePassword.Text;

    // Set license key received after purchase to use the converter in licensed mode
    // Leave it not set to use the converter in demo mode
    pdfDocument.LicenseKey = "4W9+bn19bn5ue2B+bn1/YH98YHd3d3c=";

    // Create a PDF page where to add the first HTML
    PdfPage firstPdfPage = pdfDocument.AddPage();

    // The element location in PDF
    float xLocation = float.Parse(xLocationTextBox.Text);
    float yLocation = float.Parse(yLocationTextBox.Text);

    // The URL of the HTML page to convert to PDF
    string urlToConvert = urlTextBox.Text;

    // Create the HTML to PDF element
    HtmlToPdfElement htmlToPdfElement = new HtmlToPdfElement(xLocation, yLocation, urlToConvert);

    // Optionally set the HTML viewer width
    htmlToPdfElement.HtmlViewerWidth = int.Parse(htmlViewerWidthTextBox.Text);

    // Optionally set the HTML viewer height
    if (htmlViewerHeightTextBox.Text.Length > 0)
        htmlToPdfElement.HtmlViewerHeight = int.Parse(htmlViewerHeightTextBox.Text);

    // Optionally set the HTML content clipping option to force the HTML content width to be exactly HtmlViewerWidth pixels
    htmlToPdfElement.ClipHtmlView = clipContentCheckBox.Checked;

    // Optionally set the destination width in PDF
    if (contentWidthTextBox.Text.Length > 0)
        htmlToPdfElement.Width = float.Parse(contentWidthTextBox.Text);

    // Optionally set the destination height in PDF
    if (contentHeightTextBox.Text.Length > 0)
        htmlToPdfElement.Height = float.Parse(contentHeightTextBox.Text);

    // Optionally set a delay before conversion to allow asynchonous scripts to finish
    htmlToPdfElement.ConversionDelay = 2;

    // Add the HTML to PDF element to PDF document
    // The AddElementResult contains the bounds of the HTML to PDF Element in last rendered PDF page
    // such that you can start a new PDF element right under it
    firstPdfPage.AddElement(htmlToPdfElement);

    // Save the PDF document in a memory buffer
    byte[] outPdfBuffer = pdfDocument.Save();
}
See Also

Other Resources