I've been having trouble automatically selecting a Tray (PaperSource) to print from on a specific printer (HP OfficeJet Pro 9020 series) using VB.NET. I have reduced my code to a bare minimum below to indicate the process I'm following.
After the print dialog has popped up and you select 'Tray 2' from the list. The line of code prn.PrinterSettings = pd.PrinterSettings applies that setting, and it subsequently prints from the correct tray. If, however, you just click ok on the dialog it prints from the wrong tray.
The strange thing is that, as you can see, I set the 'Tray 2' option in code, so, in theory that object should be exactly the same. I used Newtonsoft.JSON to serialise the pd.PrinterSettings object before and after I had selected 'Tray 2' in the dialog so I could compare the differences and they were both identical. The only differences at first were the ones I addressed in the code earlier on. They didn't make any difference though. Those being;
pd.PrinterSettings.PrintFileName = "IP_192.168.1.50"
pd.PrinterSettings.PrinterName = "Dispatch Office"
pd.PrinterSettings.Collate = False
But for whatever reason, it only works if I explicitly open the dialog and select the desired Tray and click ok. prn.PrinterSettings = pd.PrinterSettings is the only line of code that has any effect on what printer is used, so somewhere in pd.PrinterSettings there must be something missing that I couldn't see in the JSON string. I've had success with this code on other printers, but this specific printer seems to be different, but the problem has to be with VB.NET I would think.
Has anyone come across anything like this before? Here is my full code below.
Private Sub Print()
Dim pd As New PrintDialog
pd.PrinterSettings.PrintFileName = "IP_192.168.1.50"
pd.PrinterSettings.PrinterName = "Dispatch Office"
pd.PrinterSettings.Collate = False
For Each paperSource As Printing.PaperSource In pd.PrinterSettings.PaperSources
If paperSource.SourceName = "Tray 2" Then
pd.PrinterSettings.DefaultPageSettings.PaperSource = paperSource
Exit For
End If
Next
If pd.ShowDialog = DialogResult.OK Then
Dim prn As New Printing.PrintDocument
prn.PrinterSettings = pd.PrinterSettings
AddHandler prn.PrintPage, AddressOf Me.PrintPageHandler
prn.Print()
RemoveHandler prn.PrintPage, AddressOf Me.PrintPageHandler
End If
End Sub
Private Sub PrintPageHandler(ByVal sender As Object, ByVal args As Printing.PrintPageEventArgs)
Dim myFont As New Font("Courier New", 9)
Dim strTextFile = IO.File.ReadAllText("C:\textfile.txt")
args.Graphics.DrawString(strTextFile, New Font(myFont, FontStyle.Regular), Brushes.Black, 50, 50)
End Sub
Also, just to confirm, this is independent of the print dialog. The code below which is in the shortest form possible also does not work.
Private Sub PrintAuto()
Dim ps As New Printing.PrinterSettings With {.PrinterName = "Dispatch Office"}
ps.DefaultPageSettings.PaperSource = ps.PaperSources(3) ' Tray 2
Dim prn As New Printing.PrintDocument With {.PrinterSettings = ps}
AddHandler prn.PrintPage, AddressOf PrintPageHandler
prn.Print()
RemoveHandler prn.PrintPage, AddressOf PrintPageHandler
End Sub