0

I am using Ghostscipt 9.54 (latest version) for printing a pdf file to my windows printer. I am using below command:

    C:\Users\Pradeep Gupta>"C:\Program Files\gs\gs9.54.0\bin\gswin64c.exe" -dBATCH -dNOPAUSE -dSAFER -dNumCopies=1 -sDEVICE=mswinpr2 -sOutputFile=%printer%"Everycom-58-Series" -f C:\PDF\5601001234040921211737.pdf
GPL Ghostscript 9.54.0 (2021-03-30)
Copyright (C) 2021 Artifex Software, Inc.  All rights reserved.
This software is supplied under the GNU AGPLv3 and comes with NO WARRANTY:
see the file COPYING for details.
Processing pages 1 through 1.
Page 1

This works fine and works as expected.

Now the same thing I am trying replicate in my .NET C# application. But the issue is that whenever I run my application on my Win 10 laptop, It is showing Print dialog to select the Printer. Please let me know what I am doing wrong as I am replicating the same argument as I am stating in the above mentioned command. Below are the code for my C# application:

private static bool DoGhostscriptPrint(string printerName, string pdfFilename)
        {
            try
            {
                String[] ghostScriptArguments = { "-dBATCH", "-dNOPAUSE", "-dSAFER",  "-dNoCancel",
                                                "-dNumCopies=1", "-sDEVICE=mswinpr2", String.Format("-sOutputFile=\"%printer%{0}\"", printerName),
                                                    "-f", pdfFilename};
                GhostScript.CallAPI(ghostScriptArguments);
            }
            catch (Exception ex)
            {
                Logger.Error("Unable to Print using Ghostscript: " + ex.Message + Environment.NewLine + ex.StackTrace);
            }
            return false;

        }

I am using the code from https://github.com/mephraim/ghostscriptsharp for calling Ghostscript with the args. Here is the code for Ghosctscript.CallAPI()

/// Calls the Ghostscript API with a collection of arguments to be passed to it
        /// </summary>
        public static void CallAPI(string[] args)
        {
            // Get a pointer to an instance of the Ghostscript API and run the API with the current arguments
     

   IntPtr gsInstancePtr;
        Logger.Debug("Acquiring Lock to call GS API with Args {0}", String.Join(",", args));
        lock (resourceLock)
        {
            Logger.Debug("Lock Acquired");
            GhostScriptNativeWrapper.CreateAPIInstance(out gsInstancePtr, IntPtr.Zero);
            try
            {
                int result = GhostScriptNativeWrapper.InitAPI(gsInstancePtr, args.Length, args);

                if (result < 0)
                {
                    throw new ExternalException("Ghostscript conversion error", result);
                }
            }
            finally
            {
                Logger.Debug("Lock Released");
                Cleanup(gsInstancePtr);
                Logger.Debug("GS Cleanup Done");
            }
        }
    }

InitAPI code is:

 [DllImport("gsdll64.dll", EntryPoint = "gsapi_init_with_args")]
        internal static extern int InitAPI(IntPtr instance, int argc, string[] argv);

I have copied the gsdll64.dll from Ghoscript installation. This issue I am facing since from last one week. There is no change in the application code since from a month.

Thanks & Regards, Pradeep Gupta

Pradeep
  • 1
  • 2
  • 1
    If you specify a printer in -sOutputFile, and yet the printer selection dialog still appears it means the mswinpr2 device couldn't find a printer by that name. You need to check precisely what is being sent to Ghostscript. If it works from the command line, then my suspicion is there is something wrong with the formatting of the string argument containing the printer name. – KenS Sep 11 '21 at 16:19
  • Hi, I have finally got the answer. My process was running under the SYSTEM user and that's why the ghostscript behavior. (Don't know why? but i believe it is due to the fact that A print command can be given only by the user and not the SYSTEM.) Once I ran the process under the current logged on user, it works perfectly fine. Thanks to all of you for your help. – Pradeep Sep 15 '21 at 10:39

0 Answers0