SelectPdf for .NET - Pdf Headers and Footers with Html to Pdf Converter - VB.NET / ASP.NET MVC Sample

This sample shows how to convert an url to pdf using SelectPdf Pdf Library for .NET and how to set html headers and footers for the generated pdf document.

This sample will also show how to add page numbers in the footer of the generated pdf document and how to modify the document header to be different for a specific page.

Url:

Header:
Show on First Page
Show on Odd Numbered Pages
Show on Even Numbered Pages
Height:  px
Sample header

Footer:
Show on First Page
Show on Odd Numbered Pages
Show on Even Numbered Pages
Height:  px
Add page numbers
Sample footer


Sample Code VB.NET



Imports SelectPdf

Namespace Controllers
    Public Class HtmlToPdfHeadersAndFootersController
        Inherits Controller

        ' GET: HtmlToPdfHeadersAndFooters
        Public Function Index() As ActionResult
            Return View()
        End Function

        <HttpPost>
        Public Function SubmitAction(fields As FormCollection) As ActionResult
            ' get parameters
            Dim headerUrl As String = (New Uri(Request.Url, VirtualPathUtility.
                                               ToAbsolute("~/files/header.html"))).AbsoluteUri
            Dim footerUrl As String = (New Uri(Request.Url, VirtualPathUtility.
                                               ToAbsolute("~/files/footer.html"))).AbsoluteUri

            Dim showHeaderOnFirstPage As Boolean = fields("ChkHeaderFirstPage") = "on"
            Dim showHeaderOnOddPages As Boolean = fields("ChkHeaderOddPages") = "on"
            Dim showHeaderOnEvenPages As Boolean = fields("ChkHeaderEvenPages") = "on"

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

            Dim showFooterOnFirstPage As Boolean = fields("ChkFooterFirstPage") = "on"
            Dim showFooterOnOddPages As Boolean = fields("ChkFooterOddPages") = "on"
            Dim showFooterOnEvenPages As Boolean = fields("ChkFooterEvenPages") = "on"

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

            ' instantiate a html to pdf converter object
            Dim converter As New HtmlToPdf()

            ' 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.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.AutoFitHeight = HtmlToPdfPageFitMode.AutoFit
            converter.Footer.Add(footerHtml)

            ' add page numbering element to the footer
            If fields("ChkPageNumbering") = "on" 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(fields("TxtUrl"))

            ' 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)
                customHeader.Add(customHtml)

                page.CustomHeader = customHeader
            End If

            ' save pdf document
            Dim pdf As Byte() = doc.Save()

            ' close pdf document
            doc.Close()

            ' return resulted pdf document
            Dim fileResult As FileResult = New FileContentResult(pdf, "application/pdf")
            fileResult.FileDownloadName = "Document.pdf"
            Return fileResult
        End Function
    End Class
End Namespace