How to use ghostscript to convert pdf to image

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

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

I installed gs905w64.exe , but when I tried to use add reference for my web application, I get this error.

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

+4
source share
5 answers

You can use C # to run the GhostScript command line, or use Platform Invoke (pInvoke) calls to directly invoke the GhostScript dll.

GhostScript is mainly file-based, so the input is the path to the file on the disk, and the output is the creation of the files on the disk. The parameters used to call either dll or exe are basically the same, so there is no benefit to calling dll directly, but that makes the code nicer.

I have a C # shell that can be used to call ghostscript dll, if you write to me (the address in the profile), I will send it to you.

NTN

UPDATE:

code repo has moved to https://bitbucket.org/brightertools/ghostscript

+7
source

The gsdll32.dll file gsdll32.dll not a .NET managed library. You cannot reference it in your project. You must include it in your project as β€œcontent” (menu: Add an existing item) and let VS copy it to the output directory. In the meantime, you should read the Ghostscript API documents and this article on PInvoke.net on how to reference Ghostscript functions.

Keep in mind that Ghostscript is all unmanaged code and that you must do the cleanup yourself after using the library.

Edit: What Robert said is also important. Of course, you must use the correct version of the Ghostscript library.

+6
source

You do not need to add a link to the DLL for your project. First, download the gs910w32.exe application file, then install it on your local computer. Get the location of the installed .exe file for example: -

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

use it in your c # application like:

 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 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 aspect ratio of the output image in the argument with the -r flag. If you use "-r300", the image width will be 3000 pixels and the height will change accordingly, from the above argument you will get an image with a size from 1024 to 768 pixels.

+6
source

Why are you trying to add a library as a reference to your project? gsdll32.dll is a native dll, not a Dot-Net library.

When I create a sample project using Visual C # Express 2010, I get an exe file. If I executed it, it will try to access gsdll32.dll . The problem is that a 64-bit executable file is generated on a 64-bit system, but gsdll32.dll compiled for 32 bits.

The correct solution would be to change the source code and replace gsdll32.dll with gsdll64.dll wherever this happens. An easier solution is to use the 64-bit version of Ghostscript, copy gsdll64.dll to the same directory as ConvertPDF.exe , and rename it to gsdll32.dll . It definitely works - just tested and converted to PDF in TIFF.

+3
source

you need to run the command below to link to the http://www.nuget.org/packages/GhostScriptSharp/ library

VS2012 β†’ Tools β†’ Library Package Manager β†’ Package Manager Console

0
source

All Articles