SelectPdf for .NET - Pdf To Image Converter. Convert PDF pages to PNG, JPG, BMP or TIFF - VB.NET / ASP.NET MVC Sample

This sample shows how to use SelectPdf Pdf Library for .NET to convert PDF document pages to different image formats.

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

This sample will only convert the first page from the specified range (for PNG, JPG, BMP formats). For the TIFF multipage image format, all the specified pages will be converted.

Image Format:

Start Page:

End Page:

(Leave empty to extract until the last page)

Sample Code VB.NET

Imports SelectPdf
Imports System.IO
Imports System.Drawing.Imaging

Namespace Controllers
    Public Class PdfToImageConverterController
        Inherits Controller

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

        Public Function SubmitAction(collection As FormCollection) As ActionResult
            ' the test file
            Dim filePdf As String = Server.MapPath("~/files/selectpdf.pdf")

            ' settings
            Dim imgFormat As String = collection("DdlImageFormat")

            Dim startPage As Integer = 1
                startPage = Convert.ToInt32(collection("TxtStartPage"))
            End Try

            Dim endPage As Integer = 0
                endPage = Convert.ToInt32(collection("TxtEndPage"))
            End Try

            ' instantiate a pdf rasterizer (pdf to image converter) object
            Dim rasterizer As New PdfRasterizer()

            ' load PDF file

            ' set the properties
            rasterizer.StartPageNumber = startPage
            rasterizer.EndPageNumber = endPage

            ' other properties that can be set
            rasterizer.Resolution = 150
            rasterizer.ColorSpace = PdfRasterizerColorSpace.RGB

            ' convert pages to images
            Dim imgBytes As Byte()
            Dim httpHeader As String
            Dim fileName As String
            If imgFormat = "TIFF" Then
                ' TIFF image format
                imgBytes = rasterizer.ConvertToTiff()
                httpHeader = "image/tiff"
                fileName = "Image.tiff"
                ' the other image formats (PNG, JPG, BMP)
                Dim images As System.Drawing.Image() = rasterizer.ConvertToImages()

                Dim format As System.Drawing.Imaging.ImageFormat
                If imgFormat = "PNG" Then
                    format = System.Drawing.Imaging.ImageFormat.Png
                    httpHeader = "image/png"
                    fileName = "Image.png"
                ElseIf imgFormat = "JPG" Then
                    format = System.Drawing.Imaging.ImageFormat.Jpeg
                    httpHeader = "image/jpeg"
                    fileName = "Image.jpg"
                    format = System.Drawing.Imaging.ImageFormat.Bmp
                    httpHeader = "image/bmp"
                    fileName = "Image.bmp"
                End If

                imgBytes = GetImageBytes(images(0), format)
            End If

            ' return resulted image
            Dim fileResult As FileResult = New FileContentResult(imgBytes, httpHeader)
            fileResult.FileDownloadName = fileName
            Return fileResult
        End Function

        Private Function GetImageBytes(image As System.Drawing.Image,
                                       format As ImageFormat) As Byte()
            Using ms = New MemoryStream()
                image.Save(ms, format)
                Return ms.ToArray()
            End Using
        End Function
    End Class
End Namespace