11

Hi I have been using itextSharp for all pdf related projects in dot.net. I came across a requirement where I need to convert PDF pages to images. I could not find any sample of such a thing. I found that another tool ghostscript is able to do it the problem with that is I am on a shared hosting & I don't think ghostscript will run on server as in my local machine I had to manually copy ghost script dlls to system32 folder which is not possible in a shared hosting.

Max
  • 470
  • 2
  • 9
  • 22
  • It is not possible to convert PDF page to Image using iTextSharp.. You need to use some java script library... You can give a try with Phantomjs .. it is good for this purpose as i have used it. – TheKingPinMirza May 26 '15 at 10:49
  • The question is unclear, so I'd suggest rephrasing it into something that's actually answerable. If you just need the GhostScript DLLs, you may find that copying them to the `bin` folder will be sufficient for you to access their functionality on your hosted system. – Adrian Wragg May 26 '15 at 10:52
  • Refer below link http://stackoverflow.com/questions/23905169/how-to-convert-pdf-files-to-image – JDK May 26 '15 at 10:52
  • Hi I think Phantomjs will not work with asp.net, will it? I need something which can work with asp.net c#. – Max May 26 '15 at 10:52
  • You can take a look at this question: http://stackoverflow.com/questions/10125117/convert-pdf-file-pages-to-images-with-itextsharp – Wessel T. May 26 '15 at 10:53
  • @Max, it will work. give a try. – TheKingPinMirza May 26 '15 at 10:54
  • I have the Ghostscript DLLs, the problem is that it works on local machine since I have manually copied the DLLs to system32 folder but not on server which is a shared hosting. – Max May 26 '15 at 10:55
  • @Max Have you tried copying them to `bin` instead, alongside your application's own DLLs? – Adrian Wragg May 26 '15 at 11:15
  • Yes I have copied the ghost script dll's to bin folder. It works fine in local system, when I upload to server it gives an error regarding ghost-script dll. – Max May 26 '15 at 11:25
  • Try to use http://github.com/jhabjan/Ghostscript.NET – HABJAN May 26 '15 at 11:32
  • @Max What was the error (by the way, nobody will be notified of a reply unless you @-them)? – Adrian Wragg May 26 '15 at 11:54
  • @AdrianWragg- the error says 'Unable to load DLL 'gsdll32.dll'. I searched it and people suggested to copy the dll to system32 folder. However I am on a shared hosting hence can't do that. – Max May 26 '15 at 12:07
  • @Max Your question appears to have morphed into http://stackoverflow.com/questions/12287445/converting-a-pdf-to-an-image-with-ghostscript-how-do-i-reference-gsdll32-dll which mentions that exact error, so I'm going to flag it as a duplicate. – Adrian Wragg May 26 '15 at 12:27

1 Answers1

15

Ok I searched all over and found out that there is a nuget package for Ghost Script, so problem for me was solved by going to package manager console and adding ghost script to fresh project (I created a fresh project since the old one had all kinds of reference to win32 ghostscript dlls) by "PM> Install-Package Ghostscript.NET". So the answer to my question is: 1.> itextSharp cannot directly convert PDF pages to image. 2.> The "Ghostscript.NET 1.2.0" does it quite easily. Following is a code example.

    public void LoadImage(string InputPDFFile,int PageNumber)
    {

        string outImageName = Path.GetFileNameWithoutExtension(InputPDFFile);
        outImageName = outImageName+"_"+PageNumber.ToString() + "_.png";


        GhostscriptPngDevice dev = new GhostscriptPngDevice(GhostscriptPngDeviceType.Png256);
        dev.GraphicsAlphaBits = GhostscriptImageDeviceAlphaBits.V_4;
        dev.TextAlphaBits = GhostscriptImageDeviceAlphaBits.V_4;
        dev.ResolutionXY = new GhostscriptImageDeviceResolution(290, 290);
        dev.InputFiles.Add(InputPDFFile);
        dev.Pdf.FirstPage = PageNumber;
        dev.Pdf.LastPage = PageNumber;
        dev.CustomSwitches.Add("-dDOINTERPOLATE");
        dev.OutputPath = Server.MapPath(@"~/tempImages/" + outImageName);
        dev.Process();

    }
Max
  • 470
  • 2
  • 9
  • 22