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



using System;
using System.IO;
using System.Drawing.Imaging;
using System.Web.Mvc;

namespace SelectPdf.Samples.Controllers
{
    public class PdfToImageConverterController : Controller
    {
        // GET: PdfToImageConverter
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult SubmitAction(FormCollection collection)
        {
            // the test file
            string filePdf = Server.MapPath("~/files/selectpdf.pdf");

            // settings
            string imgFormat = collection["DdlImageFormat"];

            int startPage = 1;
            try
            {
                startPage = Convert.ToInt32(collection["TxtStartPage"]);
            }
            catch { }

            int endPage = 0;
            try
            {
                endPage = Convert.ToInt32(collection["TxtEndPage"]);
            }
            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);
            }

            // return resulted image
            FileResult fileResult = new FileContentResult(imgBytes, httpHeader);
            fileResult.FileDownloadName = fileName;
            return fileResult;
        }

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