SelectPdf for .NET - Pdf Page Settings - VB.NET / ASP.NET MVC Sample

This sample shows how to create a new PDF document using SelectPdf and how to specify the page size, margins and orientation.

The sample create a first page with no margins, size A4 and Portrait orientation and a second page with 20pt for all margins, Letter size and Landscape orientation.


Sample Code VB.NET



Imports SelectPdf

Namespace Controllers
    Public Class PageSettingsController
        Inherits Controller

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

        <HttpPost>
        Public Function SubmitAction(collection As FormCollection) As ActionResult
            ' create a new pdf document
            Dim doc As New PdfDocument()

            ' create a new pdf font
            Dim font As PdfFont = doc.AddFont(PdfStandardFont.Helvetica)
            font.Size = 18

            ' add a new page to the document 
            ' (with specific page size, margins and orientation)
            Dim page As PdfPage = doc.AddPage(PdfCustomPageSize.A4,
                    New PdfMargins(0.0F), PdfPageOrientation.Portrait)

            ' create a new text element and add it to the page
            Dim text As New PdfTextElement(0, 0, Helper.SomeText(), font)
            page.Add(text)

            ' add a new page to the document
            ' (with specific page size, margins and orientation)
            Dim page2 As PdfPage = doc.AddPage(PdfCustomPageSize.Letter,
                    New PdfMargins(20.0F), PdfPageOrientation.Landscape)

            ' create a new text element and add it to the page
            Dim text2 As New PdfTextElement(0, 0, Helper.SomeText(), font)
            page2.Add(text2)

            ' 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