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

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

  15. GuiSim Says:

    Since your library is licensed under the MIT license and you use the ghostscript dll.. doesn’t that violate’s GhostScript’s GPL license ?

  16. Matthew Ephraim Says:

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

  17. jeremy Says:

    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

  18. Mads Boye Says:

    How do i use this for an .net web application?
    Should i also reference the gsdll32.dll like i did with the ghostscriptshrap.dll?

  19. PDF manipulation in C# using GhostscriptSharp « Abnormal thoughts Says:

    [...] & 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 [...]

  20. Dennis Vasek Says:

    Excellent post Matthew! Just wondering if it’s possible to use Ghostscript with streams rather than physically using files.

    Thanks!

  21. Michael R. Brown Says:

    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);

Leave a Reply