获取额外的PaperSource详细信息
问题描述:
我试图获得各种打印机托盘的正确详细信息,但遇到了问题。一些研究之后,我已经添加了ReachFramework.dll也获取额外的PaperSource详细信息
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'也不存在。
所以我的问题是这样的。我如何过滤这个列表,以便只显示正确的托盘?
答
发现改变的foreach,如果添加了部分答案语句像下面...
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]
虽然不理想它解决问题的一部分。然而,这并不能解决不存在的有效托盘问题。
答
这是一种很晚了,但我只是想补充说,我在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
他们都是有效的打印机吗?你如何迭代打印机? – jle 2013-05-01 17:27:46
在本例中没有必要遍历打印机,因为您只需完全命名它即可,但是我使用System.Printing.LocalPrintServer()。GetPrintQueues(enumFlags)来获取Windows打印机名称。 – Thundter 2013-05-02 09:23:42