Use a report object to print and format - much more reliable/easier that
writing printing code from scratch. Create a .rdlc file (drag a
ReportViewer control onto a form and click on create report -
automatically generates a .rdlc file where you place you
textboxes/tables...)
You have some options now for printing - you can print from the
ReportViewer control - but have to set print settings each time - or you
can print from code where you just click a button. The settings are set
in code. Here is a sample of the code (I think the VS help files also
have a print sample under ReportViewer)
---------------------------------------
Imports System.Data.SqlClient
Imports System.Windows.Forms
Imports Microsoft.Reporting.WinForms
Imports System
Imports System.IO
Imports System.Data
Imports System.Text
Imports System.Drawing.Imaging
Imports System.Drawing.Printing
Imports System.Collections.Generic
Public Class clsPrintComment
Implements IDisposable
Private m_currentPageIndex As Integer
Private m_streams As IList(Of Stream)
Dim iPrintStream As Integer
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)
If iPrintStream Mod 2 = 0 Then '--trying to eliminate printing empty
pages here
m_streams.Add(stream)
End If
iPrintStream += 1
Return stream
End Function
Private Sub Export(ByVal report As LocalReport)
'--this is regular layout for printing on 8x11 paper
Dim deviceInfo As String = _
"<DeviceInfo>" + _
" <OutputFormat>EMF</OutputFormat>" + _
" <PageWidth>8.5in</PageWidth>" + _
" <PageHeight>11in</PageHeight>" + _
" <MarginTop>0.75in</MarginTop>" + _
" <MarginLeft>0.75in</MarginLeft>" + _
" <MarginRight>0.75in</MarginRight>" + _
" <MarginBottom>0.75in</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 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)
'--ev.HasMorePages = False '--set this to false if only want to print
one page
End Sub
Private Sub Print()
If m_streams Is Nothing Or m_streams.Count = 0 Then
Return
End If
Dim printDoc As New PrintDocument()
Dim strDefaultPrinter As String = printDoc.PrinterSettings.PrinterName
'--this gets the default printer for the current workstation
If Not printDoc.PrinterSettings.IsValid Then
Dim msg As String = String.Format("Can't find printer ""{0}"".",
strDefaultPrinter)
Return
End If
AddHandler printDoc.PrintPage, AddressOf PrintPage
printDoc.Print()
End Sub
Public Sub Run()
iPrintStream = 0 '--iPrintStream is for skipping print empty pages
Dim report As LocalReport = New LocalReport()
report.ReportPath = Application.StartupPath & "\Report1.rdlc"
Dim s1 As String = frmCom.txtComment.Text
Dim parm1 As ReportParameter
parm1 = New ReportParameter("prmComment", s1)
report.SetParameters(New ReportParameter() {parm1})
Export(report)
m_currentPageIndex = 0
Print()
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
----------------------------------
Rich
*** Sent via Developersdex
http://www.developersdex.com ***