Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports ColorCode.Compilation.Languages
Imports SelectPdf

Namespace SelectPdf.Samples
    Partial Public Class html_to_pdf_headers_and_footers
        Inherits System.Web.UI.Page

        Protected Sub BtnCreatePdf_Click(sender As Object, e As EventArgs)

            Dim renderingEngine As RenderingEngine = CType([Enum].Parse(GetType(RenderingEngine), DdlRenderingEngine.SelectedValue, True), RenderingEngine)

            ' get parameters
            Dim headerUrl As String = Request.Url.GetLeftPart(UriPartial.Authority) + ResolveUrl("~/files/header.html")
            Dim footerUrl As String = Request.Url.GetLeftPart(UriPartial.Authority) + ResolveUrl("~/files/footer.html")

            Dim showHeaderOnFirstPage As Boolean = ChkHeaderFirstPage.Checked
            Dim showHeaderOnOddPages As Boolean = ChkHeaderOddPages.Checked
            Dim showHeaderOnEvenPages As Boolean = ChkHeaderEvenPages.Checked

            Dim headerHeight As Integer = 50
            Try
                headerHeight = Convert.ToInt32(TxtHeaderHeight.Text)
            Catch
            End Try


            Dim showFooterOnFirstPage As Boolean = ChkFooterFirstPage.Checked
            Dim showFooterOnOddPages As Boolean = ChkFooterOddPages.Checked
            Dim showFooterOnEvenPages As Boolean = ChkFooterEvenPages.Checked

            Dim footerHeight As Integer = 50
            Try
                footerHeight = Convert.ToInt32(TxtFooterHeight.Text)
            Catch
            End Try

            ' instantiate a html to pdf converter object
            Dim converter As New HtmlToPdf()
            converter.Options.RenderingEngine = renderingEngine
            converter.Options.CefEnginePath = Server.MapPath(ConfigurationManager.AppSettings("CefEnginePath"))

            ' header settings
            converter.Options.DisplayHeader = showHeaderOnFirstPage OrElse _
                showHeaderOnOddPages OrElse showHeaderOnEvenPages
            converter.Header.DisplayOnFirstPage = showHeaderOnFirstPage
            converter.Header.DisplayOnOddPages = showHeaderOnOddPages
            converter.Header.DisplayOnEvenPages = showHeaderOnEvenPages
            converter.Header.Height = headerHeight

            Dim headerHtml As New PdfHtmlSection(headerUrl)
            headerHtml.RenderingEngine = renderingEngine
            headerHtml.CefEnginePath = Server.MapPath(ConfigurationManager.AppSettings("CefEnginePath"))

            headerHtml.AutoFitHeight = HtmlToPdfPageFitMode.AutoFit
            converter.Header.Add(headerHtml)

            ' footer settings
            converter.Options.DisplayFooter = showFooterOnFirstPage OrElse _
                showFooterOnOddPages OrElse showFooterOnEvenPages
            converter.Footer.DisplayOnFirstPage = showFooterOnFirstPage
            converter.Footer.DisplayOnOddPages = showFooterOnOddPages
            converter.Footer.DisplayOnEvenPages = showFooterOnEvenPages
            converter.Footer.Height = footerHeight

            Dim footerHtml As New PdfHtmlSection(footerUrl)
            footerHtml.RenderingEngine = renderingEngine
            footerHtml.CefEnginePath = Server.MapPath(ConfigurationManager.AppSettings("CefEnginePath"))
            footerHtml.AutoFitHeight = HtmlToPdfPageFitMode.AutoFit
            converter.Footer.Add(footerHtml)

            ' add page numbering element to the footer
            If ChkPageNumbering.Checked Then
                ' page numbers can be added using a PdfTextSection object
                Dim text As New PdfTextSection(0, 10, "Page: {page_number} of {total_pages}  ", _
                                               New System.Drawing.Font("Arial", 8))
                text.HorizontalAlign = PdfTextHorizontalAlign.Right
                converter.Footer.Add(text)
            End If

            ' create a new pdf document converting an url
            Dim doc As PdfDocument = converter.ConvertUrl(TxtUrl.Text)

            ' custom header on page 3
            If doc.Pages.Count >= 3 Then
                Dim page As PdfPage = doc.Pages(2)

                Dim customHeader As PdfTemplate = doc.AddTemplate( _
                    page.PageSize.Width, headerHeight)
                Dim customHtml As New PdfHtmlElement( _
                    "<div><b>This is the custom header that " + _
                    "will appear only on page 3!</b></div>", _
                    String.Empty)
                customHtml.RenderingEngine = renderingEngine
                customHtml.CefEnginePath = Server.MapPath(ConfigurationManager.AppSettings("CefEnginePath"))

                customHeader.Add(customHtml)

                page.CustomHeader = customHeader
            End If

            ' save pdf document
            doc.Save(Response, False, "Sample.pdf")

            ' close pdf document
            doc.Close()
        End Sub
    End Class
End Namespace
