【问题标题】:How to print a pdf file using c#如何使用c#打印pdf文件
【发布时间】:2021-06-25 09:03:31
【问题描述】:

这是我尝试在默认打印机中打印 pdf 文件的代码。 我没有收到任何错误,但文件仍未按预期打印。

            //print to printer
            try
            {
                ProcessStartInfo info = new ProcessStartInfo();
                info.Verb = "print";
                info.FileName = filePath;
                info.CreateNoWindow = true;
                info.WindowStyle = ProcessWindowStyle.Hidden;
                Process p = new Process();
                p.StartInfo = info;
                p.Start();

                p.WaitForInputIdle();
                System.Threading.Thread.Sleep(3000);
                if (false == p.CloseMainWindow())
                    p.Kill();
            }
            catch (Exception ex)
            {

                throw ex;
            }

我是不是做错了什么?

【问题讨论】:

    标签: c# pdf printing


    【解决方案1】:

    我认为您需要先启动进程 CMD.exe 并运行 print as 命令(作为进程的参数)。下面的代码可能会有所帮助...

     string filePath = @"Name_Of_File";
     string printerNameOrPRN = @"""Printer_PRN""";
     //print to printer
     try
     {
      string strCmdText;
      strCmdText = "/C print /D:" + printerNameOrPRN + " " + filePath;
      ProcessStartInfo info = new ProcessStartInfo();
      info.FileName = "CMD.exe";
      info.Arguments = strCmdText;
      info.WindowStyle = ProcessWindowStyle.Hidden;
      Process p = new Process();
      p.StartInfo = info;
      p.Start();
      System.Threading.Thread.Sleep(3000);
      if (false == p.CloseMainWindow())
        p.Kill();
     }
     catch (Exception ex)
     {
       Console.WriteLine(ex.Message);
    }
    

    【讨论】:

      猜你喜欢
      • 2012-07-19
      • 1970-01-01
      • 2014-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-04
      • 2018-11-27
      • 2018-12-25
      相关资源
      最近更新 更多