从下拉菜单中选择打印机打印PDF

本文关键字:打印 PDF 打印机 选择 下拉菜单 | 更新日期: 2023-09-27 18:18:08

大家好,我想打印一个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;

从下拉菜单中选择打印机打印PDF

试试下面的代码这对我来说是在不同的打印机上工作。希望对你也有帮助:)

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)
            {
            }
        }

将printer绑定到下拉列表的代码。

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