Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting a pdf to an image with GhostScript - how do I reference gsdll32.dll?

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?

like image 304
Steven Avatar asked Dec 27 '22 18:12

Steven


1 Answers

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

like image 117
Filipe Avatar answered Dec 29 '22 10:12

Filipe