Private Sub PrintDoc_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDoc.PrintPage
'定义指向Graphics的指针
Dim g As Graphics
'每页中可打印的文本行数
Dim linesPerPage As Long
'记录打印的当前行
Dim Current As Long
'当前的纵坐标
Dim y As Double
'页的左边距
Dim left As Double
'页的顶边距
Dim top As Double
'要输出的文本
Dim strOutput As String
'打印字体
Dim PrintFont As System.Drawing.Font
'打印时使用的刷子
Dim brush As New System.Drawing.SolidBrush(Color.Black)
'当前活动的子窗体
Dim activeChild As Form = Me.ActiveMdiChild
'子窗体中的RichTextBox控件
Dim theBox As RichTextBox
'获得当前活动的子窗体
activeChild = Me.ActiveMdiChild
'获得子窗体的RichTextBox控件
theBox = CType(activeChild.ActiveControl, RichTextBox)
'设置打印字体
PrintFont = theBox.Font
'获得Graphics对象
g = e.Graphics
'设置页面的左边距
left = e.MarginBounds.Left
'获得页面的上边距
top = e.MarginBounds.Top
'计算每页能打印的行数
'顶端留出两行用于打印标题,底端留出两行用于打印注脚
linesPerPage = e.MarginBounds.Height / PrintFont.GetHeight(g) - 4
'设置打印的起始位置
y = top
'输出标题
g.DrawString(activeChild.Text, PrintFont, brush, left, y)
'设置正文的输出位置
y = top + 2 * PrintFont.GetHeight(g)
'循环输出正文
While Current < linesPerPage
'读取要输出的内容
strOutput = line.ReadLine()
'如果没有要打印的内容,则中止循环
If Not strOutput Is Nothing Then
'计算要输出的纵坐标位置
y = y + PrintFont.GetHeight(g)
'输出正文
g.DrawString(strOutput, PrintFont, brush, left, y)
Else
Exit While
End If
End While
'设置注脚的输出位置
y = e.MarginBounds.Bottom - PrintFont.GetHeight(g)
'输出注脚
g.DrawString("Footer", PrintFont, brush, e.MarginBounds.Width / 2, y)
'如果这一页没有打完正文,将HasMorePages属性设置为True,
'这将再次激发PrintPage事件
If Not strOutput Is Nothing Then
e.HasMorePages = True
Else
e.HasMorePages = False
End If
End Sub
你试试???