SelectPdf for .NET - Pdf Resize/Scale Content - VB.NET / ASP.NET MVC Sample

This sample shows how to use SelectPdf Library for .NET to resize the pages of an existing PDF document, add margins, change page orientation. That can be done keeping the aspect ratio of the content or not, allowing scaling or not.

The sample uses the following (existing) test PDF:
Test PDF document

The file has A4 pages, Portrait orientation, 5pt margins.

Resize settings
The new file will have the following settings:
(Note: Margins bellow are added to the existing margins. Alignments are only taken into account if the scaling is allowed and the aspect ratio is preserved.)

Pdf Page Size:

Pdf Page Orientation:

Top Margin:

Right Margin:

Allow Scaling
Bottom Margin:

Left Margin:

Keep Aspect Ratio
Horizontal Alignment:

Sample Code VB.NET

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),

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

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

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

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

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

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

            ' resize the content
            Dim resizer As New PdfResizeManager()

            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

            ' 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