Imports SelectPdf
Public Class CurrentWebPageToPdfController
    Inherits System.Web.Mvc.Controller

    '
    ' GET: /CurrentWebPageToPdf

    Function Index() As ActionResult
        ViewData.Add("SampleText", "sample text")
        ViewData.Add("DropDownList1", "Value3")
        ViewData.Add("SampleCheckbox", "true")
        ViewData.Add("SampleText2", "1000")
        ViewData.Add("SampleText3", "800")

        Return View()
    End Function

    <HttpPost()>
    Public Function Convert(collection As FormCollection) As ActionResult
        ' set data for the view
        Dim MyViewData As ViewDataDictionary = GetViewData(collection)

        ' render view to get html
        Dim html As New System.IO.StringWriter()

        Dim viewEngineResult As ViewEngineResult = _
            ViewEngines.Engines.FindView(ControllerContext, "Index", Nothing)
        Dim viewContext As New ViewContext(ControllerContext, _
                                           viewEngineResult.View, MyViewData, _
                                           New TempDataDictionary(), html)
        viewEngineResult.View.Render(viewContext, html)

        Dim htmlString As String = html.ToString()

        ' get base url
        Dim baseUrl As String = Me.ControllerContext.HttpContext.Request.Url.AbsoluteUri.Substring(0, _
                Me.ControllerContext.HttpContext.Request.Url.AbsoluteUri.Length - "CurrentWebPageToPdf/Convert".Length)

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

        ' create a new pdf document converting a html string
        Dim doc As PdfDocument = converter.ConvertHtmlString(htmlString, baseUrl)

        ' 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

    Private Function GetViewData(collection As FormCollection) As ViewDataDictionary
        Dim MyViewData As New ViewDataDictionary()

        MyViewData.Add("SampleText", collection("TxtSampleText"))
        MyViewData.Add("DropDownList1", collection("DropDownList1"))
        MyViewData.Add("SampleCheckbox", collection("ChkSampleCheckbox"))

        MyViewData.Add("SampleText2", collection("TxtSampleText2"))
        MyViewData.Add("SampleText3", collection("TxtSampleText3"))

        Return MyViewData
    End Function

End Class