SelectPdf for .NET - Setting Pdf Viewer Preferences with the Html to Pdf Converter - VB.NET / ASP.NET MVC Sample

This sample shows how to convert from html to pdf using SelectPdf and set the pdf viewer preferences. With the viewer preferences, users can specify how the pdf document will appear in a pdf viewer when it is opened.

Url:

Page Layout:


Page Mode:


Center Window
Display Doc Title
Fit Window
Hide Menu Bar
Hide Toolbar
Hide Window UI


Sample Code VB.NET



Imports SelectPdf

Namespace Controllers
    Public Class PdfConverterViewerPreferencesController
        Inherits Controller

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

        <HttpPost> _
        Public Function SubmitAction(collection As FormCollection) As ActionResult
            ' read parameters from the webpage
            Dim url As String = collection("TxtUrl")

            Dim page_layout As String = collection("DdlPageLayout")
            Dim pageLayout As PdfViewerPageLayout = DirectCast(
                [Enum].Parse(GetType(PdfViewerPageLayout), page_layout, True),
                PdfViewerPageLayout)

            Dim page_mode As String = collection("DdlPageMode")
            Dim pageMode As PdfViewerPageMode = DirectCast(
                [Enum].Parse(GetType(PdfViewerPageMode), page_mode, True),
                PdfViewerPageMode)

            Dim centerWindow As Boolean = collection("ChkCenterWindow") = "on"
            Dim displayDocTitle As Boolean = collection("ChkDisplayDocTitle") = "on"
            Dim fitWindow As Boolean = collection("ChkFitWindow") = "on"
            Dim hideMenuBar As Boolean = collection("ChkHideMenuBar") = "on"
            Dim hideToolbar As Boolean = collection("ChkHideToolbar") = "on"
            Dim hideWindowUI As Boolean = collection("ChkHideWindowUI") = "on"

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

            ' set converter options
            converter.Options.ViewerPreferences.CenterWindow = centerWindow
            converter.Options.ViewerPreferences.DisplayDocTitle = displayDocTitle
            converter.Options.ViewerPreferences.FitWindow = fitWindow
            converter.Options.ViewerPreferences.HideMenuBar = hideMenuBar
            converter.Options.ViewerPreferences.HideToolbar = hideToolbar
            converter.Options.ViewerPreferences.HideWindowUI = hideWindowUI

            converter.Options.ViewerPreferences.PageLayout = pageLayout
            converter.Options.ViewerPreferences.PageMode = pageMode
            converter.Options.ViewerPreferences.NonFullScreenPageMode =
                PdfViewerFullScreenExitMode.UseNone

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

            ' 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