getting additional PaperSource details

随声附和 提交于 2019-12-07 13:15:56

问题


I'm trying to get the correct details of trays of various printers however have come across a problem. After a bit of research I've added the ReachFramework.dll and also

using System.Drawing.Printing;

To get the names of the trays for a printer I run the following code...

PrintDocument printDocument = new PrintDocument();
printDocument.PrinterSettings.PrinterName = "<Windows Printer Name>";

foreach (PaperSource paperSource in printDocument.PrinterSettings.PaperSources)
{
    Console.WriteLine(paperSource.ToString());
}

...replacing 'Windows Printer Name'. For some printers it works great and I get something like the following output...

[PaperSource Auto Tray Select Kind=AutomaticFeed]
[PaperSource Tray 1 Kind=Upper]
[PaperSource Tray 2 Kind=Middle]
[PaperSource Tray 3 Kind=Lower]
[PaperSource Bypass Tray Kind=Manual]

Which is what you would expect. However for some printers I get the following...

[PaperSource  Automatically Select Kind=FormSource]
[PaperSource  Printer auto select Kind=Custom]
[PaperSource  Manual Feed in Tray 1 Kind=Custom]
[PaperSource  Tray 1 Kind=Custom]
[PaperSource  Tray 2 Kind=Custom]
[PaperSource  Tray 3 Kind=Custom]
[PaperSource Unspecified Kind=Custom]
[PaperSource Plain Kind=Custom]
[PaperSource HP Matte 90g Kind=Custom]
[PaperSource Light 60-74g Kind=Custom]
[PaperSource Bond Kind=Custom]
[PaperSource Recycled Kind=Custom]
[PaperSource HP Matte 105g Kind=Custom]
[PaperSource HP Matte 120g Kind=Custom]
[PaperSource HP Soft Gloss 120g Kind=Custom]
[PaperSource HP Glossy 130g Kind=Custom]
... Additional 20 lines ...

This printer returned 36 trays but only the first 6 are valid tray types. Additionally the printer is only equipped with 2 standard trays, so 'Tray 3' is doesn't exist either.

So my question is this. How can I filter this list so only the correct trays are showing?


回答1:


Found a partial answer by changing the foreach and adding an if statement like the following...

foreach (PaperSource paperSource in printDocument.PrinterSettings.PaperSources)
{
    if (paperSource.RawKind < 1000)
    {
        Console.WriteLine(paperSource.ToString());
    }
}

This produces the following output...

[PaperSource  Automatically Select Kind=FormSource]
[PaperSource  Printer auto select Kind=Custom]
[PaperSource  Manual Feed in Tray 1 Kind=Custom]
[PaperSource  Tray 1 Kind=Custom]
[PaperSource  Tray 2 Kind=Custom]
[PaperSource  Tray 3 Kind=Custom]

While not ideal it does solve the part of the problem. It doesn't however solve the issue of valid trays that do not exist.




回答2:


This is kind of late but I just wanted to add that I was doing very similar thing in VB.net and part about RawKind really helped cut through the "clutter". The way I'm going about it is bit different, I essentially want end user to capture printer and tray for specific type of paper/printing. Then I can just print out reports without any prompts.

On my frmPrinterSelection i have two comboboxes; cboInstalledPrinters (to list installed printers), and cboPaperSource (to list paper source), below is my code. Hopefully it can help someone trying to capture printer and tray for later use.

    Private Sub frmPrinterSelection_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    cboInstalledPrinters.Items.Clear()
    Dim pkInstalledPrinters As String

    ' Find all printers installed
    For Each pkInstalledPrinters In
    PrinterSettings.InstalledPrinters
        cboInstalledPrinters.Items.Add(pkInstalledPrinters)
    Next pkInstalledPrinters

    'https://msdn.microsoft.com/en-us/library/system.drawing.printing.printersettings(v=vs.110).aspx
End Sub

Private Sub cboInstalledPrinters_SelectionChangeCommitted(sender As Object, e As EventArgs) Handles cboInstalledPrinters.SelectionChangeCommitted

    Dim pkSource As Printing.PaperSource
    Dim strPrinter As String
    Dim printDoc As New Printing.PrintDocument
    Dim intTray As Integer
    cboPaperSource.Items.Clear()
    ' Add list of paper sources found on the printer to the combo box.
    ' The DisplayMember property is used to identify the property that will provide the display string.
    strPrinter = cboInstalledPrinters.SelectedItem
    printDoc.PrinterSettings.PrinterName = strPrinter
    For intTray = 0 To printDoc.PrinterSettings.PaperSources.Count - 1
        pkSource = printDoc.PrinterSettings.PaperSources.Item(intTray)
        If pkSource.RawKind < 1000 Then
            cboPaperSource.Items.Add(Trim(pkSource.SourceName))
        Else
            MsgBox(pkSource)
        End If
    Next
    Me.cboPaperSource.Focus()
    Me.cboPaperSource.DroppedDown = vbTrue
End Sub


来源:https://stackoverflow.com/questions/16322196/getting-additional-papersource-details

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!