system.drawing.printing 命名空间
printerSettings 打印机设置类
PageSettings 页面设置类
PrintPageEventArgs 要打印页的设置信息类
一、打印图片(加入控件printdocument1方法)
- Public Class Form1
- Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
- Try
- AddHandler PrintDocument1.PrintPage, AddressOf Me.PrintGraphics
- PrintDocument1.Print()
- Catch ex As Exception
- MsgBox(ex.Message)
- End Try
- End Sub
- Private Sub PrintGraphics(ByVal sender As Object, ByVal ev As System.Drawing.Printing.PrintPageEventArgs)
- ev.Graphics.DrawImage(System.Drawing.Image.FromFile(TextBox1.Text), ev.Graphics.VisibleClipBounds)
- ev.HasMorePages = False
- End Sub
- End Class
- Public Class Form1
- Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
- Try
- Dim printDoc As New System.Drawing.Printing.PrintDocument
- AddHandler printDoc.PrintPage, AddressOf Me.PrintText
- printDoc.Print()
- Catch ex As Exception
- MsgBox(ex.Message)
- End Try
- End Sub
- Private Sub PrintText(ByVal sender As Object, ByVal ev As System.Drawing.Printing.PrintPageEventArgs)
- ev.Graphics.DrawString(TextBox1.Text, New Font("Arial", 11, FontStyle.Bold), Brushes.Black, 40, 40)
- ev.HasMorePages = False
- End Sub
- End Class
上面两个方法都是简单的方法,但有重大的缺点:
1、字符串不会自动转行,也就是说过长的字符串会“打印”到页面的“外面”去;
2、只能打印一页。
下面来解决上面两个问题。
三、完美打印过长的字符串(占有几页)
描述:open按钮打开一个对话框,选择一个文件,并将文本反映到richtextbox上。同时激活print,再按,就执行打印。
- Imports System.IO
- Imports System.Drawing.Printing
- Public Class Form1
- Private PrintPageSettings As New PageSettings
- Private StringToPrint As String
- Private PrintFont As New Font("Arial", 10)
- Private Sub btnOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpen.Click
- Dim FilePath As String
- OpenFileDialog1.Filter = "Text files (*.txt)|*.txt"
- OpenFileDialog1.ShowDialog()
- If OpenFileDialog1.FileName <> "" Then
- FilePath = OpenFileDialog1.FileName
- Try
- Dim MyFileStream As New FileStream(FilePath, FileMode.Open)
- RichTextBox1.LoadFile(MyFileStream, RichTextBoxStreamType.PlainText)
- MyFileStream.Close()
- StringToPrint = RichTextBox1.Text '初始化打印字符串
- btnPrint.Enabled = True
- Catch ex As Exception
- MessageBox.Show(ex.Message)
- End Try
- End If
- End Sub
- Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
- Try
- '指定当前页设置
- PrintDocument1.DefaultPageSettings = PrintPageSettings
- '指定“打印”对话框的文档并显示
- StringToPrint = RichTextBox1.Text
- PrintDialog1.Document = PrintDocument1
- Dim result As DialogResult = PrintDialog1.ShowDialog()
- If result = DialogResult.OK Then
- PrintDocument1.Print() '打印,并非由“打印”对话框控制.正如OpenFileDialog表现的形式一样。
- End If
- Catch ex As Exception
- MessageBox.Show(ex.Message)
- End Try
- End Sub
- Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
- Dim numChars As Integer
- Dim numLines As Integer
- Dim stringForPage As String
- Dim strFormat As New StringFormat
- '根据页面设置,定义可用的页面区域(打印区域)
- Dim rectDraw As New RectangleF(e.MarginBounds.Left, e.MarginBounds.Top, e.MarginBounds.Width, e.MarginBounds.Height)
- '定义区域,来确定一个页面可容纳多少文本,并使文本高度少一行,以免文本被减短
- Dim sizeMeasure As New SizeF(e.MarginBounds.Width, e.MarginBounds.Height - PrintFont.GetHeight(e.Graphics))
- '处理长字符串时,按单词进行断开(换行)
- strFormat.Trimming = StringTrimming.Word
- '用MeasureString计算出可容纳的字符串个数numChars和行数numLines
- e.Graphics.MeasureString(StringToPrint, PrintFont, sizeMeasure, strFormat, numChars, numLines)
- '计算出适应页面的字符串
- stringForPage = StringToPrint.Substring(0, numChars)
- '(逻辑上)在当前页打印字符串
- e.Graphics.DrawString(stringForPage, PrintFont, Brushes.Black, rectDraw, strFormat)
- '若还有需要打印的文本,则继续处理剩下的页面
- If numChars < StringToPrint.Length Then
- '删除已经打印的字符串
- StringToPrint = StringToPrint.Substring(numChars)
- e.HasMorePages = True
- Else
- e.HasMorePages = False
- StringToPrint = RichTextBox1.Text
- End If
- End Sub
- End Class
1、首先Printdialog1打印对话框,只是设置选择哪个打印机,用哪种方式来打印(通过PrintDialog1.Document = PrintDocument1来进行关联),与具体打印的字符串无关。
2、关键:PrintPage事件发生在打印“当前”页面,也就是说打印3页,这个事件就会发生3次。每次可以通过此事件来设置页面内容(逻辑上)。这样就可以控制打印多页。
3、通过MeasureString来计算每页可容纳的字符串和行数,这样就很好的控制每页的具体字符串,循环这个方法计算剩下的字符进行每页设置并打印。
再增加两个按钮,一个页面设置,一个是页面预览,代码如下:
- Private Sub btnSetup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSetup.Click
- Try '页面设置
- PageSetupDialog1.PageSettings = PrintPageSettings
- PageSetupDialog1.ShowDialog()
- Catch ex As Exception
- MessageBox.Show(ex.Message)
- End Try
- End Sub
- Private Sub btnPreview_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPreview.Click
- Try '页面预览
- PrintDocument1.DefaultPageSettings = PrintPageSettings
- StringToPrint = RichTextBox1.Text
- PrintPreviewDialog1.Document = PrintDocument1
- PrintPreviewDialog1.ShowDialog()
- Catch ex As Exception
- MessageBox.Show(ex.Message)
- End Try
- End Sub