How do I … directly printing a project report.rdlc in VB.Net without previewing first
23-Feb-1212 Leave a comment
Also see next blog entry
Steps
- Imports System
Imports System.IO
Imports System.Data
Imports System.Text
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Drawing.Printing
Imports System.Collections.Generic
Imports System.Windows.Forms
Imports Microsoft.Reporting.WinForms - Load data as usual
- Dim report as New LocalReport
report.ReportPath = “<ReportName>.rdlc”
or
report.ReportPath = “Reports\<ReportName>.rdlc
report.DataSources.Add(New ReportDataSource(“dtRouteCardWO”, CType(Me.DsRouteCard1.RouteCardWO, DataTable)))
Export(report)
Print() - Private Sub Export(ByVal report As LocalReport)
‘MsgBox(“Start of Export”) ‘Reaches this line on EBC Rise TS
‘then before “End of Export” get “Error occurred during local report processing” Dim deviceInfo = “<DeviceInfo>” & _
“<OutputFormat>EMF</OutputFormat>” & _
“<PageWidth>8.3in</PageWidth>” & _
“<PageHeight>11.7in</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)()‘See my blog. For this to work the Build Action must be changed from Embedded Resource to Content and a Copy policy chosen
‘For this to work from a different project. Add this file as a link in that project and then do above on linked file.
report.Render(“Image”, deviceInfo, AddressOf CreateStream, warnings)
For Each stream As Stream In m_streams
stream.Position = 0
Next‘MsgBox(“End of Export”)
End Sub - Private Function CreateStream(ByVal name As String, ByVal fileNameExtension As String, ByVal encoding As System.Text.Encoding, ByVal mimeType As String, ByVal willSeek As Boolean) As Stream
Dim stream As Stream = New MemoryStream()
m_streams.Add(stream)
Return stream
End Function - Private m_currentPageIndex As Integer
Private m_streams As IList(Of Stream) - ‘ Handler for PrintPageEvents
Private Sub PrintPage(ByVal sender As Object, ByVal ev As PrintPageEventArgs)
Dim pageImage As New Metafile(m_streams(m_currentPageIndex)) ‘ Adjust rectangular area with printer margins.
Dim adjustedRect As New Rectangle(ev.PageBounds.Left – CInt(ev.PageSettings.HardMarginX), _
ev.PageBounds.Top – CInt(ev.PageSettings.HardMarginY), _
ev.PageBounds.Width, _
ev.PageBounds.Height)‘ Draw a white background for the report
ev.Graphics.FillRectangle(Brushes.White, adjustedRect)‘ Draw the report content
ev.Graphics.DrawImage(pageImage, adjustedRect)‘ Prepare for the next page. Make sure we haven’t hit the end.
m_currentPageIndex += 1
ev.HasMorePages = (m_currentPageIndex < m_streams.Count)
ev.HasMorePages = False ‘#1074 srs fudge. Routecard was on 2 pages, should always be 1
End SubPrivate Sub Print()
‘MsgBox(“Start of Print”)
If m_streams Is Nothing OrElse m_streams.Count = 0 Then
Throw New Exception(“Error: no stream to print.”)
End If
Dim printDoc As New PrintDocument()
If Not printDoc.PrinterSettings.IsValid Then
Throw New Exception(“Error: cannot find the default printer.”)
Else
AddHandler printDoc.PrintPage, AddressOf PrintPage
m_currentPageIndex = 0
printDoc.Print()
End If
‘MsgBox(“End of Print”)
End Sub - Public Shadows Sub Dispose() Implements IDisposable.Dispose
If m_streams IsNot Nothing Then
For Each stream As Stream In m_streams
stream.Close()
Next
m_streams = Nothing
End If
End Sub
Troubleshooting
- An error occurred during local report processing copy
The report definition for report ‘..\some_path_to_report\<reportname>.rdlc’ has not been specified
Could not find file ‘C:\inetpub\wwwroot\some_path_to_report\tse.rdlc’. - Could not find file ‘C:\inetpub\wwwroot\some_path_to_report\tse.rdlc’.