0

I am trying to print a pdf in a c# console application, by starting a process. Instead of printing only 1 copy of the document it prints multiple copies (3, 4, 5 or 6 unpredictable). This is my code...

var p = new Process
{
    StartInfo = new ProcessStartInfo
    {
        CreateNoWindow = true,
        Verb = "PrintTo",
        Arguments = printerName,
        FileName = filePath
    }
};
p.Start();

Please can you tell me what I'm doing wrong?

kennyzx
  • 12,845
  • 6
  • 39
  • 83
abrull
  • 171
  • 2
  • 13
  • This is all the code? What if you print by using the `PrintDialog`, like [this](https://stackoverflow.com/a/5432909/815938)? Is only 1 copy is set in the print dialog? – kennyzx Jul 25 '17 at 03:01
  • @kennyzx I do not have a print dialogue. I want it to print automatically without the user having to click anything. – abrull Jul 25 '17 at 03:08
  • yes, I understand...But I can only think of the cause of this to be some incorrect settings of the printer, which can be examined in the print dialog. – kennyzx Jul 25 '17 at 03:15

1 Answers1

0

You can do that with this code.

 private void SendToPrinter(string prepDok)
    {
        ProcessStartInfo info = new ProcessStartInfo();
        info.Verb = "print";
        info.FileName = prepDok;
        info.CreateNoWindow = false;
        info.WindowStyle = ProcessWindowStyle.Hidden;

        System.Windows.Forms.PrintDialog pd = new System.Windows.Forms.PrintDialog();
        pd.PrinterSettings.Copies = 1;
        Process p = new Process();
        p.StartInfo = info;
        p.StartInfo.Arguments = pd.PrinterSettings.PrinterName;
        p.Start();
        p.CloseMainWindow();

        Thread.Sleep(4000);
        if (!p.WaitForExit(5000))
        {
            if (!p.HasExited)
            {
                p.Kill();
            }
        }

    }

This will set the number of copies to one and print dialog won't be shown.

Sashus
  • 411
  • 7
  • 8