Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GhostscriptRasterizer.PageCount always returns zero

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);
                 }


            }
like image 654
Vasiliy Avatar asked Feb 03 '26 10:02

Vasiliy


1 Answers

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...

like image 172
Oleksandr Tyshchenko Avatar answered Feb 05 '26 01:02

Oleksandr Tyshchenko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!