Convert PDF File Into Image File(png,jpg,jpeg) Using GhostScript

Ghostscript is an interpreter for the PostScript language and for PDF.

Official Website.

First, we need to add Ghostscript in our solution by going to the Package Manager Console or we can add its dll file directly in reference of our Solution.



After you have installed the GhostScript in your application, please check in your reference whether it exists or not.



After that, design your UI like the below image.


And, create two new folders in that Solution with the names - PdfFolder and ImgFolder respectively.


Note

  1. Please install Ghostscript in your computer according to its bit rate, that is32/64 bit. Please refer this link and always install and use the latest version. You might get confused here that if I already have installed the Ghostscript in my Solution, then what is the need of installing it again. The reason behind is that while converting the PDF file to an image file, we need Ghostscript in our computer; and to access the Ghostscript properties, we have to install it in our Visual Studio Solution.

  2. If we haven't installed the Ghostscript in our computer and are trying to run the application directly, then we can encounter the error which is shown in the below picture,


After that, I click on the Submit button. First, I am storing the original PDF in PdfFolder.



Here, I am calling Pdf2ImageConversion method which contains 2 parameters in which the first parameter is the name of the file and the second parameter is PdfFolder path.



Here, first I am checking whether that image exists in the ImgFolder or not. First, I created the object of GhostscriptPngDevice which will used to convert the Pdf to Image. After that I set the properties for that object.

img.InputFiles.Add(PdfFolderPath);


In the above line I am adding the SourceFile whose extension is “.pdf”.

Img.OutputPath=ImgFolderPath;

In the above line I am adding the OutputFile whose extension is “.png”;

Similarly I added some more properties for that object. Please see above in the picture like resolution etc.

And in the img.Process(); actually the pdf is converting to png image.

After Conversion the PdfFolder looks like this,



And the ImgFolder looks like this,



Note

  1. In the above example, I converted the PDF file into png image file. But, if you want to convert pdf file into jpg/jpeg, then in place of png, please write jpg/jpeg.
  2. If you want to convert PDF file into image file in production server or any other server, then you should install the Ghostscript (32/64 bit) for that server otherwise it will show you an error.
  3. This is just a sample application and you can change or modify anything as per your requirement.