首页 文章

如何使用一个Process调用将多个文件发送到打印机

提问于
浏览
2

我需要从硬盘驱动器打印多个PDF文件 . 我发现了如何将文件发送到打印机的美丽solution . 此解决方案的问题在于,如果要打印多个文件,则必须等待每个文件以完成该过程 .

在命令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 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();
}

2 回答

  • 0

    以下应该工作:

    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");
    
  • 2

    它不一定是一个简单的解决方案,但您可以先合并pdf然后再发送给acrobat .

    例如,使用PdfMerge

    初始方法的示例重载:

    public void printWithPrinter(string[] fileNames, string printerName)
        {
            var fileStreams = fileNames
                .Select(fileName => (Stream)File.OpenRead(fileName)).ToList();
            var bundleFileName = Path.GetTempPath();
            try
            {
    
                try
                {
                    var bundleBytes = new PdfMerge.PdfMerge().MergeFiles(fileStreams);
                    using (var bundleStream = File.OpenWrite(bundleFileName))
                    {
                        bundleStream.Write(bundleBytes, 0, bundleBytes.Length);
                    }
                }
                finally 
                {
                        fileStreams.ForEach(s => s.Dispose());
                }
    
                printWithPrinter(bundleFileName, printerName);
            }
            finally 
            {
                if (File.Exists(bundleFileName))
                    File.Delete(bundleFileName);
            }
        }
    

相关问题