هذا كودي بعد التعديل عليه قليلا
في الكلاس
Imports System.IO
Imports System.Text
Imports System.Drawing.Imaging
Imports System.Drawing.Printing
Imports Microsoft.Reporting.WinForms
Public Class MyLocalReport
Implements IDisposable
Private report As New LocalReport()
Private document As New PrintDocument()
Private m_currentPageIndex As Integer
Private m_streams As IList(Of Stream)
Public Sub Print(Optional ByVal printerSetting As PrinterSettings = Nothing)
If printerSetting IsNot Nothing Then
document.PrinterSettings = printerSetting
End If
Export(report)
If m_streams Is Nothing OrElse m_streams.Count = 0 Then
Throw New Exception("Error: no stream to print.")
End If
If Not document.PrinterSettings.IsValid Then
Throw New Exception("Error: cannot find the default printer.")
Else
AddHandler document.PrintPage, AddressOf PrintPage
m_currentPageIndex = 0
document.Print()
End If
End Sub
' Routine to provide to the report renderer, in order to
' save an image for each page of the report.
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 Stream = New MemoryStream()
m_streams.Add(stream)
Return stream
End Function
' Export the given report as an EMF (Enhanced Metafile) file.
Private Sub Export(ByVal report As LocalReport)
Dim deviceInfo As String =
"<DeviceInfo>" & _
" <OutputFormat>EMF</OutputFormat>" + _
" <PageWidth>8.27in</PageWidth>" + _
" <PageHeight>11.69in</PageHeight>" + _
" <MarginTop>0.3937in</MarginTop>" + _
" <MarginLeft>0.3937in</MarginLeft>" + _
" <MarginRight>0.3937in</MarginRight>" + _
" <MarginBottom>0.7874in</MarginBottom>" + _
"</DeviceInfo>"
Dim warnings As Warning() = Nothing
m_streams = New List(Of Stream)()
report.Render("Image", deviceInfo, AddressOf CreateStream, warnings)
For Each stream As Stream In m_streams
stream.Position = 0
Next
End Sub
' 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)
End Sub
Private _ReportName As String
Public Property ReportName As String
Get
Return _ReportName
End Get
Set(ByVal value As String)
_ReportName = value
report.ReportEmbeddedResource = Me.GetType().Namespace & "." & _ReportName
End Set
End Property
Private _ReportPath As String
Public Property ReportPath As String
Get
Return _ReportPath
End Get
Set(ByVal value As String)
_ReportPath = value
report.ReportPath = _ReportPath
End Set
End Property
Private _DataSources As ReportDataSource
Public Property DataSources As ReportDataSource
Get
Return _DataSources
End Get
Set(ByVal value As ReportDataSource)
_DataSources = value
report.DataSources.Clear()
report.DataSources.Add(value)
End Set
End Property
Public Sub SetParameters(ByVal parameter As ReportParameter)
report.SetParameters(parameter)
End Sub
Public 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
End Class
وعند طلب الكلاس
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Dim myReport As New MyLocalReport
'' تحديد ملف تقرير خارجي
'myReport.ReportPath = "C:\Report1.rdlc"
' تحديد ملف مدمج في المشروع
myReport.ReportName = "Report1.rdlc"
' تحديد مصدر البيانات اذا كان التقرير مصمم على اساسها
myReport.DataSources = New ReportDataSource("DataSet1", CType(WosysDBDataSet.Main, DataTable))
' الطباعه على الطابعه الافتراضيه
myReport.Print()
End Sub