SelectPdf for .NET - Pdf Compression - VB.NET / ASP.NET MVC Sample

SelectPdf Library for .NET offers the possibility to reduce the size of the generated pdf document using compression.

This sample shows how to create a new PDF document using SelectPdf, how to add a text element to it and use compression to reduce the size of the generated pdf document.

Compression Level:

Sample Code VB.NET

Imports SelectPdf

Namespace Controllers
    Public Class PdfCompressionController
        Inherits Controller

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

        Public Function SubmitAction(collection As FormCollection) As ActionResult
            ' create a new pdf document
            Dim doc As New PdfDocument()
            doc.Margins = New PdfMargins(20, 20, 20, 20)

            ' set compression level
            doc.CompressionLevel = DirectCast([Enum].Parse(GetType(PdfCompressionLevel),
                    collection("DdlPdfCompressionLevel"), True), PdfCompressionLevel)

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

            ' add a new page to the document
            Dim page As PdfPage = doc.AddPage()

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

            ' save pdf document
            Dim pdf As Byte() = doc.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