-1

I have a PDF file and I want it to print on click of a button. Below is the code for the same -

private void SendToPrinter(string filename)
    {
        using (PrintDialog Dialog = new PrintDialog())
        {
            Dialog.ShowDialog();

            ProcessStartInfo printProcessInfo = new ProcessStartInfo()
            {
                Verb = "print",
                CreateNoWindow = true,
                FileName = filename,
                WindowStyle = ProcessWindowStyle.Hidden
            };

            Process printProcess = new Process();
            printProcess.StartInfo = printProcessInfo;
            printProcess.StartInfo.Arguments = Dialog.PrinterSettings.PrinterName;
            printProcess.Start();

            printProcess.WaitForInputIdle();

            Thread.Sleep(3000);

            if (false == printProcess.CloseMainWindow())
            {
                printProcess.Kill();
            }
        }
    }

The above code opens the popup for PrintDialog but whatever printer I select, it uses the default printer.

Any ideas?

Ranjan Sikdar
  • 21
  • 1
  • 8

1 Answers1

2

the windows print command has got the following syntax:

Prints a text file.
PRINT [/D:device] [[drive:][path]filename[...]]
   /D:device   Specifies a print device.

So you need to change your argument to:

printProcess.StartInfo.Arguments = $"/D:\"{Dialog.PrinterSettings.PrinterName}\"" ;

But please note, that it is build for printing text files, not pdf.

dsdel
  • 1,042
  • 1
  • 8
  • 11
  • How can I print PDF files using C#? Any other ideas? Also, the final product can be used in Windows 7/8/10, so can't make this change. – Ranjan Sikdar Mar 16 '18 at 10:20
  • I am sorry, the 'print' is also available by Windows 7 (modified my post). Printed once by invoking the adobe acrobat reader resulting in many acrobat processes which did not end. You can refer to this [post](https://stackoverflow.com/questions/19124808/printing-pdfs-from-windows-command-line) which does have more explanation and solutions about printing a pdf. – dsdel Mar 16 '18 at 10:23
  • I have tried this but its not working. Just to add to it, I am trying to print via a barcode printer connected to USB003 port – Ranjan Sikdar Mar 16 '18 at 10:24
  • By barcode printer you mean some style of label printers (eg. zebra label printer)? If yes, these have mostly their own printer language where you cannot simply print a pdf only by using additional addons like from Zebra [here](https://km.zebra.com/kb/index?page=content&id=SO8279&actp=LIST_POPULAR) – dsdel Mar 16 '18 at 10:28
  • yes, exactly this one - [link here](https://www.hprt.com/Product/Label-Printer/HT300/HT330.html) – Ranjan Sikdar Mar 16 '18 at 10:32
  • This printer is using the zpl programming language - I am not common with this manufacture therefor I cannot give you exact information about this model. However does printing a pdf directly to the printer work if you issue it manually? If so, you can follow the link of the above comment to the pdf printing. If not, you would need to create a label using zpl where you replace the data and then simply sent it to the printer eg. using file.copy – dsdel Mar 16 '18 at 10:37