SelectPdf for .NET - Pdf Resize/Scale Content - VB.NET / ASP.NET 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

Bottom Margin:
pt

Left Margin:
pt

Horizontal Alignment:


Vertical Alignment:



Sample Code Vb.Net



Public Class pdf_resize
    Inherits System.Web.UI.Page

    Protected Sub BtnCreatePdf_Click(sender As Object, e As EventArgs) _
        Handles BtnCreatePdf.Click

        ' the test file
        Dim filePdf As String = Server.MapPath("~/files/selectpdf.pdf")

        ' settings
        Dim pdf_page_size As String = DdlPageSize.SelectedValue
        Dim pdfPageSize1 As PdfPageSize = _
            DirectCast([Enum].Parse(GetType(PdfPageSize), pdf_page_size, True),  _
                PdfPageSize)

        Dim pageSize As PdfCustomPageSize = PdfCustomPageSize.A4
        Select Case pdfPageSize1
            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 = DdlPageOrientation.SelectedValue
        Dim pdfOrientation As PdfPageOrientation = _
            DirectCast([Enum].Parse(GetType(PdfPageOrientation), pdf_orientation, True),  _
                PdfPageOrientation)

        Dim topMargin As Integer = 0
        Try
            topMargin = Convert.ToInt32(TxtTopMargin.Text)
        Catch
        End Try

        Dim rightMargin As Integer = 0
        Try
            rightMargin = Convert.ToInt32(TxtRightMargin.Text)
        Catch
        End Try

        Dim bottomMargin As Integer = 0
        Try
            bottomMargin = Convert.ToInt32(TxtBottomMargin.Text)
        Catch
        End Try

        Dim leftMargin As Integer = 0
        Try
            leftMargin = Convert.ToInt32(TxtLeftMargin.Text)
        Catch
        End Try

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

        Dim vertical_align As String = DdlVerticalAlign.SelectedValue
        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 = ChkAllowScaling.Checked
        resizer.KeepAspectRatio = ChkKeepAspectRatio.Checked
        resizer.HorizontalAlign = pdfHorizontalAlign
        resizer.VerticalAlign = pdfVerticalAlign

        ' save pdf document
        resizer.Save(Response, False, "Sample.pdf")

        ' close pdf document
        resizer.Close()
    End Sub

End Class