在Winforms

时间:2018-03-16 09:37:41

标签: c# winforms printing printdocument

我有一个PDF文件,我想要点击按钮进行打印。下面是相同的代码 -

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();
            }
        }
    }

以上代码打开 PrintDialog 的弹出窗口,但无论我选择什么打印机,它都会使用默认打印机。

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

windows print命令具有以下语法:

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

所以你需要将你的论点改为:

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

但请注意,它是用于打印文本文件的构建,而不是pdf。