Click here to Skip to main content
15,510,087 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I tried to convert PDF to tif images using ImageMagick it works but it is very slow with large files. Now I tried to use ghostscript, it works fine from power-shell but it is not executing with c# asp.vet VS19.
Here is the command that works:
gswin64c.exe -dNOPAUSE -r300 -sDEVICE=tiffscaled24 -sCompression=lzw -dBATCH -sOutputFile="C:\Users\name\Desktop\images\Result\ %d.tif" "C:\Users\name\Desktop\images\DropHere\New folder\name.pdf" 

you need ghostscript exe and intallatipm folder to run this.
But my c# doesn't work. No error or anything it just go through the method step by step nothing happens.

What I have tried:

  public void ExtracImagesFromPdf(string fileName, string fileNameResultDirectory)
        {
string ghostScriptPath = @"C:\Program Files\gs\gs9.50\bin\gswin64.exe";
            String ars = " - dNOPAUSE -sDEVICE=jpeg -r102.4 -o" + "C:\\Users\\syousif\\Desktop\\images" + "%d.jpg -sPAPERSIZE=a4 " + fileNameResultDirectory;
            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();
}

OR
//  System.Environment.CurrentDirectory = @"C:\Program Files\gs\gs9.50\bin\";

           // String ars = "-dNOPAUSE -r300 -sDEVICE=tiffscaled24 -sCompression=lzw -dBATCH -sOutputFile=\"" + fileNameResultDirectory + "\" -%d.tif \"" + fileName + "\"";
           // string line = ".\\c:\\gswin64c.exe " + ars;

           //var  res = System.Diagnostics.Process.Start("gswin64c.exe", ars);

I also tried this way:

 public void ExtracImagesFromPdf(string fileName, string inputpath)
        {
GhostscriptPngDevice img = new GhostscriptPngDevice(GhostscriptPngDeviceType.Png16m);
            img.GraphicsAlphaBits = GhostscriptImageDeviceAlphaBits.V_4;
            img.TextAlphaBits = GhostscriptImageDeviceAlphaBits.V_4;
            img.ResolutionXY = new GhostscriptImageDeviceResolution(200, 200);
            img.InputFiles.Add(inputpath); 
            //img.Pdf.FirstPage = 1; 
            //img.Pdf.LastPage = 1;
            img.PostScript = string.Empty;
            img.OutputPath = @"C:\Users\name\Desktop\images\Result";
            img.Process();
}

But I get this error:
An error occured when call to 'gsapi_init_with_args' is made: -100 at the last line
Posted
Updated 7-Feb-20 7:11am
v3

C#
string ghostScriptPath = @"C:\Program Files\gs\gs9.50\bin\gswin64c.exe";
The executable name doesn't match in both cases.

Neither the command lines. The version which works uses -sOutputFile="...", whereas C# version uses something else.

Also maybe you need to replace the %d environment variable with the fileName parameter of the method?
 
Share this answer
 
Comments
AskalotLearnalot 6-Feb-20 12:10pm    
thank you for the feedback I tried this:
 String ars = "-dNOPAUSE -sDEVICE=jpeg -r102.4 -o" + @"C:\Users\syousif\Desktop\images\Result\" + "%d.jpg -sPAPERSIZE=a4" + "C:\\Users\\syousif\\Desktop\\images\\DropHere\\EuroTest\\name.pdf" ;
            string ghostScriptPath = @"C:\Program Files\gs\gs9.50\bin\gswin64c.exe";

same issue
phil.o 6-Feb-20 12:13pm    
The arguments do not seem correct. Please debug and watch carefully for the contents of this string; and double-validate its syntax is the proper one.
AskalotLearnalot 6-Feb-20 14:07pm    
I updated the question.
Most likely something wrong with your parameters. You have:
C#
String ars = " - dNOPAUSE -sDEVICE=jpeg -r102.4 -o" + "C:\\Users\\syousif\\Desktop\\images" + "%d.jpg -sPAPERSIZE=a4 " + fileNameResultDirectory;

I suspect the first hyphen character should not have a space after it. Should be " -dNOPAUSE ...
The filename "%d.jpg is probably invalid.
What is in fileNameResultDirectory ?
A quick check is to display all the parameters, or check them with the debugger while the program runs.

You can also capture the output from the running process, see Process.StandardOutput Property (System.Diagnostics) | Microsoft Docs[^].
 
Share this answer
 
Comments
AskalotLearnalot 6-Feb-20 12:10pm    
thank you for the feedback I tried this:
 String ars = "-dNOPAUSE -sDEVICE=jpeg -r102.4 -o" + @"C:\Users\syousif\Desktop\images\Result\" + "%d.jpg -sPAPERSIZE=a4" + "C:\\Users\\syousif\\Desktop\\images\\DropHere\\EuroTest\\name.pdf" ;            string ghostScriptPath = @"C:\Program Files\gs\gs9.50\bin\gswin64c.exe";

same issue. First path is output second one is input.
Richard MacCutchan 6-Feb-20 12:15pm    
And that will result in a parameter string of:
-dNOPAUSE -sDEVICE=jpeg -r102.4 -oC:\Users\syousif\Desktop\images\Result\%d.jpg -sPAPERSIZE=a4C:\\Users\\syousif\\Desktop\\images\\DropHere\\EuroTest\\name.pdf

which does not look right. As I suggested above, either print the final string or use your debugger to check it.
AskalotLearnalot 6-Feb-20 14:07pm    
I updated the question.
Richard MacCutchan 6-Feb-20 14:25pm    
That message means nothing. You will need to look at the Ghostscript documentation to find out what it refers to.
You could always try utilizing their API, which is documented:
The Ghostscript Interpreter Application Programming Interface (API)[^]

Someone here even wrote an article on how to do it this way, about a decade ago:
How To Convert PDF to Image Using Ghostscript API[^]
 
Share this answer
 
Comments
AskalotLearnalot 6-Feb-20 13:48pm    
I looked at it already and don't prefer that approach. Thank you for the suggestion.
Here is a solution that uses different approach but works, or at least haven't encountered error yet.
1) Install GhosotScript to your project from NuGet.
2) Install GhostScript to your CP :
version 9.26 from here https://github.com/ArtifexSoftware/ghostpdl-downloads/releases/download/gs926/gs926aw32.exe,
Newer version had bug in PageCount (always = 0 )
3)Apply this code:
public void ExtracImagesFromPdf(string fileName, string fileNameResultDirectory)
       {
           var xDpi = 300; //set the x DPI
           var yDpi = 300; //set the y DPI

           using (var rasterizer = new GhostscriptRasterizer()) //create an instance for GhostscriptRasterizer
           {

               rasterizer.Open(fileName); //opens the PDF file for rasterizing
               int PdfPages = rasterizer.PageCount;
               for (int pageNumber = 1; pageNumber <= rasterizer.PageCount; pageNumber++)
               {
                   //set the output image(png's) complete path
                   string outputPNGPath = Path.Combine(fileNameResultDirectory, "00" + pageNumber.ToString() + ".tif");


                   //converts the PDF pages to png's
                   Image pdf2PNG = rasterizer.GetPage(xDpi, yDpi, pageNumber);

                   //save the png's
                   pdf2PNG.Save(outputPNGPath, ImageFormat.Tiff);
               }



           }

Go from there :)
Credit:
Convert PDF to PNG using Ghostscript.NET - DotNetFunda.com[^]
GhostscriptRasterizer, Ghostscript.NET.Rasterizer C# (CSharp) Code Examples - HotExamples[^]
Myself
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900