Visual Studio Development Bookmark and Share   
 index > Visual Studio Report Controls > Print server report directly
 

Print server report directly

I've seen the examples of printing an rdlc file directly, but how would I do this for an rdl file rendered by the reporting server? Is it at all possible?

TIA

GraemeH  Thursday, June 15, 2006 2:29 PM

Hi, I've just managed it. Please take a look at the following code and see what you think. It was pulled from a lot of sources and there has been alot of experimentation so there will be lots of loose ends(and because I don't understand much of it) I'm sure there is plenty wrong with it, let me know what you think.

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 clsPrintReport_PickingList

Implements IDisposable

Private _Printer As String

Dim iPageCount As Integer

Dim Report As ReportViewer

Public Property pPrinter() As String

Get

Return _Printer

End Get

Set(ByVal value As String)

_Printer = value

End Set

End Property

Private m_currentPageIndex As Integer

Private m_streams As IList(Of Stream)

Private _Rendered As Byte()

Private Sub PrintPage(ByVal sender As Object, ByVal ev As PrintPageEventArgs)

Try

'm_stream.Position = 0

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)

Catch ex As Exception

fnHandleError(ex, "clsPrintReport_PickingList.PrintPage")

End Try

End Sub

Private Sub Print()

Try

If m_streams Is Nothing Or m_streams.Count = 0 Then

Return

End If

Dim printDoc As New PrintDocument()

printDoc.PrinterSettings.PrinterName = _Printer

If Not printDoc.PrinterSettings.IsValid Then

Dim msg As String = String.Format("Can't find printer ""{0}"".", _Printer)

Console.WriteLine(msg)

Return

End If

AddHandler printDoc.PrintPage, AddressOf PrintPage

printDoc.Print()

Catch ex As Exception

fnHandleError(ex, "clsPrintReport_PickingList.Print")

End Try

End Sub

Private Sub Render(ByVal Report As ServerReport)

Dim warnings As Warning() = Nothing

Dim streamids As String() = Nothing

Dim mimeType As String = Nothing

Dim encoding As String = Nothing

Dim extension As String = Nothing 'Nothing

Dim deviceInfo As String

Dim Format As String = "Image"

deviceInfo = _

"<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 streamid As String

Dim myParameters(0) As ReportParameter

Try

'Parameters = Nothing

m_streams = New List(Of Stream)()

'm_stream = New FileStream("..\..\" + "Render", FileMode.Create)

myParameters(0) = New ReportParameter("OrderNo", 63263, False)

Report.SetParameters(myParameters)

_Rendered = Report.Render(Format, deviceInfo, mimeType, encoding, extension, streamids, warnings)

Dim stream As New FileStream("..\..\" + "Main", FileMode.Create)

stream.Write(_Rendered, 0, CInt(_Rendered.Length))

m_streams.Add(stream)

'stream.Close()

'_Rendered = Report.RenderStream(Format, streamids(0), deviceInfo, mimeType, encoding)

For Each streamid In streamids

_Rendered = Report.RenderStream(Format, streamid, deviceInfo, mimeType, encoding)

stream = New FileStream("..\..\" + streamid, FileMode.Create)

stream.Write(_Rendered, 0, CInt(_Rendered.Length))

m_streams.Add(stream)

'stream.Close()

Next

For Each stream In m_streams

stream.Position = 0

Next

Catch ex As Exception

fnHandleError(ex, "clsPrintReport_PickingList.Render")

End Try

End Sub

Public Sub Run()

Try

Report.ServerReport.ReportPath = "/Despatch Reports/PickingList_Pallets"

Report.ServerReport.ReportServerUrl = New Uri("http://itsdeesr02/reportserver")

Dim myParameters(0) As ReportParameter

myParameters(0) = New ReportParameter("OrderNo", 63263, False)

Report.ServerReport.SetParameters(myParameters)

Report.RefreshReport()

Render(Report.ServerReport)

m_currentPageIndex = 0

Print()

Catch ex As Exception

fnHandleError(ex, "clsPrintReport_PickingList.Run")

End Try

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

Public Sub New()

Report = New ReportViewer

End Sub

End Class

Darren France  Monday, July 03, 2006 1:56 PM
Thank you very much Darren for posting the code.  I finally be able to print my server report directly.
KGaun  Thursday, October 08, 2009 5:40 PM

You can use google to search for other answers

Custom Search

More Threads

• subreport not filtering
• Displaying Data In the Report
• visual studio 2005 - unable to open an analysis services database
• TableAdapter Question
• ReportViewer, Interactive Sort dropping graphic
• Multiple subreports on the same rdlc report
• Printing to printer using VS 2005 Web Browser Control error
• ReportViewer & AJAX & JavaScript Errors
• Unable to install ReportViewer 8.0.50727.817 assemblies to GAC
• LocalReport.GetDefaultPageSettings NOT return correct RDLC values.