如何将文件文件发送到打印机并进行打印?

基本前提是:

我的用户点击一些小玩意儿,一个PDF文件被吐出到他的桌面。 有没有办法让我将这个文件发送到打印机队列,并打印到本地连接的打印机?

string filePath = "filepathisalreadysethere"; SendToPrinter(filePath); //Something like this? 

他会多次做这个过程。 对于教室里的每个学生,他都要打印一张小小的成绩单。 所以我为每个学生生成一个PDF文件,我想自动化打印过程,而不是让用户生成pdf,打印,生成pdf,打印,生成pdf,打印。

有关如何解决这个问题的任何build议? 我使用Windows Forms .NET 4在Windows XP上运行。

我发现这个StackOverflow的问题,接受的答案表明:

一旦你创build了你的文件,你可以通过命令行来打印它们(你可以使用System.Diagnostics命名空间中的Command类来实现)

我将如何做到这一点?

您可以告诉Acrobat Reader使用(如此处已经提到的)“打印”动词来打印文件。 之后,您还需要以编程方式closuresAcrobat Reader:

 private void SendToPrinter() { ProcessStartInfo info = new ProcessStartInfo(); info.Verb = "print"; info.FileName = @"c:\output.pdf"; 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(); } 

这将打开Acrobat Reader,并通知它将PDF发送到默认打印机,然后在三秒钟后closuresAcrobat。

如果您愿意使用您的应用程序发布其他产品,那么您可以使用GhostScript(免费)或命令行PDF打印机,如http://www.commandlinepdf.com/ (商业版)。

注意:示例代码在当前注册的应用程序中打开PDF, 以打印PDF ,这是大多数人的机器上的Adobe Acrobat Reader。 但是,他们可能会使用不同的PDF查看器,例如Foxit( http://www.foxitsoftware.com/pdf/reader/ )。 但是示例代码仍然可以工作。

我知道标签说的Windows Forms …但是,如果有人对WPF应用程序方法感兴趣, System.Printing就像一个魅力。

 var file = File.ReadAllBytes(pdfFilePath); var printQueue = LocalPrintServer.GetDefaultPrintQueue(); using (var job = printQueue.AddJob()) using (var stream = job.JobStream) { stream.Write(file, 0, file.Length); } 

只要记住包含System.Printing引用,如果它尚未包含。 现在,这种方法在ASP.NETWindows Service中不能很好地发挥作用。 它不应该与Windows Forms一起使用,因为它具有System.Drawing.Printing 。 我没有使用上面的代码打印我的PDF文件。

但是,我应该提到,如果您的打印机不支持直接打印PDF文件格式,那么使用这种方法就不是那么幸运了。

在.net中添加一个新的答案,就是在.net中打印PDF的问题已经存在很长时间了,大部分的答案都在Google Pdfium库之前,这个库现在有一个.net包装器。 对于我来说,我自己正在研究这个问题,并一直空白,试图做出像生成Acrobat或其他PDF阅读器的黑客解决scheme,或运行到昂贵,并没有非常兼容的许可条款的商业图书馆。 但是Google Pdfium库和PdfiumViewer .net包装是开源的,所以对于很多开发者来说都是一个很好的解决scheme,包括我自己在内。 PdfiumViewer获得Apache 2.0许可。

你可以在这里获得NuGet包:

https://www.nuget.org/packages/PdfiumViewer/

你可以在这里find源代码:

https://github.com/pvginkel/PdfiumViewer

下面是一些简单的代码,它将静默地从它的文件名打印任何数量的PDF文件副本。 您也可以从stream中加载PDF(这是我们通常的做法),您可以很容易地看到代码或示例。 还有一个WinForm PDF文件视图,因此您也可以将PDF文件渲染到视图中或对其进行打印预览。 对于我们来说,我只是需要一种方法来将PDF文件静默地按需打印到特定的打印机。

 public bool PrintPDF( string printer, string paperName, string filename, int copies) { try { // Create the printer settings for our printer var printerSettings = new PrinterSettings { PrinterName = printer, Copies = (short)copies, }; // Create our page settings for the paper size selected var pageSettings = new PageSettings(printerSettings) { Margins = new Margins(0, 0, 0, 0), }; foreach (PaperSize paperSize in printerSettings.PaperSizes) { if (paperSize.PaperName == paperName) { pageSettings.PaperSize = paperSize; break; } } // Now print the PDF document using (var document = PdfDocument.Load(filename)) { using (var printDocument = document.CreatePrintDocument()) { printDocument.PrinterSettings = printerSettings; printDocument.DefaultPageSettings = pageSettings; printDocument.PrintController = new StandardPrintController(); printDocument.Print(); } } return true; } catch { return false; } } 

这是一个稍微修改的解决scheme。 当进程闲置至less1秒时,进程将被终止。 也许你应该添加一个X秒的时间,并从一个单独的线程调用该函数。

 private void SendToPrinter() { ProcessStartInfo info = new ProcessStartInfo(); info.Verb = "print"; info.FileName = @"c:\output.pdf"; info.CreateNoWindow = true; info.WindowStyle = ProcessWindowStyle.Hidden; Process p = new Process(); p.StartInfo = info; p.Start(); long ticks = -1; while (ticks != p.TotalProcessorTime.Ticks) { ticks = p.TotalProcessorTime.Ticks; Thread.Sleep(1000); } if (false == p.CloseMainWindow()) p.Kill(); } 

这是一个迟到的答案,但是您也可以使用System.IO命名空间的File.Copy方法向打印机发送一个文件:

 System.IO.File.Copy(filename, printerName); 

这工作正常

System.Diagnostics.Process.Start可用于打印文档。 将UseShellExecute设置为True并将动词设置为“打印”。

你可以像在这篇文章中一样使用GhostScript:

如何使用GhostScript(gswin32c.exe)shell命令在默认networking打印机上打印PDF

我知道埃德温回答以上,但他唯一的一个文件。 我用这个代码来打印给定目录中的所有文件。

 public void PrintAllFiles() { System.Diagnostics.ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo(); info.Verb = "print"; System.Diagnostics.Process p = new System.Diagnostics.Process(); //Load Files in Selected Folder string[] allFiles = System.IO.Directory.GetFiles(Directory); foreach (string file in allFiles) { info.FileName = @file; info.CreateNoWindow = true; info.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; p.StartInfo = info; p.Start(); } //p.Kill(); Can Create A Kill Statement Here... but I found I don't need one MessageBox.Show("Print Complete"); } 

它本质上循环通过给定的目录variablesDirectory – >中的每个文件,它是@“C:\ Users \ Owner \ Documents \ SalesVaultTesting \”并将这些文件打印到您的默认打印机

简单的方法:

 var pi=new ProcessStartInfo("C:\file.docx"); pi.UseShellExecute = true; pi.Verb = "print"; var process = System.Diagnostics.Process.Start(pi);