Ask a questionAsk a question
 

AnswerHow to Auto Print?

  • Friday, October 30, 2009 3:47 AMJerryCic Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I have posted a simlar question in th vb.net forum. But have not received an answer.

    In VB.Net 2005 window forms.
    Using an RS serverside report.
    My report is created and deployed. It uses an 8.5" by 5" form.
    Interactive size and page sizes are 8.5x5
    Margins are set to 0.5
    Body is set to 7.5x4" 

    In my vb.net 2005 project:
    I have a vb form with a button and a report viewer control on it.

    When i click the button, the report renders correctly.
    With the report rendered, I click the print button and the print dialogue shows.
    I select the printer i want and select the custom form that i have created from its properties.

    I click on the print button and it prints. Yeah so far.

    I want to automate the entire process.
    I can automate the entire process up to the point when the report fires the "render complete" event.
    Inside this event, i want to choose a printer, choose the papersize, and print it.

    How do i do this?

    I could be happy with just chooseing the printer and printing without a user. The destination printer is a dot matrix with carbon forms in it on a tractor. There is no other paper that will be in this printer. I already have figured out that i can create the form in the printer driver and set it as the default papersize.

    Anyway
    Thank you up front (as usual)
    Jerry Cic 

Answers

  • Sunday, November 15, 2009 3:28 AMJerryCic Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    Here is my solution

    I created a class object called AutoPrinter.

    It is instantiated by passing it a LocalReport or a ServerReport,
    along with the document width, height, and margins.

    Here is how it is called:
            Dim AP As New AutoPrinter(SR, 8.5, 5, 0, 0, 0, 0, "Microsoft XPS Document Writer")         
            AP.PrintReport()
            AP.Dispose()
    
    

    Where SR is a ServerReport object or a LocalReport object.

    Here is a typical ServerReport subroutine that calls the autoprint class.
    The form has a ReportViewer control on it called RV.

    Here is the Server Side report routine:
        Sub Print_AllOut(ByVal OE As Integer) ' server side render
            RV.ProcessingMode = ProcessingMode.Remote
            Dim SR As ServerReport = RV.ServerReport  'RV is a ReportViewerControl
            SR.ReportServerUrl = New System.Uri(GetGlobal("ReportServerUrl"))
            SR.ReportPath = GetGlobal("OEAllOutPath")
            Dim ReportParameters(0) As ReportParameter
            ReportParameters(0) = New ReportParameter("OrderNumber", OE.ToString)
            SR.SetParameters(ReportParameters)
            Dim ds$ = SR.GetDataSources.Item(0).Name
            Dim dc(0) As Microsoft.Reporting.WinForms.DataSourceCredentials
            dc(0) = New Microsoft.Reporting.WinForms.DataSourceCredentials
            dc(0).Name = ds
            dc(0).Password = UserPassword()
            dc(0).UserId = UserID()
            SR.SetDataSourceCredentials(dc)
            Dim AP As New AutoPrinter(SR, 8.5, 5, 0, 0, 0, 0, "Microsoft XPS Document Writer")
            AP.PrintReport()
            AP.Dispose()
            RV.ServerReport.Refresh()
            Me.RV.RefreshReport()
        End Sub
    
    


    Here is a typical Local report routine
        Sub Print_AllOutL(ByVal OE As Integer)
            RV.ProcessingMode = ProcessingMode.Local
            RV.LocalReport.ReportPath = "OE_AllOuts.rdlc"
            RV.LocalReport.DataSources.Add(New ReportDataSource("Haffners", _
               LoadSalesData()))
            Dim AP As New AutoPrinter(RV.LocalReport, 8.5, 5, 0, 0, 0, 0, "Microsoft XPS Document Writer")
            AP.PrintReport()
            AP.Dispose()
            RV.ShowParameterPrompts = False
            RV.ShowCredentialPrompts = False
            RV.LocalReport.Refresh()
            Me.RV.RefreshReport()
        End Sub
    


    Both routines call the autoprint class the same way.

    Here is the class AutoPrint:
    Imports System.IO
    Imports System.Data
    Imports System.Text
    Imports System.Drawing.Imaging
    Imports System.Drawing.Printing
    Imports System.Collections.Generic
    Imports Microsoft.Reporting.WinForms
    Public Class AutoPrinter
        Implements IDisposable
        Private m_currentPageIndex As Integer = 0
        Private m_streams As IList(Of Stream) = Nothing
        Private m_LocalReport As LocalReport = Nothing
        Private m_ServerReport As ServerReport = Nothing
        Private m_PrinterName As String = ""
        Private m_Width As Double = 0
        Private m_Height As Double = 0
        Private m_TopMargin As Double = 0
        Private m_LeftMargin As Double = 0
        Private m_RightMargin As Double = 0
        Private m_BottomMargin As Double = 0
        Public RequestResult As String = ""
        Dim MimeType As String = ""
        Dim Encoding As String = ""
        Dim Extension As String = ""
        Dim m_Streamids As String() = Nothing
        Dim m_Streamid As String = ""
    
        Public Sub New(ByVal LocalReport As LocalReport, _
                ByVal Width As Double, _
                ByVal Height As Double, _
                ByVal TopMargin As Double, _
                ByVal LeftMargin As Double, _
                ByVal RightMargin As Double, _
                ByVal BottomMargin As Double, _
                ByVal PrinterName As String)
            m_LocalReport = LocalReport
            m_Height = Height
            m_Width = Width
            m_TopMargin = TopMargin
            m_LeftMargin = LeftMargin
            m_RightMargin = RightMargin
            m_BottomMargin = BottomMargin
            m_PrinterName = PrinterName
            RequestResult = ""
        End Sub
        Public Sub New(ByVal ServerReport As ServerReport, _
                ByVal Width As Double, _
                ByVal Height As Double, _
                ByVal TopMargin As Double, _
                ByVal LeftMargin As Double, _
                ByVal RightMargin As Double, _
                ByVal BottomMargin As Double, _
                ByVal PrinterName As String)
            m_ServerReport = ServerReport
            m_Height = Height
            m_Width = Width
            m_TopMargin = TopMargin
            m_LeftMargin = LeftMargin
            m_RightMargin = RightMargin
            m_BottomMargin = BottomMargin
            m_PrinterName = PrinterName
            RequestResult = ""
        End Sub
        Public Sub PrintReport()
            Export_Report()
            m_currentPageIndex = 0
            Print_Report()
        End Sub
        Private Sub Export_Report()
            Dim DeviceInfo As String = _
              "<DeviceInfo>" + _
              "  <OutputFormat>EMF</OutputFormat>" + _
              "  <PageWidth>WWWW</PageWidth>" + _
              "  <PageHeight>HHHH</PageHeight>" + _
              "  <MarginTop>TTTT</MarginTop>" + _
              "  <MarginLeft>LLLL</MarginLeft>" + _
              "  <MarginRight>RRRR</MarginRight>" + _
              "  <MarginBottom>BBBB</MarginBottom>" + _
              "</DeviceInfo>"
            DeviceInfo = DeviceInfo.Replace("WWWW", m_Width.ToString("0.00in"))
            DeviceInfo = DeviceInfo.Replace("HHHH", m_Width.ToString("0.00in"))
            DeviceInfo = DeviceInfo.Replace("TTTT", m_Width.ToString("0.00in"))
            DeviceInfo = DeviceInfo.Replace("LLLL", m_Width.ToString("0.00in"))
            DeviceInfo = DeviceInfo.Replace("RRRR", m_Width.ToString("0.00in"))
            DeviceInfo = DeviceInfo.Replace("BBBB", m_Width.ToString("0.00in"))
            Dim warnings() As Warning = Nothing
            m_streams = New List(Of Stream)()
            If m_LocalReport IsNot Nothing Then
                m_LocalReport.Render("Image", DeviceInfo, AddressOf CreateStream, _
                   warnings)
            End If
            If m_ServerReport IsNot Nothing Then
                Dim myStream As New MemoryStream( _
                m_ServerReport.Render("Image", DeviceInfo, MimeType, _
                    Encoding, Extension, m_Streamids, warnings))
                m_streams.Add(myStream)
                Dim myImage As Byte()
                For Each m_Streamid In m_Streamids
                    myImage = m_ServerReport.RenderStream("Image", m_Streamid, _
                         Nothing, Nothing, Nothing)
                    myStream = New MemoryStream(myImage)
                    m_streams.Add(myStream)
                Next
            End If
            Dim Stream As Stream
            For Each stream In m_streams
                stream.Position = 0
            Next
        End Sub
        Private Function CreateStream(ByVal name As String, _
               ByVal fileNameExtension As String, _
               ByVal encoding As Encoding, ByVal mimeType As String, _
               ByVal willSeek As Boolean) As Stream
            Dim stream As New MemoryStream
            m_streams.Add(stream)
            Return stream
        End Function
        Private Sub Print_Report()
            If m_streams Is Nothing Or m_streams.Count = 0 Then
                Return
            End If
            Dim PrintDoc As New PrintDocument()
            'The following line removes the printing page ?? of ?? messagebox
            PrintDoc.PrintController = New System.Drawing.Printing.StandardPrintController()
            PrintDoc.PrinterSettings.PrinterName = m_PrinterName
            If Not PrintDoc.PrinterSettings.IsValid Then
                Dim msg As String = String.Format( _
                    "Can't find printer ""{0}"".", m_PrinterName)
                Console.WriteLine(msg)
                Return
            End If
            AddHandler PrintDoc.PrintPage, AddressOf PrintPage
            PrintDoc.Print()
        End Sub
        Private Sub PrintPage(ByVal sender As Object, _
                ByVal ev As PrintPageEventArgs)
            Dim PageImage As New Metafile(m_streams(m_currentPageIndex))
            ev.Graphics.DrawImage(PageImage, ev.PageBounds)
            Debug.Print(m_currentPageIndex.ToString)
            m_currentPageIndex += 1
            ev.HasMorePages = (m_currentPageIndex < m_streams.Count)
        End Sub
        Public Overloads Sub Dispose() Implements IDisposable.Dispose
            If Not (m_streams Is Nothing) Then
                Dim stream As Stream
                For Each stream In m_streams
                    stream.Close()
                Next
                m_streams = Nothing
            End If
        End Sub
    End Class
    
    

    This method will print to the printer indicated without running the print dialogue.
    It will also surpress the "Printing page ?? of ??" message box.

    The routine will display a message box if the printer passed to the routine is not a valid printer.

    This has worked well for me and i hope it will help others as well.
    Thanks
    Jerry Cicierega
    • Marked As Answer byJerryCic Sunday, November 15, 2009 3:42 AM
    •  

All Replies

  • Sunday, November 15, 2009 3:28 AMJerryCic Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    Here is my solution

    I created a class object called AutoPrinter.

    It is instantiated by passing it a LocalReport or a ServerReport,
    along with the document width, height, and margins.

    Here is how it is called:
            Dim AP As New AutoPrinter(SR, 8.5, 5, 0, 0, 0, 0, "Microsoft XPS Document Writer")         
            AP.PrintReport()
            AP.Dispose()
    
    

    Where SR is a ServerReport object or a LocalReport object.

    Here is a typical ServerReport subroutine that calls the autoprint class.
    The form has a ReportViewer control on it called RV.

    Here is the Server Side report routine:
        Sub Print_AllOut(ByVal OE As Integer) ' server side render
            RV.ProcessingMode = ProcessingMode.Remote
            Dim SR As ServerReport = RV.ServerReport  'RV is a ReportViewerControl
            SR.ReportServerUrl = New System.Uri(GetGlobal("ReportServerUrl"))
            SR.ReportPath = GetGlobal("OEAllOutPath")
            Dim ReportParameters(0) As ReportParameter
            ReportParameters(0) = New ReportParameter("OrderNumber", OE.ToString)
            SR.SetParameters(ReportParameters)
            Dim ds$ = SR.GetDataSources.Item(0).Name
            Dim dc(0) As Microsoft.Reporting.WinForms.DataSourceCredentials
            dc(0) = New Microsoft.Reporting.WinForms.DataSourceCredentials
            dc(0).Name = ds
            dc(0).Password = UserPassword()
            dc(0).UserId = UserID()
            SR.SetDataSourceCredentials(dc)
            Dim AP As New AutoPrinter(SR, 8.5, 5, 0, 0, 0, 0, "Microsoft XPS Document Writer")
            AP.PrintReport()
            AP.Dispose()
            RV.ServerReport.Refresh()
            Me.RV.RefreshReport()
        End Sub
    
    


    Here is a typical Local report routine
        Sub Print_AllOutL(ByVal OE As Integer)
            RV.ProcessingMode = ProcessingMode.Local
            RV.LocalReport.ReportPath = "OE_AllOuts.rdlc"
            RV.LocalReport.DataSources.Add(New ReportDataSource("Haffners", _
               LoadSalesData()))
            Dim AP As New AutoPrinter(RV.LocalReport, 8.5, 5, 0, 0, 0, 0, "Microsoft XPS Document Writer")
            AP.PrintReport()
            AP.Dispose()
            RV.ShowParameterPrompts = False
            RV.ShowCredentialPrompts = False
            RV.LocalReport.Refresh()
            Me.RV.RefreshReport()
        End Sub
    


    Both routines call the autoprint class the same way.

    Here is the class AutoPrint:
    Imports System.IO
    Imports System.Data
    Imports System.Text
    Imports System.Drawing.Imaging
    Imports System.Drawing.Printing
    Imports System.Collections.Generic
    Imports Microsoft.Reporting.WinForms
    Public Class AutoPrinter
        Implements IDisposable
        Private m_currentPageIndex As Integer = 0
        Private m_streams As IList(Of Stream) = Nothing
        Private m_LocalReport As LocalReport = Nothing
        Private m_ServerReport As ServerReport = Nothing
        Private m_PrinterName As String = ""
        Private m_Width As Double = 0
        Private m_Height As Double = 0
        Private m_TopMargin As Double = 0
        Private m_LeftMargin As Double = 0
        Private m_RightMargin As Double = 0
        Private m_BottomMargin As Double = 0
        Public RequestResult As String = ""
        Dim MimeType As String = ""
        Dim Encoding As String = ""
        Dim Extension As String = ""
        Dim m_Streamids As String() = Nothing
        Dim m_Streamid As String = ""
    
        Public Sub New(ByVal LocalReport As LocalReport, _
                ByVal Width As Double, _
                ByVal Height As Double, _
                ByVal TopMargin As Double, _
                ByVal LeftMargin As Double, _
                ByVal RightMargin As Double, _
                ByVal BottomMargin As Double, _
                ByVal PrinterName As String)
            m_LocalReport = LocalReport
            m_Height = Height
            m_Width = Width
            m_TopMargin = TopMargin
            m_LeftMargin = LeftMargin
            m_RightMargin = RightMargin
            m_BottomMargin = BottomMargin
            m_PrinterName = PrinterName
            RequestResult = ""
        End Sub
        Public Sub New(ByVal ServerReport As ServerReport, _
                ByVal Width As Double, _
                ByVal Height As Double, _
                ByVal TopMargin As Double, _
                ByVal LeftMargin As Double, _
                ByVal RightMargin As Double, _
                ByVal BottomMargin As Double, _
                ByVal PrinterName As String)
            m_ServerReport = ServerReport
            m_Height = Height
            m_Width = Width
            m_TopMargin = TopMargin
            m_LeftMargin = LeftMargin
            m_RightMargin = RightMargin
            m_BottomMargin = BottomMargin
            m_PrinterName = PrinterName
            RequestResult = ""
        End Sub
        Public Sub PrintReport()
            Export_Report()
            m_currentPageIndex = 0
            Print_Report()
        End Sub
        Private Sub Export_Report()
            Dim DeviceInfo As String = _
              "<DeviceInfo>" + _
              "  <OutputFormat>EMF</OutputFormat>" + _
              "  <PageWidth>WWWW</PageWidth>" + _
              "  <PageHeight>HHHH</PageHeight>" + _
              "  <MarginTop>TTTT</MarginTop>" + _
              "  <MarginLeft>LLLL</MarginLeft>" + _
              "  <MarginRight>RRRR</MarginRight>" + _
              "  <MarginBottom>BBBB</MarginBottom>" + _
              "</DeviceInfo>"
            DeviceInfo = DeviceInfo.Replace("WWWW", m_Width.ToString("0.00in"))
            DeviceInfo = DeviceInfo.Replace("HHHH", m_Width.ToString("0.00in"))
            DeviceInfo = DeviceInfo.Replace("TTTT", m_Width.ToString("0.00in"))
            DeviceInfo = DeviceInfo.Replace("LLLL", m_Width.ToString("0.00in"))
            DeviceInfo = DeviceInfo.Replace("RRRR", m_Width.ToString("0.00in"))
            DeviceInfo = DeviceInfo.Replace("BBBB", m_Width.ToString("0.00in"))
            Dim warnings() As Warning = Nothing
            m_streams = New List(Of Stream)()
            If m_LocalReport IsNot Nothing Then
                m_LocalReport.Render("Image", DeviceInfo, AddressOf CreateStream, _
                   warnings)
            End If
            If m_ServerReport IsNot Nothing Then
                Dim myStream As New MemoryStream( _
                m_ServerReport.Render("Image", DeviceInfo, MimeType, _
                    Encoding, Extension, m_Streamids, warnings))
                m_streams.Add(myStream)
                Dim myImage As Byte()
                For Each m_Streamid In m_Streamids
                    myImage = m_ServerReport.RenderStream("Image", m_Streamid, _
                         Nothing, Nothing, Nothing)
                    myStream = New MemoryStream(myImage)
                    m_streams.Add(myStream)
                Next
            End If
            Dim Stream As Stream
            For Each stream In m_streams
                stream.Position = 0
            Next
        End Sub
        Private Function CreateStream(ByVal name As String, _
               ByVal fileNameExtension As String, _
               ByVal encoding As Encoding, ByVal mimeType As String, _
               ByVal willSeek As Boolean) As Stream
            Dim stream As New MemoryStream
            m_streams.Add(stream)
            Return stream
        End Function
        Private Sub Print_Report()
            If m_streams Is Nothing Or m_streams.Count = 0 Then
                Return
            End If
            Dim PrintDoc As New PrintDocument()
            'The following line removes the printing page ?? of ?? messagebox
            PrintDoc.PrintController = New System.Drawing.Printing.StandardPrintController()
            PrintDoc.PrinterSettings.PrinterName = m_PrinterName
            If Not PrintDoc.PrinterSettings.IsValid Then
                Dim msg As String = String.Format( _
                    "Can't find printer ""{0}"".", m_PrinterName)
                Console.WriteLine(msg)
                Return
            End If
            AddHandler PrintDoc.PrintPage, AddressOf PrintPage
            PrintDoc.Print()
        End Sub
        Private Sub PrintPage(ByVal sender As Object, _
                ByVal ev As PrintPageEventArgs)
            Dim PageImage As New Metafile(m_streams(m_currentPageIndex))
            ev.Graphics.DrawImage(PageImage, ev.PageBounds)
            Debug.Print(m_currentPageIndex.ToString)
            m_currentPageIndex += 1
            ev.HasMorePages = (m_currentPageIndex < m_streams.Count)
        End Sub
        Public Overloads Sub Dispose() Implements IDisposable.Dispose
            If Not (m_streams Is Nothing) Then
                Dim stream As Stream
                For Each stream In m_streams
                    stream.Close()
                Next
                m_streams = Nothing
            End If
        End Sub
    End Class
    
    

    This method will print to the printer indicated without running the print dialogue.
    It will also surpress the "Printing page ?? of ??" message box.

    The routine will display a message box if the printer passed to the routine is not a valid printer.

    This has worked well for me and i hope it will help others as well.
    Thanks
    Jerry Cicierega
    • Marked As Answer byJerryCic Sunday, November 15, 2009 3:42 AM
    •