Convert PDF to PNG using Ghostscript.NET

Rajnilari2015
Posted by in C# category on for Beginner level | Points: 250 | Views : 39593 red flag

In this article, we will look into converting PDF files to PNG using Ghostscript.NET.


 Download source code for Convert PDF 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.

Page copy protected against web site content infringement by Copyscape

About the Author

Rajnilari2015
Full Name: Niladri Biswas (RNA Team)
Member Level: Platinum
Member Status: Member,Microsoft_MVP,MVP
Member Since: 3/17/2015 2:41:06 AM
Country: India
-- Thanks & Regards, RNA Team


Login to vote for this post.

Comments or Responses

Posted by: Vignesjoseph on: 2/10/2017 | Points: 25
You can use C# to run the GhostScript command line or use Platform Invoke (pInvoke) calls to call the GhostScript dll directly.
GhostScript is primarily file based, so the input is path to a file on disk and the output is the creation of files on disk. The parameters used to call either the dll or exe are basically the same, so there is not a huge benefit to calling the dll directly, but does make for nicer code.
I have C# wrapper that can be used to call the ghostscript dll, if you email me (address on profile) I will send it to you.
Posted by: Aprilrussell on: 9/25/2017 | Points: 25
Thank you for sharing such an important topic with us
Five nights at freddy’s: http://fivenightsat-freddys.com

Login to post response

Comment using Facebook(Author doesn't get notification)