如何通过一次进程调用将多个文件发送到打印机

2024-02-20

我需要从硬盘打印多个 PDF 文件。我发现这很美丽solution https://stackoverflow.com/questions/6103705/how-can-i-send-a-file-document-to-the-printer-and-have-it-print如何将文件发送到打印机。此解决方案的问题在于,如果您想打印多个文件,则必须等待每个文件完成该过程。

在命令 shell 中,可以对多个文件名使用相同的命令:print /D:printerName file1.pdf file2.pdf只需一次调用即可将它们全部打印出来。

不幸的是只是将所有文件名放入ProcessStartInfo不起作用

string filenames = @"file1.pdf file2.pdf file3.pdf"
ProcessStartInfo info = new ProcessStartInfo();
info.Verb = "print";
info.FileName = filenames;

它也不会将文件名设置为Arguments of the Process

info.Arguments = filename;

我总是收到错误:找不到文件!

如何通过一个进程调用打印多个文件?

这是我现在如何使用它的示例:

public void printWithPrinter(string filename, string printerName)
{

    var procInfo = new ProcessStartInfo();    
    // the file name is a string of multiple filenames separated by space
    procInfo.FileName = filename;
    procInfo.Verb = "printto";
    procInfo.WindowStyle = ProcessWindowStyle.Hidden;
    procInfo.CreateNoWindow = true;

    // select the printer
    procInfo.Arguments = "\"" + printerName + "\""; 
    // doesn't work
    //procInfo.Arguments = "\"" + printerName + "\"" + " " + filename; 

    Process p = new Process();
    p.StartInfo = procInfo;

    p.Start();

    p.WaitForInputIdle();
    //Thread.Sleep(3000;)
    if (!p.CloseMainWindow()) p.Kill();
}

以下应该有效:

public void PrintFiles(string printerName, params string[] fileNames)
{
    var files = String.Join(" ", fileNames);
    var command = String.Format("/C print /D:{0} {1}", printerName, files);
    var process = new Process();
    var startInfo = new ProcessStartInfo
    {
        WindowStyle = ProcessWindowStyle.Hidden,
        FileName = "cmd.exe",
        Arguments = command
    };

    process.StartInfo = startInfo;
    process.Start();
}

//CALL
PrintFiles("YourPrinterName", "file1.pdf", "file2.pdf", "file3.pdf");
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何通过一次进程调用将多个文件发送到打印机 的相关文章

随机推荐