Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Ghostscript for converting PDF to Image

I found that Ghostscript is able to convert PDF to Image format.

I tried PDF to Image Converter but not able to understand it clearly.

I have installed gs905w64.exe but when I tried to add reference to my web application I am getting this error.

A reference to gsdll32.dll could not be added. No type libraries were found in the component.

like image 344
user1530755 Avatar asked Jul 17 '12 07:07

user1530755


People also ask

How to convert PDF files to PNG using Ghostscript?

In this article, we will look into converting PDF files to PNG using Ghostscript.NET. Step 1: Download the GhostScript (8.64) from here. Install it. Step 2: Fire up a console application and from Nuget Package Console issue We have to convert these files into PNG.

What is ghostpdf and Ghostscript?

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.

How to convert PDF file to image file?

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. This is just a sample application and you can change or modify anything as per your requirement.

What is the difference between Ghostscript and Adobe Acrobat?

Acrobat tends to be very forgiving of invalid PDF files. Ghostscript tends to expect files to conform to the standard. For example, even though valid PDF files must begin with %PDF, Acrobat will scan the first 1000 bytes or so for this string, and ignore any preceding garbage.


1 Answers

You Do not need to add any DLL reference to your project. First download the gs910w32.exe application file then install it to your local computer. Get the location of the installed .exe file eg:-

"C:\Program Files (x86)\gs\gs8.64\bin\gswin32.exe"

use it in your C# application as :

 
  private void PdfToJpg(string inputPDFFile, string outputImagesPath)
        {
            string ghostScriptPath = @"C:\Program Files (x86)\gs\gs8.64\bin\gswin32.exe";
            String ars = "-dNOPAUSE -sDEVICE=jpeg -r102.4 -o" + outputImagesPath + "%d.jpg -sPAPERSIZE=a4 " + inputPDFFile;
            Process proc = new Process();
            proc.StartInfo.FileName = ghostScriptPath;
            proc.StartInfo.Arguments = ars;
            proc.StartInfo.CreateNoWindow = true;
            proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            proc.Start();
            proc.WaitForExit();
        }
 

IF your input PDF file name has any spaces, you need to change the argument to

 
String ars = "-dNOPAUSE -sDEVICE=jpeg -r102.4 -o" + outputImagesPath + "%d.jpg -sPAPERSIZE=a4 " +"\"" + inputPDFFile + "\"";
 

you can specify the output image's aspect ratio in the argument with the -r flag. If you use "-r300" the width of the image will be 3000 pixel and height will change accordingly, from the above argument you will get 1024 to 768 size jpg image.

like image 152
S.Roshanth Avatar answered Nov 12 '22 05:11

S.Roshanth