4
votes

I'm trying to create an image from a pdf using GhostScript. Here is my code:

GhostscriptWrapper.ConvertToBMP(inputPDFFilePath, outputBMPFilePath);

And here's my GhostscriptWrapper class:

public class GhostscriptWrapper
{
    public static void ConvertToBMP(string inputPath, string outputPath)
    {
        CallAPI(GetArgs(inputPath, outputPath));
    }

    private static void CallAPI(string[] args)
    {
        IntPtr ptr;
        CreateAPIInstance(out ptr, IntPtr.Zero);
        InitAPI(ptr, args.Length, args);
        Cleanup(ptr);
    }

    private static void Cleanup(IntPtr gsInstancePtr)
    {
        ExitAPI(gsInstancePtr);
        DeleteAPIInstance(gsInstancePtr);
    }        

    [DllImport("gsdll32.dll", EntryPoint="gsapi_new_instance")]
    private static extern int CreateAPIInstance(out IntPtr pinstance,
        IntPtr caller_handle);

    [DllImport("gsdll32.dll", EntryPoint="gsapi_delete_instance")]
    private static extern void DeleteAPIInstance(IntPtr instance);

    [DllImport("gsdll32.dll", EntryPoint="gsapi_exit")]
    private static extern int ExitAPI(IntPtr instance);                

    [DllImport("gsdll32.dll", EntryPoint="gsapi_init_with_args")]
    private static extern int InitAPI(IntPtr instance, int argc,
        string[] argv);

    private static string[] GetArgs(string inputPath, string outputPath)
    {
        return new string[] { "-dNOPAUSE", "-dBATCH", "-dSAFER",
            "-dTextAlphaBits=4", "-dGraphicsAlphaBits=4", "-sDEVICE=bmp16m",
             string.Format("-r{0}x{1}", 0x48, 0x48), "-dEPSCrop",
             string.Format("-sOutputFile={0}", outputPath), inputPath };
    }
}

My problem is that when I run my code on my page, I get this error:

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

I have the actual dll file, I thought maybe I just need to add a reference to my bin folder, but when I try that, I get this error:

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

So I'm kind of stuck - I have the dll, but I have no idea how to reference it. Anyone know what I need to do?

4
what was the solution? This worked just fine yesterday for me. I then moved the directory of the entire site and now nothing. I can use the package manager console but when I deploy to the server I will need it to work there. - Mike
@Mike Here is an article on how to resolve this error.link . I am sure it will resolve your problem. - Aamerallous

4 Answers

5
votes

In Package Manager Console type: Install-Package Ghostscript.Net

2
votes

As I understand it, you can't just 'add a reference' to a DLL, unless possibly the DLL was written for C# or .NET, which Ghostscript is not, its written in C.

You need to use the Win32 API call 'LoadLibrary' or whatever the C#/.NET equivalent is.

Your first error looks like the DLL simply can't be found, have you got a copy of the DLL in the current directory when you start the application ?

2
votes

Try with complete path of dll instead of only name. Like if your dll kept at

D:\TestApplication\bin\gsdll32.dll

then,

[DllImport("gsdll32.dll", EntryPoint="gsapi_new_instance")]

above statement will be

[DllImport("D:\\TestApplication\\bin\\gsdll32.dll", EntryPoint="gsapi_new_instance")].