matthew ephraim

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: ,

14 Responses to “Introducing GhostscriptSharp”

  1. Martin Bour Says:

    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.

  2. flalar Says:

    Any thought to use this in a x64 environment?

  3. Matthew Ephraim Says:

    @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.

  4. Charles Crolley Says:

    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!

  5. Dash Says:

    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.

  6. Milky Joe Says:

    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?

  7. Milky Joe Says:

    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?

  8. Matthew Ephraim Says:

    @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.

  9. Bob Says:

    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!!

  10. Mehul Says:

    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

  11. Jerry Says:

    The output image size is 10x times larger. I set 100×100, but got 978×1023. Why?

  12. Matthew Says:

    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

  13. Robert Says:

    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.

  14. Robert Says:

    Nevermind…Like I said, dumb question.

Leave a Reply