我有一个列表框,其中显示我可以在 Windows 客户端上找到的所有打印机。
List<MyPrinterClass> lstPrinters = new List<MyPrinterClass>();
foreach (var item in PrinterSettings.InstalledPrinters)
{
MyPrinterClass d = new MyPrinterClass();
d.printerName = item.ToString();
lstPrinters.Add(d);
}
如果到目前为止需要我的课程
public class MyPrinterClass
{
public string imageSource { get; set; }
public Bitmap imageBmp { get; set; }
public string printerName { get; set; }
public List<PaperSource> paperSources { get; set; }
}
我正在尝试获取托盘,如果可能的话(通过打印机)获取包含的纸张来源。打印机必须安装在客户端上(这是我的解决方案的给定条件)。所以 Windows 应该知道托盘和 paperSource 的详细信息。
我尝试过的: 通过 PrinterSettings.InstalledPrinters 我能够获取 PrinterNames,但似乎无法访问其他数据。 通过 PrinterSettings.PaperSources 我能够接收 PaperSources,但我来自 PrintDocument。因此,数据与我的列表框中选择的已安装打印机无关。
我的期望: 类似于 MS 指南给出的通用 Windows 解决方案的打印机类。
我能做什么: 我可以创建一个通用的 Windows 解决方案并移交数据,但这是我最不喜欢的选择。
System.Printing
) 而不是 Winforms 库 (System.Drawing.Printing
)。
PrintCapabilities
和 PrintQueue
检查默认值或获取当前打印作业,以便通过 PrintTicket
获取/设置当前作业的设置(例如页面大小、份数等) .).LocalPrintServer
构造函数重载。PrintServer
访问远程打印机。
var printServer = new LocalPrintServer();
/* Select a destination printer using one of the following methods */
PrintQueue destinationPrinter;
// Filter list of available printers
PrintQueueCollection availablePrinters = printServer.GetPrintQueues();
destinationPrinter = availablePrinters.First(pq => pq.Name.Contains("PDF", StringComparison.OrdinalIgnoreCase));
// Use the default printer
destinationPrinter = printServer.DefaultPrintQueue;
// Pick a particular printer by name e.g., native PDF printer
destinationPrinter = printServer.GetPrintQueue("Microsoft Print to PDF");
// Print a document (FlowDocument, FixedDocument or from a path)
XpsDocumentWriter documentWriter = PrintQueue.CreateXpsDocumentWriter(destinationPrinter);
documentWriter.WriteAsync(fixedDocument);
// Get the current print job settings...
PrintJobSettings jobSettings = destinationPrinter.CurrentJobSettings;
// ... and for example, use it to get the current print ticket
PrintTicket printTicket = jobSettings.CurrentPrintTicket;
// Via the PrintTicket you get various information about the print job
// for example, the current paper tray (input bin).
//
// Test for the default paper tray (input bin)
if (printTicket.InputBin == InputBin.Cassette)
{
}
// Get a list of available paper trays for the current printer (PrintQueue)
// by using the PrintCapabilities
PrintCapabilities printerCapabilities = destinationPrinter.GetPrintCapabilities();
ReadOnlyCollection<InputBin> availablePaperTrays = printerCapabilities.InputBinCapability;