getting additional PaperSource details

陌路散爱 提交于 2019-12-05 19:28:42

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.

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