3

This problem has already been discussed here:GhostscriptRasterizer Objects Returns 0 as PageCount value But the answer to this question did not help me solve the problem.

In my case, it doesn’t help from kat to an older version of Ghostscript. 26 and 25. I always have PageCount = 0, and if the version is lower than 27, I get an error "Native Ghostscript library not found."

private static void PdfToPng(string inputFile, string outputFileName)
            {
                var xDpi = 100; //set the x DPI
                var yDpi = 100; //set the y DPI
                var pageNumber = 1; // the pages in a PDF document

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

                         rasterizer.Open(inputFile); //opens the PDF file for rasterizing

                        //set the output image(png's) complete path
                        var outputPNGPath = Path.Combine(outputFolder, string.Format("{0}_Page{1}.png", outputFileName,pageNumber));

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

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

                        Console.WriteLine("Saved " + outputPNGPath);
                 }


            }
Vasiliy
  • 331
  • 1
  • 12

1 Answers1

3

I was struggling with the same problem and ended up using iTextSharp just to get the page count. Below is a snippet from the production code:

using (var reader = new PdfReader(pdfFile))
{
    //  as a matter of fact we need iTextSharp PdfReader (and all of iTextSharp) only to get the page count of PDF document;
    //  unfortunately GhostScript itself doesn't know how to do it
    pageCount = reader.NumberOfPages;
}

Not a perfect solution but this is exactly what solved my problem. I left that comment there to remind myself that I have to find a better way somehow but I’ve never bothered to come back because it just works fine as it is...

PdfReader class is defined in iTextSharp.text.pdf namespace.

And I'm using Ghostscript.NET.GhostscriptPngDevice instead of GhostscriptRasterizer to rasterize the specific page of PDF document.

Here is my method that rasterizes the page and saves it to PNG file

private static void PdfToPngWithGhostscriptPngDevice(string srcFile, int pageNo, int dpiX, int dpiY, string tgtFile)
{
    GhostscriptPngDevice dev = new GhostscriptPngDevice(GhostscriptPngDeviceType.PngGray);
    dev.GraphicsAlphaBits = GhostscriptImageDeviceAlphaBits.V_4;
    dev.TextAlphaBits = GhostscriptImageDeviceAlphaBits.V_4;
    dev.ResolutionXY = new GhostscriptImageDeviceResolution(dpiX, dpiY);
    dev.InputFiles.Add(srcFile);
    dev.Pdf.FirstPage = pageNo;
    dev.Pdf.LastPage = pageNo;
    dev.CustomSwitches.Add("-dDOINTERPOLATE");
    dev.OutputPath = tgtFile;
    dev.Process();
}

Hope that would help...

  • The fact is that not only does not determine the number of pages for me, the method GetPage (xDpi, yDpi, pageNumber) works for me, when I myself specify pageNumber = 1. – Vasiliy Aug 29 '19 at 14:09
  • I’m sorry I’m not quite sure that I fully understand you. Can you clarify – does you code line `var pdf2PNG = rasterizer.GetPage(xDpi, yDpi, pageNumber); ` work or not with `pageNumber == 1` ? – Oleksandr Tyshchenko Aug 29 '19 at 14:21
  • All right, apparently I misunderstood your initial question. I thought that you problem was that you always obtaining 0 from rasterizer.PageCount as described in the referred topic (https://stackoverflow.com/questions/56205425/ghostscriptrasterizer-objects-returns-0-as-pagecount-value) I've just checked my code and it seems that I had problems with `rasterizer.GetPage(xDpi, yDpi, pageNumber` call too. Probably that was the reason I ended up with using `Ghostscript.NET.GhostscriptPngDevice` instead of `GhostscriptRasterizer`. – Oleksandr Tyshchenko Aug 29 '19 at 14:36
  • I will update my answer and include there my method called `PdfToPngWithGhostscriptPngDevice` that apparently does exactly what you are trying to achieve. Hopefully that would help... – Oleksandr Tyshchenko Aug 29 '19 at 14:36
  • Alexander, how can I use GhostscriptPngDevice to convert a page of a pdf file into memory, i.e. without saving the resulting png file to disks, for example, getting a byte array? – Vasiliy Aug 30 '19 at 08:48
  • As far as I know there is no way to write images generated with GhostscriptPngDevice to memory stream or other in memory media. Apparently it only allows to write them do disk file. – Oleksandr Tyshchenko Sep 01 '19 at 19:03
  • I am saving the content to a temp, and then, I delete. So far so good... – rodrigocl Jan 09 '20 at 22:58