Search code examples
c#wpfprintingavalonia

Get printer trays and paperSource by selected printerName


I am having a ListBox where I show all the printer I can find on the windows client.

List<MyPrinterClass> lstPrinters = new List<MyPrinterClass>();

foreach (var item in PrinterSettings.InstalledPrinters)
{
    MyPrinterClass d = new MyPrinterClass();
    d.printerName = item.ToString();

    lstPrinters.Add(d);
}

If needed my class so far

public class MyPrinterClass
{
    public string imageSource { get; set; }
    public Bitmap imageBmp { get; set; }
    public string printerName { get; set; }
    public List<PaperSource> paperSources { get; set; }
}

I am trying to get the trays and if possible (by the printer) the containing paperSource. The printers have to be installed on the client (its a given condition to my solution). So the details of trays and paperSource should be known by Windows.

What I tried: By PrinterSettings.InstalledPrinters I am able to get the printerNames but there seems to be no access to other data. by PrinterSettings.PaperSources I am able to receive the PaperSources but I am coming from a PrintDocument. So the data isnt related to the selected installed printer from my ListBox.

What I expected: A printer class similar to the universal windows solution given by the MS Guides.

What I could do: I could create a universal windows solution and hand over the data but its my least favorite option to do so.


Solution

  • I recommend to use the WPF library (System.Printing) instead of the Winforms library (System.Drawing.Printing).

    Use the PrintCapabilities and PrintQueue to inspect the defaults or to get the current print job in order to get/set the current job's settings via the PrintTicket (e.g. page size, copy count etc.).
    Use the LocalPrintServer constructor overload in case you want to configure a printer.
    Use PrintServer to access a remote printer.

    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;