Skip Navigation Links.
In this demo you can learn how to rasterize the pages of PDF document to images. You can choose the color space of resulted images, the resolution of the rasterization and the range of PDF pages to rasterize. You can also choose to convert to a multipage TIFF image.
Server Settings
IP: Port: Password:
PDF document:
Open the Input PDF Document
Settings
Images Color Space:
Rasterization Resolution: DPI
Page Range
From: To:
Skip Navigation Links
C# Code
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using HiQPdfClient;

namespace HiQPdf_Demo
{
    public partial class RasterizePdfPagesToImages : System.Web.UI.Page
    {
        protected void buttonRasterizeToImages_Click(object sender, EventArgs e)
        {
            string serverIP = textBoxServerIP.Text;
            uint serverPort = uint.Parse(textBoxServerPort.Text);
            string serverPassword = textBoxServerPassword.Text;

            // get the PDF file
            string pdfFile = Server.MapPath("~") + @"\DemoFiles\Pdf\InputPdf.pdf";

            // create the PDF rasterizer
            PdfRasterizer pdfRasterizer = new PdfRasterizer(serverIP, serverPort);

            // use server password if necessary
            if (serverPassword.Length > 0)
                pdfRasterizer.ServerPassword = serverPassword;

            // set a demo serial number
            pdfRasterizer.SerialNumber = "YCgJMTAE-BiwJAhIB-EhlWTlBA-UEBRQFBA-U1FOUVJO-WVlZWQ==";

            // set the output images color space
            pdfRasterizer.ColorSpace = GetColorSpace();

            // set the rasterization resolution in DPI
            pdfRasterizer.DPI = int.Parse(textBoxDPI.Text);

            int fromPdfPageNumber = int.Parse(textBoxFromPage.Text);
            int toPdfPageNumber = textBoxToPage.Text.Length > 0 ? int.Parse(textBoxToPage.Text) : 0;

            byte[] imageBuffer = null;

            if (checkBoxToTiff.Checked)
            {
                // convert the PDF document to a multipage TIFF image in a memory buffer
                // the TIFF images can also be produced in a file using the RasterizeToTiffFile method
                imageBuffer = pdfRasterizer.RasterizeToTiff(pdfFile, fromPdfPageNumber, toPdfPageNumber);
            }
            else
            {
                // rasterize a range of pages of the PDF document to memory in .NET Image objects
                // the images can also be produced to a folder using the RasterizeToImageFiles method
                // or they can be produced one by one using the RaisePageRasterizedEvent method
                PdfPageRasterImage[] pageImages = pdfRasterizer.RasterizeToImageObjects(pdfFile, fromPdfPageNumber, toPdfPageNumber);

                // return if no page was rasterized
                if (pageImages.Length == 0)
                    return;

                // get the first page image bytes in a buffer            
                try
                {
                    // get the .NET Image object
                    imageBuffer = pageImages[0].ImageData;
                }
                finally
                {
                    // dispose the page images
                    for (int i = 0; i < pageImages.Length; i++)
                        pageImages[i].Dispose();
                }
            }
            
            if (checkBoxToTiff.Checked)
            {
                // inform the browser about the binary data format
                HttpContext.Current.Response.AddHeader("Content-Type", "image/tiff");

                // let the browser know how to open the image and the image name
                HttpContext.Current.Response.AddHeader("Content-Disposition",
                            String.Format("attachment; filename={0}; size={1}", "PageImage.tiff", imageBuffer.Length.ToString()));
            }
            else
            {
                // inform the browser about the binary data format
                HttpContext.Current.Response.AddHeader("Content-Type", "image/png");

                // let the browser know how to open the image and the image name
                HttpContext.Current.Response.AddHeader("Content-Disposition",
                            String.Format("attachment; filename={0}; size={1}", "PageImage.png", imageBuffer.Length.ToString()));
            }

            // write the image buffer to HTTP response
            HttpContext.Current.Response.BinaryWrite(imageBuffer);

            // call End() method of HTTP response to stop ASP.NET page processing
            HttpContext.Current.Response.End();
        }

        private RasterImageColorSpace GetColorSpace()
        {
            switch (dropDownListColorSpace.SelectedItem.ToString())
            {
                case "RGB":
                    return RasterImageColorSpace.Rgb;
                case "Gray Scale":
                    return RasterImageColorSpace.GrayScale;
                case "Black and White":
                    return RasterImageColorSpace.BlackWhite;
                default:
                    return RasterImageColorSpace.Rgb;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string pageUri = HttpContext.Current.Request.Url.AbsoluteUri;
                hyperLinkOpenPdf.NavigateUrl = pageUri.Substring(0, pageUri.LastIndexOf('/')) + @"/DemoFiles/Pdf/InputPdf.pdf";

                Master.SelectNode("rasterizePdf");

                Master.LoadCodeSample("RasterizePdfPagesToImages");
            }
        }
    }
}