How to Use Ghostscript DLL to Convert PDF to Image in C#
Ghostscript is a popular software that can interpret PostScript and PDF files and convert them to various formats, such as image, text, or printer formats. Ghostscript.NET is a .NET wrapper library around the Ghostscript library that allows you to use its functionality from C# code.
In this article, we will show you how to use Ghostscript.NET to convert PDF files to image files in C#, using the following steps:
Download and install Ghostscript on your machine from here. You can choose either the open source AGPL license or the commercial license, depending on your needs. Make sure you download the correct version for your platform (32-bit or 64-bit).
Download and install Ghostscript.NET from here. You can use the NuGet package manager or the command line tool to install it in your project.
Add a reference to Ghostscript.NET.dll in your project.
Create an instance of GhostscriptRasterizer class, which is the main class for converting PDF files to image files.
Call the Open method of the GhostscriptRasterizer instance, passing the path of the PDF file you want to convert and an instance of GhostscriptVersionInfo, which contains information about the installed Ghostscript version.
Use a loop to iterate through the pages of the PDF file, and call the GetPage method of the GhostscriptRasterizer instance, passing the desired resolution (in DPI) and the page number. This method will return an Image object that represents the converted page.
Save the Image object to a file using the Save method, passing the path and format of the output image file.
Close the GhostscriptRasterizer instance using the Close method.
The following code snippet shows an example of how to use Ghostscript.NET to convert a PDF file to PNG images in C#:
static void Main(string args)
// Create an instance of GhostscriptRasterizer
using (var rasterizer = new GhostscriptRasterizer())
// Get the path of the PDF file
var pdfPath = @"C:\sample.pdf";
// Get the path of the installed Ghostscript
var gsVersion = new GhostscriptVersionInfo(@"C:\Program Files\gs\gs10.0.0\bin\gsdll64.dll");