【问题标题】:print pdf with selected printer from dropdown使用下拉列表中选择的打印机打印 pdf
【发布时间】:2016-11-15 06:04:58
【问题描述】:

大家好,我正在尝试打印 pdf 文件。为此,我创建了一个 Windows 窗体应用程序。如果我使用系统的默认打印机,我的应用程序会打印 pdf。但问题是,当我尝试使用从下拉列表中选择的打印机打印 PDF 文件时,我的代码不起作用。

 private bool SendToPrinter(string filePath, string filename)
    {
        bool Status = false;
        string PrinterName;
        string PaperName;
        PrinterName = "";
        try
        {
            PrinterName = Convert.ToString(cmbPrinterList.SelectedItem);
            // Set the printer.
             AddPrinterConnection(PrinterName);
             SetDefaultPrinter(PrinterName);
            //Print the file with number of copies sent.
            ProcessStartInfo info = new ProcessStartInfo();
            info.Verb = "print";
            info.Arguments = "\"" + PrinterName + "\"";
            info.FileName = @filePath + @"\" + filename;
            info.CreateNoWindow = false;
            info.WindowStyle = ProcessWindowStyle.Hidden;
            info.UseShellExecute = true;

            Process p = new Process();
            p.StartInfo = info;
            p.Start();
            p.WaitForInputIdle();                
            System.Threading.Thread.Sleep(2);
            if (false == p.CloseMainWindow())
                p.Kill();
            Status = true;
        }
        catch (Exception ex)
        {

        }
        return Status;
    }

    //Set the added printer as default printer.
    [DllImport("winspool.drv", CharSet = CharSet.Auto, SetLastError = true)]
    public static extern bool SetDefaultPrinter(string Name);

   //Add the printer connection for specified pName.
   [DllImport("winspool.drv")]
    public static extern bool AddPrinterConnection(string pName);
    public bool viewTabOrder = true;

【问题讨论】:

  • 请说明您的错误。你的代码不起作用是什么意思。
  • @active92 我想说我的代码没有用我选择的打印机打印 pdf 文件
  • @active92 但使用系统默认打印机

标签: c# winforms printing


【解决方案1】:

试试下面的代码 这对我来说正在使用不同的打印机。希望对你也有帮助:)

public void SendToPrinter(string filePath, string Printer)
        {
            try
            {
                Process proc = new Process();
                proc.Refresh();
                proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                proc.StartInfo.Verb = "printto";
                proc.StartInfo.FileName = ConfigurationManager.AppSettings["ReaderPath"].ToString();                    
                proc.StartInfo.Arguments = String.Format("/t \"{0}\" \"{1}\"", filePath, Printer);
                proc.StartInfo.UseShellExecute = false;
                proc.StartInfo.CreateNoWindow = true;
                proc.Start();
                proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                if (proc.HasExited == false)
                {
                    proc.WaitForExit(20000);
                }
                proc.EnableRaisingEvents = true;
                proc.Close();
            }
            catch (Exception e)
            {
            }
        }

将打印机绑定到下拉列表的代码。

cmbPrinter.Items.Insert(0, "--Select Printer--");

for (int i = 0; i < PrinterSettings.InstalledPrinters.Count; i++)
{
     var pkInstalledPrinters = PrinterSettings.InstalledPrinters[i];
     cmbPrinter.Items.Insert(i + 1, pkInstalledPrinters);
}

并传递如下参数

SendToPrinter(FilePath,cmbPrinter.SelectedItem.ToString());

【讨论】:

  • 感谢您的回答,但这是发送副本以供默认打印机打印:(
  • 您是否尝试将打印机名称传递给此方法的打印机参数?
  • 那么我认为问题出在打印机名称的下拉绑定中,让我添加代码来绑定打印机下拉列表并将选定的打印机名称传递给此方法。
  • 谢谢,我今天会检查并通知您
  • 再次对我不起作用,它发送打印我的默认打印机以测试我的硬代码PrinterName = @"\\server1\HP DeskJet 5820 series";,但它发送到\\pc22\HP LaserJet 1020,这是我的默认打印机。你能给我建议吗
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-11-26
  • 2021-11-02
  • 2016-10-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-08
相关资源
最近更新 更多