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:
pt

Right Margin:
pt

Allow Scaling
Bottom Margin:
pt

Left Margin:
pt

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