【问题标题】:Print Multiple PDF file in one click一键打印多个PDF文件
【发布时间】:2013-05-25 16:42:57
【问题描述】:

我想一键打印现有的 .pdf 文件。 It works fine when i only print one file at a time, but when choose to print multiple files at once it only prints one file.

这是我的代码:

List<string> ListFilePath; //Assume i have a collection of filepath stored here
foreach(string FilePath in ListFilePath)
{
    Process proc = new Process();
    proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
    proc.StartInfo.Verb = "print";
    proc.StartInfo.FileName = FilePath;
    proc.StartInfo.Arguments = String.Format(@"/p /h {0}", FilePath);
    proc.StartInfo.CreateNoWindow = true;
    proc.Start();
    proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
    if (proc.HasExited == false)
    {
          proc.WaitForExit(10000);
    }

    proc.EnableRaisingEvents = true;
    proc.Close();
}

我的错在哪里?

【问题讨论】:

  • 它会遍历整个集合吗?它会抛出任何异常吗?在调试器中,你能看到它遍历了所有文件的 process.Start() 行还是第一个文件?
  • 是的,它会遍历所有文件的 process.Start()。当我将它定位到 OneNote 时,输出是多个文件。但是当我将它定位到打印机时,它只打印一个文件
  • 也许问题更深层 - PrintSpooler 服务。它有多个文档排队的已知问题。
  • 好的,我会检查一下。但是从代码方面来说,我并没有做错什么,对吗?

标签: c# asp.net pdf printing


【解决方案1】:

这对我很有用

foreach (string pdf in ListFilePath)
      {
           string filename = System.IO.Path.GetFileName(pdf);
           string fullPath = (@"U:\Define\Full\Path" + filename);
           Print(fullPath, 'PrinterNameHere');
      }


public static bool Print(string file, string printer)
        {
            try
            {
                Process.Start(Registry.LocalMachine.OpenSubKey(
                        @"SOFTWARE\Microsoft\Windows\CurrentVersion" +
                        @"\App Paths\AcroRd32.exe").GetValue("").ToString(),
                        string.Format("/h /t \"{0}\" \"{1}\"", file, printer));
                return true;
            }
            catch 
            { return false; }
        }

【讨论】:

    【解决方案2】:

    您的代码正在启动一个进程并在服务器上创建一个打印作业。你确定这是正确的,而不是应该在客户端上打印?

    您可能会尝试的一件事是将所有 pdf 合并为一个 (PDFSharp does that nicely),然后只打印合并后的 pdf。

    【讨论】:

      【解决方案3】:

      你能分配给事件处理程序,然后停止进程吗?

      不在此方法内。

      【讨论】:

      • 所以你的意思是我必须停止循环之外的过程?
      • 对。当过程告诉你,它准备好了。这是一个建议。在事件中检查这个。
      猜你喜欢
      • 2011-11-03
      • 1970-01-01
      • 2013-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-16
      相关资源
      最近更新 更多