Print SSRS report directly to a printer without viewing using vb.net

1. Add a form PrintReport.vb

2. Add a report viewer in the form. configure the report viewer property (server url, path)

3.  code behind:

 

Imports System.Drawing.Printing
Imports System.Drawing.Imaging
Imports System.IO
Imports Microsoft.Reporting.WinForms

Public Class PrintReport
Dim pages As New List(Of Metafile)
Dim pageIndex As Integer = 0
Dim doc As New Printing.PrintDocument()

Private Sub PrintReport_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim doc As New Printing.PrintDocument()
doc = New Printing.PrintDocument()
AddHandler doc.PrintPage, AddressOf PrintPageHandler
Dim dialog As New PrintDialog()
dialog.Document = doc
Dim print As DialogResult
print = dialog.ShowDialog()

doc.PrinterSettings = dialog.PrinterSettings

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 Microsoft.Reporting.WinForms.Warning ‘Warning
Dim streamids() As String
Dim mimeType, encoding, filenameExtension, path As String
mimeType = “” : encoding = “” : filenameExtension = “”

‘Report Parameter Input. Add form text box as per your requirements

Dim SALES_ORDER_NUMBER As String
Dim INVOICE_NUMBER As String
SALES_ORDER_NUMBER = CRMInvoicing.txtSALES_ORDER_NUMBER.Text
INVOICE_NUMBER = CRMInvoicing.txtINVOICE_NUMBER.Text
Dim parmSO As New ReportParameter(“SALES_ORDER_NUMBER”, SALES_ORDER_NUMBER)
Dim parmI As New ReportParameter(“INVOICE_NUMBER”, INVOICE_NUMBER)
Dim parmSO1(1) As ReportParameter
parmSO1(0) = parmSO
parmSO1(1) = parmI

Dim data() As Byte
rpt_control.ServerReport.SetParameters(parmSO1)
data = rpt_control.ServerReport.Render(“Image”, deviceInfo, mimeType, encoding, filenameExtension, streamids, warnings)
pages.Add(New Metafile(New MemoryStream(data)))

For Each pageName As String In streamids
data = rpt_control.ServerReport.RenderStream(“Image”, pageName, deviceInfo, mimeType, encoding)
pages.Add(New Metafile(New MemoryStream(data)))
Next
doc.Print()

Me.rpt_control.RefreshReport()
End Sub

Private Sub PrintPageHandler(ByVal sender As Object, ByVal e As PrintPageEventArgs)
Dim page As Metafile = pages(pageIndex)
pageIndex += 1
Dim pWidth As Integer = 827
Dim pHeight As Integer = 1100
e.Graphics.DrawImage(page, 0, 0, pWidth, pHeight)
‘e.Graphics.DrawImage(page, 0, 0, page.Width, page.Height)
e.HasMorePages = pageIndex < pages.Count
End Sub

End Class

 

 

Further Details here

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s