.netghostscriptghostscriptsharp

Ghost Script pdf thumbnail in .NET


I want to display thumbnails of the uploaded pdf files on my website(ASP.NET). So far I have done following things.

  1. From this link i got the idea to use ghostscript How to generate thumbnail for some pages of a PDF file?

You could probably use one of the general-purpose PDF libraries: • Ghostscript - C, available under the GPL • Poppler - C++, available under the GPL • Adobe PDF Library SDK - expensive Google reveals quite a few PDF-to-image converters which you may be able to incorporate if one of the above options doesn't work.

  1. Then Generate a pdf thumbnail (open source/free) told me to go look for that mentioned wrapper

Matthew Ephraim released an open source wrapper for Ghostscript that sounds like it does what you want and is in C#. Link to Source Code: https://github.com/mephraim/ghostscriptsharp Link to Blog Posting: http://www.mattephraim.com/blog/2009/01/06/a-simple-c-wrapper-for-ghostscript/ You can make a simple call to the GeneratePageThumb method to generate a thumbnail (or use GeneratePageThumbs with a start and end page number to generate thumbnails for multiple seperate pages, with each page being a seperate output file), default file format is jpeg but you can change it, and many other options, by using the alternate GenerateOutput method call and specify options such as file format, page size, etc...

Now while following instructions of http://mattephraim.com/blog/2009/01/06/a-simple-c-wrapper-for-ghostscript/ I have installed ghostscript on my system which is windows 8 64-bit.

Now I've created a solution containing the test project by above guy and in my own project i am calling a function of his project

try
        {
            GhostscriptSharpTests.GhostscriptSharpTests ss = new GhostscriptSharpTests.GhostscriptSharpTests();
            ss.GenerateSinglePageThumbnail();
        }
        catch (Exception ex)
        { 

        }

but I am getting an exception :

Unable to load DLL 'gsdll32.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)


Solution

  • About the error:

    The error you are getting could be due gsdll32.dll could not be found or a wrong Ghostscript version installation you used. For a 64bit system you need to install 64bit Ghostscript library which has gsdll64.dll. If you compile your application for AnyCPU platfrom target, on a 64bit system it will run as 64bit process and you will need gsdll64.dll. If you compile your application as x86 and run it on a 64bit system, your application will run as 32bit process and you can use gsdll32.dll. When you use DllImport make sure that dll your are trying to call is in a same (bin) folder your applicatione executes or it can be in windows\system. If you want custom dll location, you can use full path in a DllImport ([DllImport("C:\Program Files\gs\gs9.14\bin\gsdll32.dll", EntryPoint = "gsapi_new_instance")]) which is normally not recommended.

    Why dont you simply use Ghostscript.NET library. It's a well tested native Ghostscript library wrapper which will allow you to do what you need and it's compatible with both x86 and x64 Ghostscript libraries.

    Sample code that shows you how to rasterize pdf to image is here: https://github.com/ArtifexSoftware/Ghostscript.NET/blob/master/Ghostscript.NET.Samples/Samples/RasterizerSample1.cs

    Try different (lower) values with "desired_x_dpi" and "desired_y_dpi" and output image will be smaller.