获取其他PaperSource详细信息

时间:2013-05-01 16:26:52

标签: c# .net system.drawing

我正在尝试获取各种打印机的托盘的正确细节但是遇到了问题。经过一些研究后,我添加了ReachFramework.dll以及

using System.Drawing.Printing;

要获取打印机托盘的名称,请运行以下代码...

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

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

...替换“Windows打印机名称”。对于某些打印机,它工作得很好,我得到类似以下输出......

[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]

这是你所期望的。但是对于某些打印机,我得到以下内容......

[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 ...

此打印机返回36个托盘,但只有前6个是有效托盘类型。此外,打印机仅配备2个标准托盘,因此“托盘3”也不存在。

所以我的问题是这个。如何过滤此列表以便只显示正确的托盘?

2 个答案:

答案 0 :(得分:2)

通过更改foreach并添加如下所示的if语句找到部分答案...

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

这会产生以下输出......

[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]

虽然不理想但它确实解决了问题的一部分。但是,它不能解决不存在的有效托盘的问题。

答案 1 :(得分:1)

这有点晚了但我只想补充一点,我在VB.net中做了非常相似的事情,而关于RawKind的部分确实帮助切断了“混乱”。我的方式有点不同,我基本上希望最终用户捕获特定类型纸张/打印的打印机和托盘。然后我可以在没有任何提示的情况下打印出报告。

在我的frmPrinterSelection上,我有两个组合框; cboInstalledPrinters(列出已安装的打印机)和cboPaperSource(列出纸张来源),下面是我的代码。希望它可以帮助有人试图捕获打印机和托盘供以后使用。

    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