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

        // 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=";

        // Add a page to PDF document
        PdfPage firstPdfPage = pdfDocument.AddPage();

        // Create Header
        if (addHeaderCheckBox.Checked)
            CreateHeader(pdfDocument, drawHeaderLineCheckBox.Checked);

        // Create Footer
        if (addFooterCheckBox.Checked)
            CreateFooter(pdfDocument, drawFooterLineCheckBox.Checked, addPageNumbersInFooterCheckBox.Checked);

        Cursor = Cursors.WaitCursor;
        string outPdfFile = @"DemoAppFiles\Output\HTML_to_PDF\Header_Footer_in_Merge_Multipe_HTML.pdf";
        try
        {
            // Add First HTML

            // Create the first HTML to PDF element
            HtmlToPdfElement firstHtml = new HtmlToPdfElement(0, 0, firstUrlTextBox.Text);

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

            // Optionally add a space between header and the content generated by this HTML to PDF element
            // The spacing for first page and the subsequent pages can be set independently
            // Leave this option not set for no spacing
            firstHtml.Y = float.Parse(firstPageSpacingTextBox.Text);
            firstHtml.TopSpacing = float.Parse(headerSpacingTextBox.Text);

            // Optionally add a space between footer and the content generated by this HTML to PDF element
            // Leave this option not set for no spacing
            firstHtml.BottomSpacing = float.Parse(footerSpacingTextBox.Text);

            // Install a handler where to set header visibility in the pages where the HTML element is rendered
            firstHtml.PrepareRenderPdfPageEvent += new PrepareRenderPdfPageDelegate(htmlToPdfElement_PrepareRenderPdfPageEvent);

            // Add the first HTML to PDF element to PDF document
            // The PrepareRenderPdfPageEvent event handler will be invoked for each rendered PDF page
            AddElementResult firstAddResult = firstPdfPage.AddElement(firstHtml);

            // Uninstall the handler
            firstHtml.PrepareRenderPdfPageEvent -= new PrepareRenderPdfPageDelegate(htmlToPdfElement_PrepareRenderPdfPageEvent);

            // Add Second HTML

            PdfPage secondPdfPage = null;
            PointF secondHtmlLocation = Point.Empty;

            if (startNewPageCheckBox.Checked)
            {
                // Create a PDF page where to add the second HTML
                secondPdfPage = pdfDocument.AddPage();
                secondHtmlLocation = PointF.Empty;
            }
            else
            {
                // Add the second HTML on the PDF page where the first HTML ended
                secondPdfPage = pdfDocument.Pages[firstAddResult.EndPageIndex];
                secondHtmlLocation = new PointF(firstAddResult.EndPageBounds.Left, firstAddResult.EndPageBounds.Bottom);
            }

            // Create the second HTML to PDF element
            HtmlToPdfElement secondHtml = new HtmlToPdfElement(secondHtmlLocation.X, secondHtmlLocation.Y, secondUrlTextBox.Text);

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

            // Optionally add a space between header and the content generated by this HTML to PDF element
            // Leave this option not set for no spacing
            secondHtml.TopSpacing = float.Parse(headerSpacingTextBox.Text);

            // Optionally add a space between footer and the content generated by this HTML to PDF element
            // Leave this option not set for no spacing
            secondHtml.BottomSpacing = float.Parse(footerSpacingTextBox.Text);

            // Install a handler where to set header visibility in the pages where the HTML element is rendered
            secondHtml.PrepareRenderPdfPageEvent += new PrepareRenderPdfPageDelegate(htmlToPdfElement_PrepareRenderPdfPageEvent);

            // Add the second HTML to PDF element to PDF document
            // The PrepareRenderPdfPageEvent event handler will be invoked for each rendered PDF page
            secondPdfPage.AddElement(secondHtml);

            // Uninstall the handler
            secondHtml.PrepareRenderPdfPageEvent -= new PrepareRenderPdfPageDelegate(htmlToPdfElement_PrepareRenderPdfPageEvent);

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

            // Write the memory buffer in a PDF file
            System.IO.File.WriteAllBytes(outPdfFile, outPdfBuffer);
        }
        catch (Exception ex)
        {
            // The HTML to PDF conversion failed
            MessageBox.Show(String.Format("HTML to PDF Error. {0}", ex.Message));
            return;
        }
        finally
        {
            // Close the PDF document
            pdfDocument.Close();

            Cursor = Cursors.Arrow;
        }

        // Open the created PDF document in default PDF viewer
        try
        {
            System.Diagnostics.Process.Start(outPdfFile);
        }
        catch (Exception ex)
        {
            MessageBox.Show(String.Format("Cannot open created PDF file '{0}'. {1}", outPdfFile, ex.Message));
        }
    }

    /// <summary>
    /// Creates the document header
    /// </summary>
    /// <param name="pdfDocument">The PDF document object</param>
    /// <param name="drawHeaderLine">A flag indicating if a line should be drawn at the bottom of the header</param>
    private void CreateHeader(Document pdfDocument, bool drawHeaderLine)
    {
        string headerHtmlUrl = System.IO.Path.Combine(Application.StartupPath,
                    @"DemoAppFiles\Input\HTML_Files\Header_HTML.html");

        // Add a header to document having 60 points in height
        pdfDocument.AddHeaderTemplate(60);

        // Create a HTML element to be added in header
        HtmlToPdfElement headerHtml = new HtmlToPdfElement(headerHtmlUrl);

        // Set the HTML element to fit the container height
        headerHtml.FitHeight = true;

        // Add HTML element to header
        pdfDocument.Header.AddElement(headerHtml);

        if (drawHeaderLine)
        {
            float headerWidth = pdfDocument.Header.Width;
            float headerHeight = pdfDocument.Header.Height;

            // Create a line element for the bottom of the header
            LineElement headerLine = new LineElement(0, headerHeight - 1, headerWidth, headerHeight - 1);

            // Set line color
            headerLine.ForeColor = Color.Gray;

            // Add line element to the bottom of the header
            pdfDocument.Header.AddElement(headerLine);
        }
    }

    /// <summary>
    /// Creates the document footer
    /// </summary>
    /// <param name="htmlToPdfConverter">The HTML to PDF Converter object</param>
    /// <param name="addPageNumbers">A flag indicating if the page numbering is present in footer</param>
    /// <param name="drawFooterLine">A flag indicating if a line should be drawn at the top of the footer</param>
    private void CreateFooter(Document pdfDocument, bool addPageNumbers, bool drawFooterLine)
    {
        string footerHtmlUrl = System.IO.Path.Combine(Application.StartupPath,
                    @"DemoAppFiles\Input\HTML_Files\Footer_HTML.html");

        // Add a footer to document having 60 points in height
        pdfDocument.AddFooterTemplate(60);

        // Create a HTML element to be added in footer
        HtmlToPdfElement footerHtml = new HtmlToPdfElement(footerHtmlUrl);

        // Set the HTML element to fit the container height
        footerHtml.FitHeight = true;

        // Add HTML element to footer
        pdfDocument.Footer.AddElement(footerHtml);

        // Add page numbering
        if (addPageNumbers)
        {
            // Create a text element with page numbering place holders &p; and & P;
            TextElement footerText = new TextElement(0, 30, "Page &p; of &P;  ",
                new System.Drawing.Font(new System.Drawing.FontFamily("Times New Roman"), 10, System.Drawing.GraphicsUnit.Point));

            // Align the text at the right of the footer
            footerText.TextAlign = HorizontalTextAlign.Right;

            // Set page numbering text color
            footerText.ForeColor = Color.Navy;

            // Embed the text element font in PDF
            footerText.EmbedSysFont = true;

            // Add the text element to footer
            pdfDocument.Footer.AddElement(footerText);
        }

        if (drawFooterLine)
        {
            // Calculate the footer width based on PDF page size and margins
            float footerWidth = pdfDocument.Footer.Width;

            // Create a line element for the top of the footer
            LineElement footerLine = new LineElement(0, 0, footerWidth, 0);

            // Set line color
            footerLine.ForeColor = Color.Gray;

            // Add line element to the bottom of the footer
            pdfDocument.Footer.AddElement(footerLine);
        }
    }

    /// <summary>
    /// The handler for HtmlToPdfElement.PrepareRenderPdfPageEvent event where you can set the visibility of header and footer
    /// in each page or you can add a custom header or footer in a page
    /// </summary>
    /// <param name="eventParams">The event parameter containin the PDF page to customize before rendering</param>
    void htmlToPdfElement_PrepareRenderPdfPageEvent(PrepareRenderPdfPageParams eventParams)
    {
        // Set the header visibility in first, odd and even pages
        if (addHeaderCheckBox.Checked)
        {
            if (eventParams.PageNumber == 1)
                eventParams.Page.ShowHeader = showHeaderInFirstPageCheckBox.Checked;
            else if ((eventParams.PageNumber % 2) == 0 && !showHeaderInEvenPagesCheckBox.Checked)
                eventParams.Page.ShowHeader = false;
            else if ((eventParams.PageNumber % 2) == 1 && !showHeaderInOddPagesCheckBox.Checked)
                eventParams.Page.ShowHeader = false;
        }

        // Set the footer visibility in first, odd and even pages
        if (addFooterCheckBox.Checked)
        {
            if (eventParams.PageNumber == 1)
                eventParams.Page.ShowFooter = showFooterInFirstPageCheckBox.Checked;
            else if ((eventParams.PageNumber % 2) == 0 && !showFooterInEvenPagesCheckBox.Checked)
                eventParams.Page.ShowFooter = false;
            else if ((eventParams.PageNumber % 2) == 1 && !showFooterInOddPagesCheckBox.Checked)
                eventParams.Page.ShowFooter = false;
        }
    }