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



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SelectPdf.Samples
{
    public partial class pdf_to_image_converter : System.Web.UI.Page
    {
        protected void BtnSubmit_Click(object sender, EventArgs e)
        {
            // the test file
            string filePdf = Server.MapPath("~/files/selectpdf.pdf");

            // settings
            string imgFormat = DdlImageFormat.SelectedValue;

            int startPage = 1;
            try
            {
                startPage = Convert.ToInt32(TxtStartPage.Text);
            }
            catch { }

            int endPage = 0;
            try
            {
                endPage = Convert.ToInt32(TxtEndPage.Text);
            }
            catch { }

            // instantiate a pdf rasterizer (pdf to image converter) object
            PdfRasterizer rasterizer = 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
            byte[] imgBytes;
            string httpHeader;
            string fileName;
            if (imgFormat == "TIFF")
            {
                // TIFF image format
                imgBytes = rasterizer.ConvertToTiff();
                httpHeader = "image/tiff";
                fileName = "Image.tiff";
            }
            else
            {
                // the other image formats (PNG, JPG, BMP)
                System.Drawing.Image[] images = rasterizer.ConvertToImages();

                System.Drawing.Imaging.ImageFormat format;
                if (imgFormat == "PNG")
                {
                    format = System.Drawing.Imaging.ImageFormat.Png;
                    httpHeader = "image/png";
                    fileName = "Image.png";
                }
                else if (imgFormat == "JPG")
                {
                    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";
                }

                imgBytes = GetImageBytes(images[0], format);
            }

            // 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();
        }

        private byte[] GetImageBytes(System.Drawing.Image image, 
            System.Drawing.Imaging.ImageFormat format)
        {
            using (var ms = new System.IO.MemoryStream())
            {
                image.Save(ms, format);
                return ms.ToArray();
            }
        }
    }
}