2013-05-01 243 views
2

我試圖獲得各種打印機托盤的正確詳細信息,但遇到了問題。一些研究之後,我已經添加了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'也不存在。

所以我的問題是這樣的。我如何過濾這個列表,以便只顯示正確的托盤?

+0

他們都是有效的打印機嗎?你如何迭代打印機? – jle 2013-05-01 17:27:46

+0

在本例中沒有必要遍歷打印機,因爲您只需完全命名它即可,但是我使用System.Printing.LocalPrintServer()。GetPrintQueues(enumFlags)來獲取Windows打印機名稱。 – Thundter 2013-05-02 09:23:42

回答

2

發現改變的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] 

雖然不理想它解決問題的一部分。然而,這並不能解決不存在的有效托盤問題。

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