1

I have a Sharepoint application that uses the Ghostscript.net wrapper to rasterize pdf documents to png. Right now I am using the example from their site. But the issue I have is when I try to convert to pdfs at the same time. Using this code works only one at a time. But I get the error "An error occured when call to 'gsapi_new_instance' is made: -100" when I attempt to convert 2 pdf's simultaneously.

               using (MemoryStream pdfStream = new MemoryStream(pdfbyte))
            using (GhostscriptRasterizer rasterizer = new GhostscriptRasterizer())
            {

                rasterizer.Open(pdfStream, version, false);
                for (int i = 1; i <= rasterizer.PageCount; i++)
                {

                    using (MemoryStream ms = new MemoryStream())
                    {
                        DrawImage img = rasterizer.GetPage(dpi, dpi, i);
                        img.Save(ms, ImageFormat.Png);
                        ms.Close();
                        output = "data:image/png;base64," + Convert.ToBase64String((byte[])ms.ToArray());

                    }

                }

                rasterizer.Close();

Otherwise if I use rasterizer.Open(pdfStream, version, true); I the error "Arithmetic operation resulted in an overflow"

Should I be using a GhostscriptProcessor or Viewer instance instead? Does anyone have a good example of this code?

Community
  • 1
  • 1
RobbZ
  • 1,410
  • 14
  • 19

1 Answers1

1

So I figured out what the problem is. I had to compile Ghostscript.net for 3.5 because I am using it in a Sharepoint 2010 site. When I did this I screwed up this line of code in the DynamicNativeLibrary.cs file.

string procName = Marshal.PtrToStringAnsi((IntPtr)(byte*)(thunkData) + 2);

When I changed it to the following it seems to work without a problem now.

IntPtr a = (IntPtr)(byte*)(thunkData);
string procName = Marshal.PtrToStringAnsi(new IntPtr(a.ToInt64() + 2));
RobbZ
  • 1,410
  • 14
  • 19
  • Thats great to know you found your answer. I'm developing a HoloLens app using Unity and the solution in VS says the target framework is .net 3.5. I can use gsdll64 in the Unity Editor no prob, but when I try to import the Dll in the UWP app (gsdll32), it returns a dllnotfound exception. I'm thinking this is because the ghost script dll is not compatible with UWP apps? Continually looking into this to uncover definite answers. Just hoping to contact you and see how you did it first. Thanks! – jtth Jul 12 '17 at 14:57
  • I downloaded the source and changed the targeted framework down from 4.x to 3.5. I then fixed all of the errors that showed up because of 4.x specific code. I think this is the only one what was slightly difficult to fix. – RobbZ Aug 03 '17 at 14:42
  • Gotcha. I ended up acquiring a gsdll32 that was compiled for WinRT. Thanks for the reply though! – jtth Aug 03 '17 at 14:50
  • I forgot to add that when I tried to use Ghostscript NET as opposed to Ghostscript it returned a plethora of errors so I just backed off – jtth Aug 03 '17 at 14:51