How to Convert PDF to Image in C#

This quick guide provides details on how to convert PDF to image in C#. You can set resolution, height, width and other properties like setting default font, scaling images to fit the page width, and many others. For converting a PDF page to image C# code is used and detailed steps are given below to convert a PDF file to a PNG image.

Steps to Convert PDF to Image in C#

  1. Install Aspose.PDF using NuGet package manager to convert PDF to image
  2. Load the source PDF file into the Document class object for converting to image
  3. Create the Resolution object to set output image resolution
  4. Initialize PngDevice class object using the resolution object
  5. Parse through all the pages in the source PDF using Document.Pages collection
  6. Call PngDevice.Process function to convert each PDF page to image and save on disk

Here you get the step-by-step details to render the PDF to image in C# by adding necessary references and then loading the target PDF. You can set different configurations for all the output images file and parse through all the PDF file pages for converting to image. In the end, each converted image is saved to a separate file on disk.

Code to Export PDF to image in C#

using Aspose.Pdf;
using Aspose.Pdf.Devices;
using System.IO;
namespace ConvertPdfToImageInCSharp
{
class Program
{
static void Main(string[] args)
{
// Instantiate the license as the first step to avoid trial version restrictions and watermark
License PdfToImageLicense = new License();
PdfToImageLicense.SetLicense("Aspose.PDF.lic");
// Load the source PDF file to be converted to PDF
Document sourcePdfDoc = new Document("ConvertAllPagesToPng.pdf");
// Create Resolution object
Resolution imgResolution = new Resolution(300);
// Initialize the PngDevice object to create and configure output images
PngDevice pngDevice = new PngDevice(imgResolution);
// Parse through all the pages in the PDF for conversion to image
for (int pageNumber = 1; pageNumber <= sourcePdfDoc.Pages.Count; pageNumber++)
{
// Create the output file stream by providing different name for each image
using (FileStream fileStream = new FileStream($"image{pageNumber}_out.png",
FileMode.Create))
{
// Convert a particular page and save the image to stream
pngDevice.Process(sourcePdfDoc.Pages[pageNumber], fileStream);
// Close stream
fileStream.Close();
}
}
}
}
}

This code uses the Document class object to load the source PDF where the Document class contains pages collection for iteration. To set different properties of the output images, Resolution and PngDevice objects are declared that support setting a variety of parameters like image resolution, height, width, BarcodeOptimization, InterpolationHighQuality, and OptimizeDimensions. Note that you can use BmpDevice, EmfDevice, GifDevice, and many others to create different types of images.

We have learned the process to transfer PDF pages to images using C#. If you want to learn the creation of PDF files from scratch, refer to the article on how to create PDF in C#.

 English