在C#中打印或未打印的文档

时间:2013-11-28 09:14:00

标签: c# .net printing process system.management

我使用以下代码打印pdf:

var fileName = filepath;
ProcessStartInfo psInfo = new ProcessStartInfo();
psInfo.Arguments = ConfigurationManager.AppSettings["printer_name"];
psInfo.FileName = fileName;
psInfo.WindowStyle = ProcessWindowStyle.Hidden;
psInfo.Verb = "print";
psInfo.CreateNoWindow = false;
psInfo.UseShellExecute = true;
process = Process.Start(psInfo);

以下获取打印机的状态:

string query = string.Format("SELECT * from Win32_Printer "+ "WHERE Name LIKE '%   {0}'",printerName);
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
ManagementObjectCollection coll = searcher.Get();

foreach (ManagementObject printer in coll)
{
    foreach (PropertyData property in printer.Properties)
    {
        Logger.LogInfo(""+property.Name, "" +property.Value);
    }
}

并尝试跟踪监控打印队列:

LocalPrintServer server = new LocalPrintServer();
PrintQueueCollection queueCollection =      server.GetPrintQueues();
PrintQueue printQueue = null;
foreach (PrintQueue pq in queueCollection)
{
    if (pq.FullName == "HP LaserJet P1505n")
        printQueue = pq;
}

int numberOfJobs = 0;
if (printQueue != null)
    numberOfJobs = printQueue.NumberOfJobs;

我想要做的就是知道我使用打印的文件(1)是否成功打印! (2)nd代码片段只显示相同的属性anme和值always.So无法通知打印状态。(3)rd代码片段始终监视队列一次并说“0”numberofjobs。

那么获取打印状态的实际方法是什么?

1 个答案:

答案 0 :(得分:0)

了解打印作业状态..!试试这个,

 SelectQuery qry = new SelectQuery("PrintJob");

  using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(qry))
  using (ManagementObjectCollection printJobs = searcher.Get())
     foreach (ManagementObject printJob in printJobs)
       {
    string name = (string) product["Name"];
    string[] nameParts = name.Split(',');
    string printerName = nameParts[0];
    string jobNumber = nameParts[1];
    string document = (string) product["Document"];
    string jobStatus = (string) product["JobStatus"];
       }

我希望这会有所帮助。