Click or drag to resize
Pdf Library for .NET

Convert PDF Pages to Images

SelectPdf Library for .NET can be used to convert PDF pages to PNG, JPG, BMP or TIFF using the Pdf To Image Converter.

The main class of the PDF to Image Converter is PdfRasterizer. The PDF can be loaded using Load methods. The PDF pages can be converted to sytem Image objects using the the ConvertToImages merthod or to TIFF multiframe images using the ConvertToTiff method.

Altenativelly, the images can be saved directly into files on disk using Save or the SaveTiff methods.

The PdfRasterizer class provides are few other features:

Pdf To Image Converter Properties

StartPageNumber - The page number from where the current operation will start on the PDF file. The default value is 1 which means that the operation will start from the first page.

EndPageNumber - The page number where the current operation will end on the PDF file. The default value is 0 which means that all the PDF document is processed starting from the StartPageNumber page.

UserPassword - The user password to be used to open the PDF document for reading. The default value is null, which means that no password will be used to open the PDF document.

ColorSpace - The color space of the generated images. Default value is RGB.

Resolution - The resolution of the generated images in DPI. Default value is 120.

Some additional properties can be set if the images are saved directly into files on disk using the Save method:

ImagesFormat - The image format for the generated images.

ImagesPath - The path to the disk folder where the images will be saved.

ImagesPrefix - The prefix for the generated images file name.

Pdf To Image Converter Sample

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

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