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

本文关键字:获取 打印机 状态 WPF | 更新日期: 2023-09-27 18:19:01

我是这样打印我的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线程中运行。

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

你可以像这样检查

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.");
                }
            }
        }
    }
}

请通过此链接获取有关打印机状态的更多信息