Imports SelectPdf

Namespace Controllers
    Public Class PdfResizeController
        Inherits Controller

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

        <HttpPost> _
        Public Function SubmitAction(collection As FormCollection) As ActionResult
            ' the test file
            Dim filePdf As String = Server.MapPath("~/files/selectpdf.pdf")

            ' settings
            Dim pdf_page_size As String = collection("DdlPageSize")
            Dim pdfSize As PdfPageSize = DirectCast(
                [Enum].Parse(GetType(PdfPageSize), pdf_page_size, True), PdfPageSize)

            Dim pageSize As PdfCustomPageSize = PdfCustomPageSize.A4
            Select Case pdfSize
                Case PdfPageSize.A1
                    pageSize = PdfCustomPageSize.A1
                    Exit Select
                Case PdfPageSize.A2
                    pageSize = PdfCustomPageSize.A2
                    Exit Select
                Case PdfPageSize.A3
                    pageSize = PdfCustomPageSize.A3
                    Exit Select
                Case PdfPageSize.A5
                    pageSize = PdfCustomPageSize.A5
                    Exit Select
                Case PdfPageSize.Letter
                    pageSize = PdfCustomPageSize.Letter
                    Exit Select
                Case PdfPageSize.HalfLetter
                    pageSize = PdfCustomPageSize.HalfLetter
                    Exit Select
                Case PdfPageSize.Ledger
                    pageSize = PdfCustomPageSize.Ledger
                    Exit Select
                Case PdfPageSize.Legal
                    pageSize = PdfCustomPageSize.Legal
                    Exit Select
            End Select

            Dim pdf_orientation As String = collection("DdlPageOrientation")
            Dim pdfOrientation As PdfPageOrientation = DirectCast(
                [Enum].Parse(GetType(PdfPageOrientation), pdf_orientation, True),
                PdfPageOrientation)

            Dim topMargin As Integer = 0
            Try
                topMargin = Int32.Parse(collection("TxtTopMargin"))
            Catch
            End Try

            Dim rightMargin As Integer = 0
            Try
                rightMargin = Int32.Parse(collection("TxtRightMargin"))
            Catch
            End Try

            Dim bottomMargin As Integer = 0
            Try
                bottomMargin = Int32.Parse(collection("TxtBottomMargin"))
            Catch
            End Try

            Dim leftMargin As Integer = 0
            Try
                leftMargin = Int32.Parse(collection("TxtLeftMargin"))
            Catch
            End Try

            Dim horizontal_align As String = collection("DdlHorizontalAlign")
            Dim pdfHorizontalAlign As PdfHorizontalAlign = DirectCast(
                [Enum].Parse(GetType(PdfHorizontalAlign), horizontal_align, True),
                PdfHorizontalAlign)

            Dim vertical_align As String = collection("DdlVerticalAlign")
            Dim pdfVerticalAlign As PdfVerticalAlign = DirectCast(
                [Enum].Parse(GetType(PdfVerticalAlign), vertical_align, True),
                PdfVerticalAlign)

            ' resize the content
            Dim resizer As New PdfResizeManager()
            resizer.Load(filePdf)

            resizer.PageMargins = New PdfMargins(
                leftMargin, rightMargin, topMargin, bottomMargin)
            resizer.PageSize = pageSize
            resizer.PageOrientation = pdfOrientation
            resizer.AllowScale = collection("ChkAllowScaling") = "on"
            resizer.KeepAspectRatio = collection("ChkKeepAspectRatio") = "on"
            resizer.HorizontalAlign = pdfHorizontalAlign
            resizer.VerticalAlign = pdfVerticalAlign

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

            ' close pdf document
            resizer.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