在开发应用程序时,经常需要为用户提供打印文档的功能。本文将介绍如何在VB.NET中实现打印对话框和打印预览对话框,包括详细的代码示例和实现步骤。
打印对话框允许用户选择打印机、页面范围和其他打印选项。以下是实现打印对话框的步骤和代码示例。
首先,需要定义一些变量来存储打印字体和要打印的文本。然后,在打印按钮的点击事件中,创建一个打印对话框,并设置其属性。如果用户点击了“确定”按钮,将文本转换为流,以便在打印时逐行读取。
Private printFont As Font = New Font("Tahoma", 12, FontStyle.Regular)
Private aBunchOfLongLines As String = "This software is provided 'as-is', with no warranties of any kind stating operability of a specific or error free operation. Use of the application is entirely the user's responsibility. The software vendor assumes no responsibility for use of this application. This includes damage which may occur during operation of the application to the users files or computer system. By use of this application, you agree that you will take full responsibility for any damage that may occur. If you do not wish to use this application any further, please uninstall it from your computer."
接下来,创建一个打印文档,并为其添加打印页面事件处理程序。在打印页面事件中,使用StringFormat对象来实现文本的自动换行,并计算每行的高度,以便在页面上绘制文本。
Private Sub tsbPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbPrint.Click
Dim pd As New Printing.PrintDocument()
Me.PrintDialog1.AllowPrintToFile = False
Me.PrintDialog1.AllowSelection = False
Me.PrintDialog1.AllowSomePages = False
Me.PrintDialog1.UseEXDialog = True
Me.PrintDialog1.Document = pd
If Me.PrintDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
Try
Dim aBytes() As Byte = System.Text.Encoding.ASCII.GetBytes(aBunchOfLongLines)
Dim strmMem As New MemoryStream(aBytes)
Dim streamToPrint As New IO.StreamReader(strmMem)
pd.PrinterSettings = Me.PrintDialog1.PrinterSettings
AddHandler pd.PrintPage, AddressOf pd_PrintPage
pd.Print()
Catch ex As Exception
Finally
streamToPrint.Close()
End Try
End If
End Sub
打印预览对话框允许用户在打印之前查看文档的打印效果。以下是实现打印预览对话框的步骤和代码示例。
在打印预览按钮的点击事件中,首先将文本转换为流。然后,创建一个打印文档,并为其添加打印页面事件处理程序。接下来,设置打印预览对话框的属性,并显示打印预览。
Private Sub tsbPreview_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbPreview.Click
Dim aBytes() As Byte = System.Text.Encoding.ASCII.GetBytes(aBunchOfLongLines)
Dim strmMem As New MemoryStream(aBytes)
Dim streamToPrint As New IO.StreamReader(strmMem)
Dim pd As New Printing.PrintDocument()
AddHandler pd.PrintPage, AddressOf pd_PrintPage
Me.PrintPreviewDialog1.AllowTransparency = False
Dim dlg As Form = DirectCast(Me.PrintPreviewDialog1, Form)
dlg.Width = 600
dlg.Height = 400
dlg.WindowState = FormWindowState.Maximized
Me.PrintPreviewDialog1.Document = pd
Me.PrintPreviewDialog1.ShowDialog()
streamToPrint.Close()
End Sub
Private Sub pd_PrintPage(ByVal sender As Object, ByVal ev As Printing.PrintPageEventArgs)
Dim yPos As Single = 0
Dim leftMargin As Single = ev.MarginBounds.Left
Dim topMargin As Single = ev.MarginBounds.Top
Dim line As String = Nothing
Dim actual As SizeF = Nothing
yPos = topMargin
While yPos < ev.MarginBounds.Top + ev.MarginBounds.Size.Height
line = streamToPrint.ReadLine()
Dim sf As StringFormat = StringFormat.GenericTypographic
sf.Alignment = StringAlignment.Near
sf.LineAlignment = StringAlignment.Near
sf.FormatFlags = StringFormatFlags.LineLimit
sf.Trimming = StringTrimming.Word
If line Is Nothing Then Exit While
If line.Equals("") Then
line = ""
End If
actual = ev.Graphics.MeasureString(line, printFont, New SizeF(ev.MarginBounds.Size.Width, ev.MarginBounds.Size.Height), sf)
ev.Graphics.DrawString(line, printFont, Brushes.Black, New RectangleF(leftMargin, yPos, ev.MarginBounds.Size.Width, ev.MarginBounds.Size.Height), sf)
yPos = yPos + actual.Height
End While
If line IsNot Nothing Then ev.HasMorePages = True Else ev.HasMorePages = False End If
End Sub