I have a report created by report designer with a RDLC extension. I can display it with the reportviewer control but i would like to print this report directly to the printer after the user clicks a print report button. Does anyone know how?
Report.ReportPath = "C:\InspectionReport.rdlc"
Report.DataSources.Add(New Microsoft.Reporting.WinForms.ReportDataSource _
("InspectionReportDS_Inspection_Report", Me.InspectionReportDS.Inspection_Report))
PrintReport.Export(Report)
PrintReport.m_currentPageIndex = 0
PrintReport.Print()
that calls the following class
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 Reporting
Implements IDisposable
Public m_currentPageIndex As Integer
Public m_streams As IList(Of Stream)
Public 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 Stream = _
New FileStream("C:\" + name + "." + fileNameExtension, FileMode.Create)
m_streams.Add(stream)
Return stream
End Function
Public Sub Export(ByVal report As LocalReport)
Dim deviceInfo As String = _
"<DeviceInfo>" + _
" <OutputFormat>EMF</OutputFormat>" + _
" <PageWidth>8.5in</PageWidth>" + _
" <PageHeight>11in</PageHeight>" + _
" <MarginTop>0.25in</MarginTop>" + _
" <MarginLeft>0.25in</MarginLeft>" + _
" <MarginRight>0.25in</MarginRight>" + _
" <MarginBottom>0.25in</MarginBottom>" + _
"</DeviceInfo>"
Dim warnings() As Warning = Nothing
m_streams = New List(Of Stream)()
'' report.Render("Image", deviceInfo, AddressOf CreateStream, warnings)
Try
report.Render("Image", deviceInfo, AddressOf CreateStream, warnings)
Catch e As System.Exception
Dim inner As Exception = e.InnerException
While Not (inner Is Nothing)
MsgBox(inner.Message)
inner = inner.InnerException
End While
End Try
' Dim stream As Stream
' For Each stream In m_streams
'Stream.Position = 0
' Next
End Sub
Public Sub PrintPage(ByVal sender As Object, ByVal ev As PrintPageEventArgs)
' Dim pageImage As New Metafile(m_streams(m_currentPageIndex))
Dim pageImage As New Metafile(m_streams(m_currentPageIndex))
ev.Graphics.DrawImage(pageImage, ev.PageBounds)
m_currentPageIndex += 1
ev.HasMorePages = (m_currentPageIndex < m_streams.Count)
End Sub
Public Sub Print()
' Const printerName As String = "Microsoft Office Document Image Writer"
Const printerName As String = "Laser on Tiger"
If m_streams Is Nothing OrElse m_streams.Count = 0 Then
Return
End If
Dim printDoc As New PrintDocument()
' printDoc.PrinterSettings..PrinterName = printerName
If Not printDoc.PrinterSettings.IsValid Then
Dim msg As String = String.Format( _
"Can't find printer ""{0}"".", printerName)
Console.WriteLine(msg)
Return
End If
AddHandler printDoc.PrintPage, AddressOf PrintPage
printDoc.Print()
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