Converting PDF to Image in C#

In this article, we will explore how to use C# to convert PDF to image.

The following way can be used to perform C# PDF to image conversion. We're going to be using a resolution of 300 DPI in the examples.

Ghostscript.NET

A .NET wrapper around the Ghostscript library, which is a powerful tool for working with PDF and PostScript files. It can be used for converting PDFs to images but requires understanding of Ghostscript commands.

using Ghostscript.NET.Rasterizer;

string pdfPath = "input.pdf";
string imagePath = "output.jpg";

using (GhostscriptRasterizer rasterizer = new GhostscriptRasterizer())
{
    rasterizer.Open(pdfPath);
    var img = rasterizer.GetPage(300, 300, 1);
    img.Save(imagePath, ImageFormat.Jpeg);
}

Spire.PDF

Part of the Spire.Doc suite, this library can convert PDFs to various formats including images. It offers both trial and commercial versions.

using Spire.Pdf;
using System.Drawing.Imaging;

namespace PdfToImageConversion
{
    class Program
    {
        static void Main(string[] args)
        {
            string pdfPath = @"path\to\your\file.pdf";
            string imagePath = @"path\to\your\output.png";

            ConvertPdfToImage(pdfPath, imagePath);
        }

        private static void ConvertPdfToImage(string pdfPath, string imagePath)
        {
            using (PdfDocument doc = new PdfDocument())
            {
                doc.LoadFromFile(pdfPath);

                using (var image = doc.SaveAsImage(0))
                {
                    image.Save(imagePath, ImageFormat.Png);
                }
            }
        }
    }
}

Patagames Pdf.Net

This library provides functionality for rendering PDFs, and it can be used to convert PDF pages to images. It's a commercial product with a free trial available.

using Patagames.Pdf.Net;

string pdfPath = "input.pdf";
string imagePath = "output.jpg";

using (PdfDocument doc = PdfDocument.Load(pdfPath))
{
    using (var bmp = doc.Render(0, 300, 300, true))
    {
        bmp.Save(imagePath, ImageFormat.Jpeg);
    }
}

Bonus

Alternative way to convert PDFs to images could be invoking external commands that will perform the conversion.

Ghostscript gswin64c

using System.Diagnostics;

public void ConvertPdfToImage(string pdfInputPath, string imageOutputPath)
{
    using (Process process = new Process())
    {
        process.StartInfo.UseShellExecute = false;
        process.StartInfo.FileName = "gswin64c.exe"; // or gswin32c.exe
        process.StartInfo.Arguments = $"-sDEVICE=jpeg -o {imageOutputPath} -r300 {pdfInputPath}";
        process.Start();
        process.WaitForExit();
    }
}

ImageMagick

using System.Diagnostics;

public void ConvertPdfToImage(string pdfInputPath, string imageOutputPath)
{
    using (Process process = new Process())
    {
        process.StartInfo.UseShellExecute = false;
        process.StartInfo.FileName = "magick"; // Ensure ImageMagick is installed and in PATH
        process.StartInfo.Arguments = $"convert -density 300 {pdfInputPath} {imageOutputPath}";
        process.Start();
        process.WaitForExit();
    }
}