Introducing GhostscriptSharp
A few months ago, I posted some code that I had used to generate thumbnail images from PDF files. Before writing the code, I had searched high and low for some simple code that would allow me to take a PDF file and generate a thumbnail from the first page of that PDF file. Unfortunately, I didn’t find many C# solutions. The best I could find was the Ghostscript library and some references to using it from C#.
In my previous post, I talked about some of the issues I had with using Ghostscript from the command line. It wasn’t difficult, but it wasn’t something that I wanted to do. Finally, I ended up writing a C# wrapper that would talk to the Ghostscript library and generate the PDF thumbnail files that I wanted. I figured it was a common enough issue that at least someone else would find it useful.
Almost immediately, that post became the most popular post on my site. I still get 10 or so Google hits from it every day. So, I figured I’d clean up the code a little bit (which involved some helpful changes that people suggested in the comments of my post) and release it to the world.
I decided to call the library GhostscriptSharp and you can find it on Github. There are probably a lot of things that could be added or cleaned up, so if you’d like to contribute, feel free to create your own fork and let me know if you have changes I could pull in.
Tags: Ghostscript, GhostscriptSharp
January 8th, 2010 at 8:33 pm
Thanks for making this public. Searching the web, there are many references to Ghostscript for conversion, but no good examples of making it work in .NET. Your wrapper makes it clean and simple.
January 14th, 2010 at 7:04 am
Any thought to use this in a x64 environment?
January 24th, 2010 at 7:36 pm
@flalar I haven’t given any thought to it. I haven’t been doing .Net development in a while, so it just hasn’t come up.
Hopefully, someone else ends up needing that and forks the project on github. Otherwise, it might be a while before I get around to it.
January 29th, 2010 at 11:49 am
Fantastic, Matthew! Excellent work and you saved me a whole bunch of time and headache.
The only addition I would have is that the ghostscript you made available didn’t work on my machine, but this one: http://pages.cs.wisc.edu/~ghost/doc/GPL/gpl863.htm did. (Make sure you get the Windows version toward the bottom of the page.)
I don’t know if it was specific to my installation, but it may save others some time. AGAIN, GREAT WORK!
February 4th, 2010 at 3:07 am
Works fine with x64. .NET doesn’t care as it’s all CLR and Ghostscript is available as a x64 binary.
I’ve forked the project and added a few features to suit what I was doing. Feel free to merge it back in if you’re interested.
March 1st, 2010 at 5:49 am
I’m on Windows 7 64bit. I’ve installed the 64bit release of GhostScript 8.64, and added a path entry to the bin folder. However, I’m encountering an error:
An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0×8007000B)
Any ideas?
March 1st, 2010 at 8:54 am
Hmmm… I uninstalled the x64 release and installed the x86 release, and everything is working!? Non of my projects are set to target x86, so I’m not sure why it’s working.
Can this be used commercially?
March 11th, 2010 at 3:39 pm
@Milky
Sorry, I don’t know much about the x64 issues, so I can’t really help there.
The software is licensed under the MIT License, so, yes, it can be used commercially. It’s a pretty permissive license, but be sure to take a look at it if you have any concerns.
May 13th, 2010 at 1:16 pm
This looks exactly like what I have been trying to do. I have a simple .NET form that needs to be able to post a PDF thumbnail to a website (among other things, of course). But, as I am still in the infancy of my programmnig knowledge, I need to know how to include this into my existing project. I’m used to writing everything from scratch (with exception of some reference dll’s) and haven’t used a project such as this inside another project. Any and all assistance would be useful. Thanks!!
May 21st, 2010 at 1:27 pm
I’m trying to get this working on a ASP.NET page but the InitAPI method errors out returning a -100 for the result integer. Any ideas would be appreciated.
Thank,
Mehul
May 22nd, 2010 at 12:06 pm
The output image size is 10x times larger. I set 100×100, but got 978×1023. Why?
July 6th, 2010 at 1:19 am
First off - thanks for the great library. It has the potential to solve a major problem I’m currently working on.
However, I’m also getting that InitAPI method returning a -100. It’s a bit difficult to debug as well. Using the exact same arguments through the gswin32.exe executable works fine but initialising the API in code seems to fall over for some reason which I can’t seem to get.
Any help?
Regards,
Matthew
August 18th, 2010 at 9:28 am
I know this is a stupid question. Every time I try to add the reference, I get “..could not be added. Please make sure the file is accessible and that is is a valid assembly or COM component.”
I have tried two different gs releases and the dll packed in your GhostScript wrapper. I would love any help.
August 18th, 2010 at 9:34 am
Nevermind…Like I said, dumb question.
December 10th, 2010 at 9:40 am
Since your library is licensed under the MIT license and you use the ghostscript dll.. doesn’t that violate’s GhostScript’s GPL license ?
January 6th, 2011 at 9:59 pm
@GuiSim I actually don’t know the answer to that. I hadn’t really thought about it. After looking around a little bit, it looks like there’s not a clear answer: (http://en.wikipedia.org/wiki/GPL_license#Linking_and_derived_works).
For what it’s worth, I included the Ghostscript DLL in the Third Party folder as a convenience, but my code doesn’t touch the source for Ghostscript at all.
January 13th, 2011 at 6:32 am
Hi Mathew thanks great code but however, I’m also getting that InitAPI method returning a -100. using the gswin32.exe executable works fine but initialising the API in code seems to crash please could you help thanks
April 17th, 2011 at 10:48 pm
How do i use this for an .net web application?
Should i also reference the gsdll32.dll like i did with the ghostscriptshrap.dll?
May 9th, 2011 at 6:16 pm
[...] & GhostScript integration brought me to a project titled GhostscriptSharp by Matthew Ephraim (http://www.mattephraim.com/blog/2009/07/08/introducing-ghostscriptsharp/). The only thing that I needed for my project that wasn’t already implemented in [...]
June 24th, 2011 at 1:53 pm
Excellent post Matthew! Just wondering if it’s possible to use Ghostscript with streams rather than physically using files.
Thanks!
August 16th, 2011 at 9:08 am
Great class! I used your GenerateOutput method as I had problems with image sizes using your other methods as stated above. Even when I specified sizes of 100 and 100 it would still generate a very large image. Using the GhostscriptSettings constructor I was able to set properties and use the generateoutput method successfully with much more manageable image sizes. Works great!
string inputPath = userFormsPath + formId + “.pdf”;
string outputPath = userFormsPath + formId + “_%d.jpg”;
//int firstPage = 1;
//int lastPage = 3;
int imageWidth = 150;
int imageHeight = 150;
GhostscriptSettings s = new GhostscriptSettings();
s.Device = GhostscriptSharp.Settings.GhostscriptDevices.jpeg;
s.Page.AllPages = true;
//s.Page.Start = firstPage;
//s.Page.End = lastPage;
s.Resolution = new System.Drawing.Size(imageWidth, imageHeight);
GhostscriptSharp.Settings.GhostscriptPageSize pageSize = new GhostscriptSharp.Settings.GhostscriptPageSize();
pageSize.Native = GhostscriptSharp.Settings.GhostscriptPageSizes.a7;
s.Size = pageSize;
GhostscriptWrapper.GenerateOutput(inputPath, outputPath, s);
//GhostscriptWrapper.GeneratePageThumbs(inputPath, outputPath, firstPage, lastPage, imageWidth, imageHeight);
//GhostscriptWrapper.GeneratePageThumb(inputPath, outputPath, firstPage, imageWidth, imageHeight);