    protected void createPdfButton_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 PDF page to PDF document
        PdfPage pdfPage = pdfDocument.AddPage();

        HtmlToPdfElement htmlToPdfElement = null;
        try
        {
            // Add document header
            if (addHeaderCheckBox.Checked)
                AddHeader(pdfDocument, drawHeaderLineCheckBox.Checked);

            // Add document footer
            if (addFooterCheckBox.Checked)
                AddFooter(pdfDocument, addPageNumbersInFooterCheckBox.Checked, drawFooterLineCheckBox.Checked);

            // Create a HTML to PDF element to add to document
            htmlToPdfElement = new HtmlToPdfElement(0, 0, urlTextBox.Text);

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

            // Install a handler where to change the header and footer in pages generated by the HTML to PDF element
            htmlToPdfElement.PrepareRenderPdfPageEvent += new PrepareRenderPdfPageDelegate(htmlToPdfElement_PrepareRenderPdfPageEvent);

            // Optionally add a space between header and the page body
            // Leave this option not set for no spacing
            htmlToPdfElement.Y = float.Parse(firstPageSpacingTextBox.Text);
            htmlToPdfElement.TopSpacing = float.Parse(headerSpacingTextBox.Text);

            // Optionally add a space between footer and the page body
            // Leave this option not set for no spacing
            htmlToPdfElement.BottomSpacing = float.Parse(footerSpacingTextBox.Text);

            // Add the HTML to PDF element to document
            // This will raise the PrepareRenderPdfPageEvent event where the header and footer visibilit per page can be changed
            pdfPage.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=HTML_in_Header_Footer.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
        {
            // uninstall handler
            htmlToPdfElement.PrepareRenderPdfPageEvent -= new PrepareRenderPdfPageDelegate(htmlToPdfElement_PrepareRenderPdfPageEvent);

            // Close the PDF document
            pdfDocument.Close();
        }
    }

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

    /// <summary>
    /// Add a header to document
    /// </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 AddHeader(Document pdfDocument, bool drawHeaderLine)
    {
        string headerHtmlUrl = Server.MapPath("~/DemoAppFiles/Input/HTML_Files/Header_HTML.html");

        // Create the document footer template
        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>
    /// Add a footer to document
    /// </summary>
    /// <param name="pdfDocument">The PDF document 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 AddFooter(Document pdfDocument, bool addPageNumbers, bool drawFooterLine)
    {
        string footerHtmlUrl = Server.MapPath("~/DemoAppFiles/Input/HTML_Files/Footer_HTML.html");

        // Create the document footer template
        pdfDocument.AddFooterTemplate(60);

        // Set footer background color
        RectangleElement backColorRectangle = new RectangleElement(0, 0, pdfDocument.Footer.Width, pdfDocument.Footer.Height);
        backColorRectangle.BackColor = Color.WhiteSmoke;
        pdfDocument.Footer.AddElement(backColorRectangle);

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