Monday, February 13, 2012

Auto Print

I am using Visual studio 2005. This may be a vb question but it deals with reporting services.

I have a local report using the reportviewer control. (rdcl 2005 fmt)

It displays perfectly, and when i click the print button it prints flawlessly.

How do i print the document to the default printer automatically?

This seems like a basic function that i can't seem to find the answer to.

Thanx

Jerry Cicierega

I found the answer. Its not as bad as it looks. Thois is a form called "slip.vb" I am printing to a slip printer. The form has a rpt viewer on it. The form nevers shows. It is just used as a containor for the report. I created the report as an RDL report. I coppied it into the program folder and re-named it to a RDCL extension. I then established the datasource. and it worked interactivly. I then added the following code and presto. Auto Print. I also asume the default printer is the printer of choice.

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 Slip

Private m_currentPageIndex As Integer

Private m_streams As IList(Of Stream)

Public Sub Print_Slip()

Me.TxnHistoryTableAdapter.Connection.ConnectionString = My.Utilities.ConnectionString

Me.TxnHistoryTableAdapter.FillBy(Me.PCZonDataSet.TxnHistory, My.GlobalVariables.ComputerData.ComputerName, My.GlobalVariables.ComputerData.Sequencenumber)

Export(Me.ReportViewer1.LocalReport)

m_currentPageIndex = 0

Print()

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 Stream = New FileStream(name + "." + fileNameExtension, FileMode.Create)

m_streams.Add(stream)

Return stream

End Function

Private Sub Export(ByVal report As LocalReport)

Dim deviceInfo As String = _

"<DeviceInfo>" + _

" <OutputFormat>EMF</OutputFormat>" + _

" <PageWidth>9in</PageWidth>" + _

" <PageHeight>11in</PageHeight>" + _

" <MarginTop>0.0in</MarginTop>" + _

" <MarginLeft>0.0in</MarginLeft>" + _

" <MarginRight>0.25in</MarginRight>" + _

" <MarginBottom>0.25in</MarginBottom>" + _

"</DeviceInfo>"

Dim warnings() As Warning = Nothing

m_streams = New List(Of Stream)()

report.Render("Image", deviceInfo, _

AddressOf CreateStream, warnings)

Dim stream As Stream

For Each stream In m_streams

stream.Position = 0

Next

End Sub

Private Sub Print()

If m_streams Is Nothing Or m_streams.Count = 0 Then

Return

End If

Dim printDoc As New PrintDocument()

AddHandler printDoc.PrintPage, AddressOf PrintPage

printDoc.Print()

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

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)

m_currentPageIndex += 1

ev.HasMorePages = (m_currentPageIndex < m_streams.Count)

End Sub

End Class

No comments:

Post a Comment