Rasterize PDF Pages to Images

With HiQPdf library you can rasterize PDF pages to images using the HiQPdfClientPdfRasterizer class. You can extract the images to memory HiQPdfClientPdfPageRasterImage objects using the PdfRasterizerRasterizeToImageObjects(String, Int32, Int32) method or to image files using the PdfRasterizerRasterizeToImageFiles(String, Int32, Int32, String, String) method. There is also a special method PdfRasterizerRaisePageRasterizedEvent(String, Int32, Int32) which can be used to rasterize the PDF document pages one by one in memory to avoid high memory usage for large PDF documents. The HiQPdfClientPdfPageRasterImage objects can be disposed when they are no longer necessary.

You have also the option to convert the pages of a PDF document to a multipage TIFF image into a file on disk or in a memory buffer. You can use the PdfRasterizerRasterizeToTiffFile(String, Int32, Int32, String) method to produce a TIFF file on disk or the PdfRasterizerRasterizeToTiff(String, Int32, Int32) method to produce the TIFF image in a memory buffer.

Rasterize PDF Pages to Images Demo

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, the range of PDF pages to rasterize. You can also choose to convert to a multipage TIFF image.

Demo Source Code

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Hosting;

using HiQPdfClient;

namespace HiQPdf_Demo.Controllers
{
    public class RasterizePdfPagesToImagesController : Controller
    {
        IFormCollection m_formCollection;
        IWebHostEnvironment m_hostingEnvironment;
        public RasterizePdfPagesToImagesController(IWebHostEnvironment hostingEnvironment)
        {
            m_hostingEnvironment = hostingEnvironment;
        }

        // GET: RasterizePdfPagesToImages
        public ActionResult Index()
        {
            SetCrtPageUri();

            return View();
        }

        [HttpPost]
        public ActionResult RasterizeToImages(IFormCollection collection)
        {
            m_formCollection = collection;

            string serverIP = collection["textBoxServerIP"];
            uint serverPort = uint.Parse(collection["textBoxServerPort"]);
            string serverPassword = collection["textBoxServerPassword"];

            // get the PDF file
            string pdfFile = m_hostingEnvironment.WebRootPath + "/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(collection["textBoxDPI"]);

            int fromPdfPageNumber = int.Parse(collection["textBoxFromPage"]);
            int toPdfPageNumber = collection["textBoxToPage"][0].Length > 0 ? int.Parse(collection["textBoxToPage"]) : 0;

            byte[] imageBuffer = null;

            if (collection["checkBoxToTiff"].Count > 0)
            {
                // 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 Redirect("/RasterizePdfPagesToImages");

                // get the first page image bytes in a buffer            
                try
                {
                    // get the image data in a buffer
                    imageBuffer = pageImages[0].ImageData;
                }
                finally
                {
                    // dispose the page images
                    for (int i = 0; i < pageImages.Length; i++)
                        pageImages[i].Dispose();
                }
            }

            if (collection["checkBoxToTiff"].Count > 0)
            {
                FileResult fileResult = new FileContentResult(imageBuffer, "image/tiff");
                fileResult.FileDownloadName = "PageImage.tiff";

                return fileResult;
            }
            else
            {
                FileResult fileResult = new FileContentResult(imageBuffer, "image/png");
                fileResult.FileDownloadName = "PageImage.png";

                return fileResult;
            }
        }

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

        private void SetCrtPageUri()
        {
            HttpRequest httpRequest = this.ControllerContext.HttpContext.Request;
            UriBuilder uriBuilder = new UriBuilder();
            uriBuilder.Scheme = httpRequest.Scheme;
            uriBuilder.Host = httpRequest.Host.Host;
            if (httpRequest.Host.Port != null)
                uriBuilder.Port = (int)httpRequest.Host.Port;
            uriBuilder.Path = httpRequest.PathBase.ToString() + httpRequest.Path.ToString();
            uriBuilder.Query = httpRequest.QueryString.ToString();

            ViewData["CrtPageUri"] = uriBuilder.Uri.AbsoluteUri;
        }
    }
}

See Also