SelectPdf for .NET - Pdf To Image Converter. Convert PDF pages to PNG, JPG, BMP or TIFF - VB.NET / ASP.NET 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



Public Class pdf_to_image_converter
    Inherits System.Web.UI.Page

    Protected Sub BtnSubmit_Click(sender As Object, e As EventArgs)
        ' the test file
        Dim filePdf As String = Server.MapPath("~/files/selectpdf.pdf")

        ' settings
        Dim imgFormat As String = DdlImageFormat.SelectedValue

        Dim startPage As Integer = 1
        Try
            startPage = Convert.ToInt32(TxtStartPage.Text)
        Catch
        End Try

        Dim endPage As Integer = 0
        Try
            endPage = Convert.ToInt32(TxtEndPage.Text)
        Catch
        End Try

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

        ' load PDF file
        rasterizer.Load(filePdf)

        ' 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"
        Else
            ' 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"
            Else
                format = System.Drawing.Imaging.ImageFormat.Bmp
                httpHeader = "image/bmp"
                fileName = "Image.bmp"
            End If

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

        ' send image to browser
        Response.Clear()
        Response.ClearHeaders()

        Response.AddHeader("Content-Type", httpHeader)
        Response.AddHeader("Content-Length", imgBytes.Length.ToString())
        Response.AppendHeader("content-disposition", _
                              "attachment;filename=""" + fileName + """")

        Response.BinaryWrite(imgBytes)
        Response.[End]()
    End Sub

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

End Class