如何以编程方式打印各种文件类型

我正在编写一个应用程序,它执行一些测试并生成许多不同的报告。 这些可以是标签的任意组合,最终客户的PDF,维修部门的PDF,XML文件等。

根据报告类型,我需要将文件发送到文件系统或许多不同的打印机之一(A4,标签等)。 理想情况下,应该没有弹出窗口 – 只是直接用纸。

如何将文件(PDF,XML)发送到打印机? 我曾经想过,对于XML / Text我只能将File.Copy转换为LPTn,但这似乎不起作用。 对于PDF我猜我可以使用一些导致打印PDF的参数调用Acrobat。

我使用的打印机映射到LPTn。 有没有更好的方法来执行此操作并将定义存储在应用程序中? 即标签转到MyLabelPrinter,A4 PDF转到MyA4Printer。

有没有人这样做过?

ProcessStartInfo info = new ProcessStartInfo("[path to your file]"); info.Verb = "PrintTo"; info.Arguments = "\"[printer name]\""; info.CreateNoWindow = true; info.WindowStyle = ProcessWindowStyle.Hidden; Process.Start(info); 

看看这个网页 。 您应该找到您正在查找的PDF信息。 例如,它看起来像这样:

  ProcessStartInfo infoOnProcess = new ProcessStartInfo("C:/example.pdf"); info.Verb = "PrintTo"; //Put a if there, if you want to change printer depending to file extension info.Arguments = "\"HP-example-Printer\""; info.CreateNoWindow = true; info.WindowStyle = ProcessWindowStyle.Hidden; Process.Start(infoOnProcess);