2011-12-30 42 views
1

這是我如何打印我的FlowDocument:如何獲取WPF中的打印機狀態?

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線程中運行。

+0

我可以檢查LPT打印機端口的狀態,但是我使用的USB打印機,並希望從打印機聽。 – user995387 2011-12-30 10:42:48

回答

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