Imports SelectPdf

Namespace Controllers
    Public Class ConvertMultipleUrlsToPdfController
        Inherits Controller

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

        <HttpPost>
        Public Function SubmitAction(collection As FormCollection) As ActionResult
            Dim renderingEngine As RenderingEngine = CType([Enum].Parse(GetType(RenderingEngine), Request("DdlRenderingEngine"), True), RenderingEngine)
            ' read parameters from the webpage
            Dim url1 As String = collection("TxtUrl1")
            Dim url2 As String = collection("TxtUrl2")
            Dim url3 As String = collection("TxtUrl3")

            ' create a new pdf document 
            Dim doc As New PdfDocument()

            ' add a new page to the document
            Dim page As PdfPage = doc.AddPage()

            ' add the first url
            Dim html1 As New PdfHtmlElement(url1)
            html1.RenderingEngine = renderingEngine
            html1.CefEnginePath = Server.MapPath(ConfigurationManager.AppSettings("CefEnginePath"))
            Dim result As PdfRenderingResult = page.Add(html1)

            ' get the last page of the previous conversion
            Dim page2 As PdfPage = doc.Pages(result.PdfPageLastIndex)

            ' add the second url
            Dim html2 As New PdfHtmlElement(result.PdfPageLastRectangle.Left,
                                            result.PdfPageLastRectangle.Bottom, url2)
            html2.RenderingEngine = renderingEngine
            html2.CefEnginePath = Server.MapPath(ConfigurationManager.AppSettings("CefEnginePath"))
            result = page2.Add(html2)

            ' get the last page of the previous conversion
            Dim page3 As PdfPage = doc.Pages(result.PdfPageLastIndex)

            ' add the third url
            Dim html3 As New PdfHtmlElement(result.PdfPageLastRectangle.Left,
                                            result.PdfPageLastRectangle.Bottom, url3)
            html3.RenderingEngine = renderingEngine
            html3.CefEnginePath = Server.MapPath(ConfigurationManager.AppSettings("CefEnginePath"))
            result = page3.Add(html3)

            ' 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