Adding HTML to PDF Elements to a PDF Document

EVO HTML to PDF Converter allows you to convert a HTML page to PDF by adding a HtmlToPdfElement 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.

protected void convertToPdfButton_Click(object sender, EventArgs e)
{
    // Create a PDF document
    Document pdfDocument = new Document();

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

    try
    {
        // 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
        AddElementResult result = firstPdfPage.AddElement(htmlToPdfElement);

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

        // Send the PDF as response to browser

        // Set response content type
        Response.AddHeader("Content-Type", "application/pdf");

        // Instruct the browser to open the PDF file as an attachment or inline
        Response.AddHeader("Content-Disposition", String.Format("attachment; filename=Add_HTML_to_PDF_Elements_to_PDF.pdf; size={0}", outPdfBuffer.Length.ToString()));

        // Write the PDF document buffer to HTTP response
        Response.BinaryWrite(outPdfBuffer);

        // End the HTTP response and stop the current page processing
        Response.End();
    }
    finally
    {
        // Close the PDF document
        pdfDocument.Close();
    }
}

A live demo for adding HTML to PDF Elements to a PDF document with complete C# code can be found on product website.