XtraReports – Selecting a paper tray

This sample shows how to enumerate and select a specific tray to use for printing. The code works bij intercepting the oReport.PrintingSystem.StartPrint event and adding a handler to send it to a routine which enumerates the trays of the printer. There are a few pitfalls to take note off: My HP drivers insisted on having spaces at the end of the tray names, so i needed to trim the Papersources.Name enumeration. Also, the printer trays ended up being named “Lade 1″ for “Tray 1″ on my dutch language system. I have not explorered if this comes from the printer driver or the base windows version, but keep this in mind when implementing this technique: you will need to sort out the internationalisations. ( this can be solved by simply creating an enumeration of the trays and having the user choose one instead of hard coding, which basicaly always is bad – but works like a charm for a basic sample)

Imports DevExpress.XtraReports.UI
  
Public Class Form1
  
    ''' <summary>
    ''' This button sends the job to Tray 2 "Lade 2"
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub ButtonG1DRR_Click(sender As System.Object, e As System.EventArgs) Handles ButtonG1DRR.Click
  
        Dim sPrinter As String = ComboBoxPrinterSelection.Text
  
        Using oReport As New XtraReportTestPage()
            AddHandler oReport.PrintingSystem.StartPrint, AddressOf ProcessStartPrintEventLade3
            Dim oTool As New ReportPrintTool(oReport)
            oTool.Print(sPrinter)
        End Using
  
    End Sub
  
    ''' <summary>
    ''' This button sends the hob to tray 3 "Lade 3"
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub ButtonG1WEB_Click(sender As System.Object, e As System.EventArgs) Handles ButtonG1WEB.Click
  
        Dim sPrinter As String = ComboBoxPrinterSelection.Text
  
        Using oReport As New XtraReportTestPage()
            AddHandler oReport.PrintingSystem.StartPrint, AddressOf ProcessStartPrintEventLade2
            Dim oTool As New ReportPrintTool(oReport)
            oTool.Print(sPrinter)
        End Using
  
    End Sub
  
    ''' <summary>
    ''' Locate Tray 2 "Lade 2" and use it for the print job
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub ProcessStartPrintEventLade2(ByVal sender As Object, ByVal e As DevExpress.XtraPrinting.PrintDocumentEventArgs)
  
        For i = 0 To e.PrintDocument.PrinterSettings.PaperSources.Count - 1
            If Trim(LTrim(e.PrintDocument.PrinterSettings.PaperSources(i).SourceName)) = "Lade 2" Then
                e.PrintDocument.DefaultPageSettings.PaperSource = e.PrintDocument.PrinterSettings.PaperSources(i)
                Exit For
            End If
        Next
  
    End Sub
  
    ''' <summary>
    ''' Locate Tray 3 "Lade 3" and use it for the print job
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub ProcessStartPrintEventLade3(ByVal sender As Object, ByVal e As DevExpress.XtraPrinting.PrintDocumentEventArgs)
  
        For i = 0 To e.PrintDocument.PrinterSettings.PaperSources.Count - 1
            If Trim(LTrim(e.PrintDocument.PrinterSettings.PaperSources(i).SourceName)) = "Lade 3" Then
                e.PrintDocument.DefaultPageSettings.PaperSource = e.PrintDocument.PrinterSettings.PaperSources(i)
                Exit For
            End If
        Next
  
    End Sub
  
End