Public Class RichTextBoxPrint
    Private printtye As Integer = 1
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        printtye = 1
        myp.InvalidatePreview()
    End Sub

    Private Sub myp_PrintDocument() Handles myp.PrintDocument
        myp.NewPage()
        Dim myh As Single
        Dim myfont As New Font("黑体", 28, FontStyle.Bold)
        Dim mys As String
        mys = "RTF打印测试，连续打印输出2个RichTextBox控件的内容"
        myh = myp.MeasureText(mys, myfont, myp.PaperPrintWidth).Height
        myp.DrawText(mys, myp.PaperPrintWidth, myh, myfont, Color.Black, StringAlignment.Center, StringAlignment.Center, True, False, False, False, 6, 6)
        myp.NewRow(myp.LastDrawTextHeight * 1.2)
        If printtye = 1 Then
            myp.DrawRichTextBox(r1)
            myp.DrawRichTextBox(R2)
        Else
            myp.DrawRichTextBoxAsBMP(r1)
            myp.DrawRichTextBoxAsBMP(R2)
        End If
        myp.DrawText("打印RichTextBox控件的内容后接下去可以打印的内容")
    End Sub

 

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        If myp.PageUnits = VBprinter.VB2008Print.PageExportUnit.CentiMeter Then
            myp.PageUnits = VBprinter.VB2008Print.PageExportUnit.Inch
        ElseIf myp.PageUnits = VBprinter.VB2008Print.PageExportUnit.Inch Then
            myp.PageUnits = VBprinter.VB2008Print.PageExportUnit.CentiMeter
        End If
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        printtye = 0
        myp.InvalidatePreview()
    End Sub

    Private Sub RichTextBoxPrint_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim mypath As String
        mypath = Application.ExecutablePath
        mypath = My.Computer.FileSystem.GetParentPath(mypath)
        Try
            r1.Clear()
            r1.LoadFile(mypath & "\rtf1.rtf")
            R2.Clear()
            R2.LoadFile(mypath & "\rtf2.rtf")
        Catch ex As Exception

        End Try
   
    End Sub

    Private Sub myp_HeaderFooterOut(ByVal pages As System.Int32, ByVal curpage As System.Int32) Handles myp.HeaderFooterOut
        myp.PrintFooter("中国航空动力机械研究所", "共" & CStr(pages) & "页 第" & CStr(curpage) & "页", "打印时间：" & Now.ToString)
    End Sub
End Class