Suppressing the print dialog while using mswinpr2 in Ghostscript

224 views Asked by At

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

0

There are 0 answers