How to use Ghostscript for converting PDF to Image

后端 未结 5 1746
轻奢々
轻奢々 2021-01-01 04:42

I found that Ghostscript is able to convert PDF to Image format.

I tried PDF to Image Converter but not able to understand it clearly.

I have installed

相关标签:
5条回答
  • 2021-01-01 05:07

    You Do not need to add any DLL reference to your project. First download the gs910w32.exe application file then install it to your local computer. Get the location of the installed .exe file eg:-

    "C:\Program Files (x86)\gs\gs8.64\bin\gswin32.exe"

    use it in your C# application as :

     
      private void PdfToJpg(string inputPDFFile, string outputImagesPath)
            {
                string ghostScriptPath = @"C:\Program Files (x86)\gs\gs8.64\bin\gswin32.exe";
                String ars = "-dNOPAUSE -sDEVICE=jpeg -r102.4 -o" + outputImagesPath + "%d.jpg -sPAPERSIZE=a4 " + inputPDFFile;
                Process proc = new Process();
                proc.StartInfo.FileName = ghostScriptPath;
                proc.StartInfo.Arguments = ars;
                proc.StartInfo.CreateNoWindow = true;
                proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                proc.Start();
                proc.WaitForExit();
            }
     

    IF your input PDF file name has any spaces, you need to change the argument to

     
    String ars = "-dNOPAUSE -sDEVICE=jpeg -r102.4 -o" + outputImagesPath + "%d.jpg -sPAPERSIZE=a4 " +"\"" + inputPDFFile + "\"";
     

    you can specify the output image's aspect ratio in the argument with the -r flag. If you use "-r300" the width of the image will be 3000 pixel and height will change accordingly, from the above argument you will get 1024 to 768 size jpg image.

    0 讨论(0)
  • 2021-01-01 05:10

    You can use C# to run the GhostScript command line or use Platform Invoke (pInvoke) calls to call the GhostScript dll directly.

    GhostScript is primarily file based, so the input is path to a file on disk and the output is the creation of files on disk. The parameters used to call either the dll or exe are basically the same, so there is not a huge benefit to calling the dll directly, but does make for nicer code.

    I have C# wrapper that can be used to call the ghostscript dll, if you email me (address on profile) I will sent it to you.

    HTH

    UPDATE:

    code repo moved to https://bitbucket.org/brightertools/ghostscript

    0 讨论(0)
  • 2021-01-01 05:14

    The gsdll32.dll file is not a managed .NET library. You can't reference it in your project. You have to include it in your project as "content" (menu: Add existing item) and let VS copy it to the output directory. Meanwhile you should read the Ghostscript API docs and this article on PInvoke.net on how to reference the Ghostscript functions.

    Keep in mind that Ghostscript is all unmanaged code and that you have to do the clean-up yourself after using the library.

    Edit: What Robert said is important, too. Of course, you have to use the correct version of the Ghostscript library.

    0 讨论(0)
  • 2021-01-01 05:20

    you need to run the below command to reference the library http://www.nuget.org/packages/GhostScriptSharp/

    VS2012 --> Tools --> Library Package Manager --> Package Manager Console

    0 讨论(0)
  • 2021-01-01 05:27

    Why do you try to add the library as reference to your project? gsdll32.dll is a native dll, not a Dot-Net library.

    When I build the sample project using Visual C# Express 2010 I get an exe file. If I execute it it tries to access the gsdll32.dll. The problem is now that on a 64bit system a 64bit executable is generated but the gsdll32.dll is compiled for 32bit.

    The correct solution would be to modify the source code and replace gsdll32.dll with gsdll64.dll everywhere it occurs. The simpler solution is to use the 64 bit version of Ghostscript, copy the gsdll64.dll into the same directory as the ConvertPDF.exe and rename it to gsdll32.dll. This definitely works - just tested and converted a PDF to TIFF.

    0 讨论(0)
提交回复
热议问题