Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GhostscriptRasterizer Objects Returns 0 as PageCount value

            txtStatus.Text = "";
            if (!File.Exists(txtOpenLocation.Text))
            {
                txtStatus.Text = "File Not Found";
                return;
            }

            txtStatus.Text = "File Found";



            const string DLL_32BITS = "gsdll32.dll";
            const string DLL_64BITS = "gsdll64.dll";

            //select DLL based on arch
            string NomeGhostscriptDLL;
            if (Environment.Is64BitProcess)
            {
                NomeGhostscriptDLL = DLL_64BITS;
            }
            else
            {
                NomeGhostscriptDLL = DLL_32BITS;
            }




            GhostscriptVersionInfo gvi = new GhostscriptVersionInfo(NomeGhostscriptDLL);
            var rasterizer = new GhostscriptRasterizer();
            try
            {              
                rasterizer.Open(txtOpenLocation.Text, gvi, true);

                Console.WriteLine(rasterizer.PageCount); //This line always prints 0
            } catch(Exception er)
            {
                txtStatus.AppendText("\r\nUnable to Load the File: "+ er.ToString());
                return;
            }

I have googled on it, but got no solution, and no helpful documentation about the rasterizer.Open() function.

The Console.WriteLine(rasterizer.PageCount); always prints 0, regardless which pdf file I load.

txtStatusis a multiline TextBox in UI. txtOpenLocation is another TextBox in UI, non-editable by user, and its value is set by a OpenFileDialog.

I am using Visual Studio 2019 Community Edition.

Another observation I feel worth mentioning— for every pdf file on my machine, when I try to open any pdf file with either Adobe Acrobat DC or Foxit Reader, first the reader crashes, becomes 'not responsive' for about 10 to 15 seconds and then it opens the pdf file.

like image 364
Muhammad Rasel Parvej Avatar asked May 19 '19 06:05

Muhammad Rasel Parvej


3 Answers

I had the same problem yesterday, I downloaded version 9.26 from here https://github.com/ArtifexSoftware/ghostpdl-downloads/releases/download/gs926/gs926aw32.exe, and works!

I think this is a bug of ghostscript 9.27 release.

like image 151
Oswaldo Cotes Solano Avatar answered Nov 03 '22 01:11

Oswaldo Cotes Solano


This isn't a bug at all, I suspect, (I certainly do not believe its a Ghostscript bug) but its probably a change in behaviour. Due to reported security vulnerabilities the Ghostscript developers have been removing access to many non-standard PostScript extensions (unique to Ghostscript). Most recently access to the dictionary for processing PDF files has been secured.

My suspicion is that Ghostscript.NET (which is not maintained by the Ghostscript developers) is using one or more non-standard extensions to do the work of retrieving the count of pages. Without knowing what exactly is being used currently I can't be sure of course.

If the developer of Ghostscript.NET would like to contact us and confirm this is the problem then we can discuss the currently supported method for retrieving the count of pages in a PDF file.

It won't help at all to send me a project using Ghostscript.NET, since I don't know anything about it. I'm also not a C# or .NET developer, so the code would likely be meaningless to me.

Ghostscript returns considerable information on the back channel, stdout and/or stderr. These can be redirected to an application-defined data sink. I imagine that Ghostscript.NET will give you some means to retrieve these and if you plan to do any real development involving Ghostscript then I would very strongly reccomend that you find out how to get this information.

When you say 'no error is thrown from Ghostscript' I think you may be confusing Ghostscript and Ghostscript.NET. Without seeing the back channel from Ghostscript I don't see how you can tell if Ghostscript is generating an error.

NB if you plan to distribute your application you must abide by the terms of the AGPL version 3 (which is the license applying to Ghostscript), and that includes shipping a copy of the license, and some means for informing users where they can get the original.

like image 21
KenS Avatar answered Nov 02 '22 23:11

KenS


As with the OP and the primary answer to this question, I too encountered this exact issue just yesterday.

I just want to add that for me the suggested version of ghostscript (9.26) wasn't working. It complained that I should be using a 64 bit version.

For those who need that, it's here: https://github.com/ArtifexSoftware/ghostpdl-downloads/releases/download/gs926/gs926aw64.exe

I had to just guess at the URL. I'm amazed at how difficult it has been to find older versions.

like image 27
Stuart Aitken Avatar answered Nov 03 '22 01:11

Stuart Aitken