4

我正在尝试获取各种打印机托盘的正确详细信息,但是遇到了问题。经过一番研究,我添加了 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”也不存在。

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

4

2 回答 2

3

通过更改 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]

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

于 2013-05-01T16:39:52.443 回答
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
于 2015-12-10T14:38:39.513 回答