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 AutoPrinter
Implements IDisposable
Private m_currentPageIndex As Integer = 0
Private m_streams As IList(Of Stream) = Nothing
Private m_LocalReport As LocalReport = Nothing
Private m_ServerReport As ServerReport = Nothing
Private m_PrinterName As String = ""
Private m_Width As Double = 0
Private m_Height As Double = 0
Private m_TopMargin As Double = 0
Private m_LeftMargin As Double = 0
Private m_RightMargin As Double = 0
Private m_BottomMargin As Double = 0
Public RequestResult As String = ""
Dim MimeType As String = ""
Dim Encoding As String = ""
Dim Extension As String = ""
Dim m_Streamids As String() = Nothing
Dim m_Streamid As String = ""
Public Sub New(ByVal LocalReport As LocalReport, _
ByVal Width As Double, _
ByVal Height As Double, _
ByVal TopMargin As Double, _
ByVal LeftMargin As Double, _
ByVal RightMargin As Double, _
ByVal BottomMargin As Double, _
ByVal PrinterName As String)
m_LocalReport = LocalReport
m_Height = Height
m_Width = Width
m_TopMargin = TopMargin
m_LeftMargin = LeftMargin
m_RightMargin = RightMargin
m_BottomMargin = BottomMargin
m_PrinterName = PrinterName
RequestResult = ""
End Sub
Public Sub New(ByVal ServerReport As ServerReport, _
ByVal Width As Double, _
ByVal Height As Double, _
ByVal TopMargin As Double, _
ByVal LeftMargin As Double, _
ByVal RightMargin As Double, _
ByVal BottomMargin As Double, _
ByVal PrinterName As String)
m_ServerReport = ServerReport
m_Height = Height
m_Width = Width
m_TopMargin = TopMargin
m_LeftMargin = LeftMargin
m_RightMargin = RightMargin
m_BottomMargin = BottomMargin
m_PrinterName = PrinterName
RequestResult = ""
End Sub
Public Sub PrintReport()
Export_Report()
m_currentPageIndex = 0
Print_Report()
End Sub
Private Sub Export_Report()
Dim DeviceInfo As String = _
"<DeviceInfo>" + _
" <OutputFormat>EMF</OutputFormat>" + _
" <PageWidth>WWWW</PageWidth>" + _
" <PageHeight>HHHH</PageHeight>" + _
" <MarginTop>TTTT</MarginTop>" + _
" <MarginLeft>LLLL</MarginLeft>" + _
" <MarginRight>RRRR</MarginRight>" + _
" <MarginBottom>BBBB</MarginBottom>" + _
"</DeviceInfo>"
DeviceInfo = DeviceInfo.Replace("WWWW", m_Width.ToString("0.00in"))
DeviceInfo = DeviceInfo.Replace("HHHH", m_Width.ToString("0.00in"))
DeviceInfo = DeviceInfo.Replace("TTTT", m_Width.ToString("0.00in"))
DeviceInfo = DeviceInfo.Replace("LLLL", m_Width.ToString("0.00in"))
DeviceInfo = DeviceInfo.Replace("RRRR", m_Width.ToString("0.00in"))
DeviceInfo = DeviceInfo.Replace("BBBB", m_Width.ToString("0.00in"))
Dim warnings() As Warning = Nothing
m_streams = New List(Of Stream)()
If m_LocalReport IsNot Nothing Then
m_LocalReport.Render("Image", DeviceInfo, AddressOf CreateStream, _
warnings)
End If
If m_ServerReport IsNot Nothing Then
Dim myStream As New MemoryStream( _
m_ServerReport.Render("Image", DeviceInfo, MimeType, _
Encoding, Extension, m_Streamids, warnings))
m_streams.Add(myStream)
Dim myImage As Byte()
For Each m_Streamid As String In m_Streamids
myImage = m_ServerReport.RenderStream("Image", m_Streamid, _
Nothing, Nothing, Nothing)
myStream = New MemoryStream(myImage)
m_streams.Add(myStream)
Next
End If
Dim Stream As Stream
For Each stream In m_streams
stream.Position = 0
Next
End Sub
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 New MemoryStream
m_streams.Add(stream)
Return stream
End Function
Private Sub Print_Report()
If m_streams Is Nothing Or m_streams.Count = 0 Then
Return
End If
Dim PrintDoc As New PrintDocument()
'The following line removes the printing page ?? of ?? messagebox
PrintDoc.PrintController = New System.Drawing.Printing.StandardPrintController()
PrintDoc.PrinterSettings.PrinterName = m_PrinterName
If Not PrintDoc.PrinterSettings.IsValid Then
Dim msg As String = String.Format( _
"Can't find printer ""{0}"".", m_PrinterName)
Console.WriteLine(msg)
Return
End If
AddHandler PrintDoc.PrintPage, AddressOf PrintPage
PrintDoc.Print()
End Sub
Private Sub PrintPage(ByVal sender As Object, _
ByVal ev As PrintPageEventArgs)
Dim PageImage As New Metafile(m_streams(m_currentPageIndex))
ev.Graphics.DrawImage(PageImage, ev.PageBounds)
Debug.Print(m_currentPageIndex.ToString)
m_currentPageIndex += 1
ev.HasMorePages = (m_currentPageIndex < m_streams.Count)
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
Dim ReportViewer1 As New ReportViewer
ReportViewer1.ProcessingMode = ProcessingMode.Local
ReportViewer1.LocalReport.ReportPath = "..\..\Report\Address.rdlc"
ReportViewer1.LocalReport.DataSources.Add(New ReportDataSource(ReportViewer1.LocalReport.GetDataSourceNames()(0), dt))
Dim AP As New AutoPrinter(ReportViewer1.LocalReport, 11.69, 8.27, 0.5, 0.5, 0.5, 0.5, ps.PrinterName)
AP.PrintReport()
AP.Dispose()
ReportViewer1.RefreshReport()
ReportViewer1.LocalReport.Refresh()
No comments:
Post a Comment