如何在WPF中获取打印机状态?

时间:2011-12-30 10:35:53

标签: c# .net wpf multithreading printing

以下是我打印flowdocument的方法:

PrintDialog pd = new PrintDialog();
LocalPrintServer local = new LocalPrintServer();
PrintQueue pq = local.DefaultPrintQueue;//GetPrintQueue("[Printer Name]"); //e.g. local.GetPrintQueue("Microsoft XPS Document Writer");
pd.PrintQueue = pq;
PrintTicket pt = pq.DefaultPrintTicket;
pt.PageMediaSize = new PageMediaSize(PageMediaSizeName.ISOA5);// or we can specify the custom size(width, height) here
pd.PrintTicket = pt;
pt.PageBorderless = PageBorderless.Borderless;
pt.PageOrientation = PageOrientation.ReversePortrait;
PrintCapabilities capabilities = pd.PrintQueue.GetPrintCapabilities(pd.PrintTicket);
double sizeWidth = capabilities.PageImageableArea.ExtentWidth;
double sizeHeight = capabilities.PageImageableArea.ExtentHeight;
var fd = new FlowDocument();

DocumentPaginator sd = ((IDocumentPaginatorSource)fd).DocumentPaginator;
sd.PageSize = new Size(sizeWidth + 20, sizeHeight);
pd.PrintDocument(sd, "My Doc");
// GET THE PRINTER STATUS IN MESSAGE BOX HERE..
 MessageBox.Show(printerStatus());  // printerStatus() is a pseudo method to retrieve the status of the printer.

如何获取打印机的当前状态以便输出,打印,缺纸,卡纸,打印机脱机等消息????

在搜索时,我看到了这个页面:http://msdn.microsoft.com/en-us/library/system.printing.printqueuestatus.aspx

但是我对它的用法一无所知。你能帮助我帮助我吗?

此打印过程正在STA线程中运行。

1 个答案:

答案 0 :(得分:2)

你可以这样检查

using System.Management;

class PrinterOffline
{
    private static void Main(string[] args)
    {
          // Set management scope
          ManagementScope scope = new ManagementScope("\\root\\cimv2");
          scope.Connect();

         // Select Printers from WMI Object Collections
         ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Printer");
         string printerName = "";
         foreach (ManagementObject printer in searcher.Get()) 
         {
            printerName = printer("Name").ToString().ToLower();
            if (printerName.Equals("Name_Of_Printer")) 
            {
                Console.WriteLine("Printer = " + printer("Name"));
                if (printer("WorkOffline").ToString().ToLower().Equals("true"))
                {
                      // printer is offline by user
                      Console.WriteLine("Your Plug-N-Play printer is not connected.");
                }
                else 
                {
                     // printer is not offline
                      Console.WriteLine("Your Plug-N-Play printer is connected.");
                }
            }
        }
    }
}

请点击此链接获取更多information on status of the printer