How to send multiple files to the printer with one Process call

孤人 提交于 2020-05-15 06:27:06

问题


I need to print multiple PDF-files from the hard-drive. I have found this beautiful solution of how to send a file to the printer. The problem with this solution is that if you want to print multiple files you have to wait for each file for the process to finish.

in the command shell it is possible to use the same command with multiple filenames: print /D:printerName file1.pdf file2.pdf and one call would print them all.

unfortunately simply just to put all the filenames into the ProcessStartInfo doesn't work

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

neither does it to put the filenames as Arguments of the Process

info.Arguments = filename;

I always get the error: Cannot find the file!

How can I print a multitude of files with one process call?

Here is an example of how I use it now:

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

回答1:


Following should work:

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:


It's not necessarily a simple solution, but you could merge the pdfs first and then send then to acrobat.

For example, use PdfMerge

Example overload to your initial method:

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


来源:https://stackoverflow.com/questions/37526151/how-to-send-multiple-files-to-the-printer-with-one-process-call

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!