In this article, we will look into converting PDF files to PNG using Ghostscript.NET.
Introduction
Ghostscript is an interpreter for PostScript and Portable Document Format (PDF) files. It consists of a PostScript interpreter layer, and a graphics library. GhostPDF is an interpreter built on top of Ghostscript that handles PDF files. It relies on extensions to the PostScript language/imaging model.
Ghostscript.NET (written in C#) is the most completed managed wrapper library around the native Ghostscript library (32-bit & 64-bit), an interpreter for the PostScript language, PDF, related software and documentation.
In this article, we will look into converting PDF files to PNG using Ghostscript.NET.
Step by Step Explanation
Step 1: Download the GhostScript(8.64) from here.
Install it.


The installed files will be located at
Step 2: Fire up a console application and from Nuget Package Console issue
Install-Package Ghostscript.NET
Now let's say we have a set of pdf files
We have to convert these files into PNG.
Step 3: The below program will do the conversion
using Ghostscript.NET.Rasterizer;
using System;
using System.Drawing.Imaging;
using System.IO;
namespace PDF2Images
{
class Program
{
// Output Folder
static string outputFolder = @"D:\PDFSplit\Example\outputImages\";
static void Main(string[] args)
{
//Get all the PDF files in the specified location
var pdfFiles = Directory.GetFiles(@"D:\PDFSplit\Example\outputFolder\", "*.pdf");
//process each PDF file
foreach (var pdfFile in pdfFiles)
{
var fileName = Path.GetFileNameWithoutExtension(pdfFile);
PdfToPng(pdfFile, fileName);
}
Console.ReadKey();
}
private static void PdfToPng(string inputFile, string outputFileName)
{
var xDpi = 100; //set the x DPI
var yDpi = 100; //set the y DPI
var pageNumber = 1; // the pages in a PDF document
using (var rasterizer = new GhostscriptRasterizer()) //create an instance for GhostscriptRasterizer
{
rasterizer.Open(inputFile); //opens the PDF file for rasterizing
//set the output image(png's) complete path
var outputPNGPath = Path.Combine(outputFolder, string.Format("{0}.png", outputFileName));
//converts the PDF pages to png's
var pdf2PNG = rasterizer.GetPage(xDpi, yDpi, pageNumber);
//save the png's
pdf2PNG.Save(outputPNGPath, ImageFormat.Png);
Console.WriteLine("Saved " + outputPNGPath);
}
}
}
}
At first we are identified all the pdf files in a given location
var pdfFiles = Directory.GetFiles(@"D:\PDFSplit\Example\outputFolder\", "*.pdf");
Next we are processing each files by using the foreach loop and invoke the PdfToPng function.
Inside the PdfToPng function, initially we are creating instance for GhostscriptRasterizer. This class rasterize PDF, EPS or multi-page PostScript files to any common image format. Next by calling the GetPage function, we are converting the PDF pages to images by passing the printing resolution (DPI) and the pageNumber.
var pdf2PNG = rasterizer.GetPage(xDpi, yDpi, pageNumber);
Finally we are saving the images.
pdf2PNG.Save(outputPNGPath, ImageFormat.Png);
The resultant output will be
References
Ghostscript.NET
Conclusion
This article taught us how to convert PDF to PNG using Ghostscript.NET. Hope this will be helpful. Thanks for reading. Zipped file is attached herewith.