0

I'm trying to send a pdf to the printer without the print dialog coming up using GhostScript.NET. My understanding is if I provide the exact name of the printer in the -sOutputFile switch, the user will not be prompted..

The exact name of my printer is 101-XER4250, and in debugging, the name that the processor receives is: "-sOutputFile=%printer%101-XER4250-E". Is there something that I am missing regarding this? Also if it's worth mentioning, I'm using a Xerox machine with PCL6 drivers.

Here's my example code:

private static void PrintWithGSNET(byte[] pdfFormBytes, string printer, int copies)
    {
        try
        {
            var fileName = @"c:\temp\" + $"{DateTime.Now:yyyyMMddhhmmssffff} - {Security.CurrentUser}";
            using (var file = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite))
            {
                file.Write(pdfFormBytes, 0, pdfFormBytes.Length);
                using (GhostscriptProcessor processor = new GhostscriptProcessor(GhostscriptVersionInfo.GetLastInstalledVersion(), true))
                {
                    List<string> switches = new List<string>();
                    switches.Add("-empty");
                    switches.Add("-dPrinted");
                    switches.Add("-dBATCH");
                    switches.Add("-dPDFFitPage");
                    switches.Add("-dNOPAUSE");
                    switches.Add("-dNOSAFER");
                    switches.Add("-dNOPROMPT");
                    switches.Add("-dQUIET");
                    switches.Add("-sDEVICE=mswinpr2");
                    switches.Add("-sOutputFile=%printer%" + printer.Trim());
                    switches.Add("-dNumCopies=1");


                    switches.Add(fileName);
                    processor.StartProcessing(switches.ToArray(), null);
                }
                file.Close();
            }
            File.Delete(fileName);
        }
        catch (Exception ex)
        {
            throw new InvalidOperationException($"Error printing. [Printer: {printer.Trim()}] [Copies: {copies}", ex);
        }
    }
Crumblenautjs
  • 169
  • 1
  • 3
  • 24
  • You say that the printer name is "101-XER4250" but the -sOutputFile is "%printer%101-XER4250-E", so clearly that's not the same name (assuming you didn't make a typo there). If the mswinpr2 device can't find the device then it will open the print dialog, otherwise it doesn't know where to send the output.... – KenS Feb 25 '17 at 09:15

2 Answers2

0

If you want the user should not notice that the file is being printed you can add the no cancel command switches.Add("-dNoCancel");

Shloime Rosenblum
  • 927
  • 11
  • 26
-1

Is it a network printer? I had to qualify my printer name with the server name.

If I set the printer to be "printerName" the dialog popped up. When I changed it to @"\servername\printerName" the dialog went away and it printed silently.

Zoey
  • 226
  • 2
  • 13