Public Class Form10
    Private printtype As Integer
    Private Sub Form10_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        '构建测试数据
        dgv1.RowCount = 40
        For i As Integer = 0 To dgv1.RowCount - 1
            dgv1.Rows(i).Cells(0).Value = CStr(i + 1)
            dgv1.Rows(i).Cells(1).Value = "电缆"
            dgv1.Rows(i).Cells(3).Value = "米"
        Next
        For i As Integer = 0 To dgv1.RowCount - 1
            dgv1.Rows(i).Cells(2).Value = 50 + i
        Next

        dgv1.Rows(0).Cells(4).Value = 10
        dgv1.Rows(0).Cells(5).Value = 50
        dgv1.Rows(0).Cells(6).Value = 500

        dgv1.Columns(6).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        dgv1.Columns(5).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight


        dgv1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells)
        dgv1.AutoResizeRows(DataGridViewAutoSizeRowsMode.AllCells)
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        printtype = 1 '自定义方式打印
        myp.InvalidatePreview()
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        printtype = 2 '直接用VB2008Print组件提供的补白打印功能
        myp.IsDrawTableFooterEveryPage = True
        myp.InvalidatePreview()
    End Sub

    Private Sub myp_PrintDocument() Handles myp.PrintDocument
        Dim d1 As DateTime
        Dim d2 As DateTime
        d1 = Now
        If printtype = 2 Then
            '直接用到最新版的VB2008Print组件的补白打印功能
            myp.NewPage()
            myp.IsAutoAddEmptyRow = True
            myp.PrintDGV(dgv1, maintitle.Text, New Font("黑体", 18, FontStyle.Bold), subtitle.Text, New Font("华文行楷", 13), StringAlignment.Near, "1111", CheckBox1.Checked, False, Nothing, dgvheader.Text, "", "", Nothing, dgvfooter.Text, "", "", 6, True, True, 0, 0, False)
            myp.IsAutoAddEmptyRow = False
        Else
            '自定义方式补白打印
            myp.NewPage()
            Dim txtfont As New Font("宋体", 10) '表尾字体
            Dim isneednewpage As Boolean = True
            Dim mysize As SizeF
            Dim footertxt As String
            footertxt = Trim(dgvfooter.Text)
            mysize = myp.MeasureText(footertxt, txtfont, myp.PaperPrintWidth) '表尾要打印的大小
            Dim footerheight As Single
            footerheight = mysize.Height '表尾的高度
            Dim rowheight As Single = 0
            For i As Integer = 0 To dgv1.RowCount - 1
                If isneednewpage = True Then
                    '换页打印表头
                    PrintHeader()
                    isneednewpage = False
                End If
                myp.DrawDGVRow(dgv1, i, CheckBox1.Checked, True, "1111", 6)
                If i <> dgv1.RowCount - 1 Then
                    '非最后一行
                    rowheight = myp.ConvFromDisplay(dgv1.Rows(i + 1).Height, False) '下一行的高度
                    If myp.IsNewPage(rowheight + footerheight, False, False) = True Then
                        '需要换新页
                        '先打印表尾
                        PrintFooter(footertxt, txtfont, footerheight)
                        myp.NewPage()
                        isneednewpage = True
                    End If
                End If
            Next

            '
            '补白的打印
            Dim lastrowheight As Single '最后一行的高度，补白行均用此高度打印行
            lastrowheight = myp.ConvFromDisplay(dgv1.Rows(dgv1.RowCount - 1).Height, False) '

            Dim tempheight As Single
            tempheight = myp.PaperPrintHeight - myp.Currenty - footerheight '剩余这么多可以打印补白
            Dim num As Integer
            num = Math.Floor(tempheight / lastrowheight) '可以补白这么多行
            For i As Integer = 1 To num
                myp.DrawDGVRowForEmpty(dgv1, dgv1.RowCount - 1, CheckBox1.Checked, False, "1111", 0)
            Next
            '最后打印最后一页的表格尾部
            PrintFooter(footertxt, txtfont, footerheight)


        End If

        d2 = Now()


        '    MsgBox("预处理耗时" & DateDiff(DateInterval.Second, d1, d2) & "秒")
    End Sub
    ''' <summary>
    ''' 打印表头
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub PrintHeader()
        myp.Currentx = 0
        Dim txtfont As Font
        Dim mysize As SizeF
        If Trim(maintitle.Text) <> "" Then
            '主标题
            txtfont = New Font("黑体", 18, FontStyle.Bold)
            mysize = myp.MeasureText(maintitle.Text, txtfont, myp.PaperPrintWidth)
            myp.DrawText(maintitle.Text, myp.PaperPrintWidth, mysize.Height, txtfont, Color.Blue, StringAlignment.Center, StringAlignment.Center, True, False, False, False, 0, 0)
            myp.NewRow(myp.LastDrawTextHeight * 1.6)
        End If
        If Trim(subtitle.Text) <> "" Then
            '小标题
            txtfont = New Font("华文行楷", 13)
            mysize = myp.MeasureText(subtitle.Text, txtfont, myp.PaperPrintWidth)
            myp.DrawText(subtitle.Text, myp.PaperPrintWidth, mysize.Height, txtfont, Color.Blue, StringAlignment.Center, StringAlignment.Center, True, False, False, False, 0, 0)
            myp.NewRow(myp.LastDrawTextHeight * 1.3)
        End If
        If Trim(dgvheader.Text) <> "" Then
            '表头内容
            txtfont = New Font("宋体", 11)
            mysize = myp.MeasureText(dgvheader.Text, txtfont, myp.PaperPrintWidth)
            myp.DrawText(dgvheader.Text, myp.PaperPrintWidth, mysize.Height, txtfont, Color.Blue, StringAlignment.Near, StringAlignment.Center, True, False, False, False, 0, 0)
            myp.NewRow(myp.LastDrawTextHeight * 1.2)
        End If
        '再打印表格的列标题
        myp.DrawDGVHeader(dgv1, CheckBox1.Checked, True, "1111", 6)

    End Sub
    ''' <summary>
    ''' 打印表格尾部
    ''' 
    ''' </summary>
    ''' <param name="txt" >表尾打印内容</param>
    ''' <param name="txtfont" >字体</param>
    ''' <param name="hh" >打印高度</param>
    ''' <remarks></remarks>
    Private Sub PrintFooter(ByVal txt As String, ByVal txtfont As Font, ByVal hh As Single)
        myp.DrawText(dgvfooter.Text, myp.PaperPrintWidth, hh, txtfont, Color.Blue, StringAlignment.Near, StringAlignment.Center, True, False, False, False, 0, 0)

    End Sub


    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        myp.InvalidatePreview()
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        If IsNumeric(tablerows.Text) = False Then
            Exit Sub
        End If
        Try
            Dim num As Integer
            num = CInt(tablerows.Text)
            If num <= 0 Then
                Exit Sub
            End If
            dgv1.RowCount = num
            For i As Integer = 0 To dgv1.RowCount - 1
                dgv1.Rows(i).Cells(0).Value = CStr(i + 1)
            Next
            For i = 0 To dgv1.RowCount - 1
                For j As Integer = 1 To dgv1.ColumnCount - 1
                    dgv1.Rows(i).Cells(j).Value = "第" & CStr(i + 1) & "行第" & CStr(j) & "列"
                Next
            Next
        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(curpage) & "页 共" & CStr(pages) & "页", CStr(Now()))
    End Sub



    Private Sub Button2_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        DgVprint1.MainTitle = maintitle.Text
        DgVprint1.SubTitle = subtitle.Text
        DgVprint1.TableHeaderLeft = dgvheader.Text
        DgVprint1.TableFooterLeft = dgvfooter.Text
        DgVprint1.IsAutoAddEmptyRow = True
        DgVprint1.Print(dgv1, False)
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click

        DgVprint1.IsAddRowID = True '添加行号
        DgVprint1.IsGroupNewRowID = True '新组行号从1开始重新编号
        DgVprint1.IsDGVCellValignmentCenter = True
        'DgVprint1.IsShowPrintStatusDialog = False
        'DgVprint1.IsDrawmargin = True
        DgVprint1.OuterBorder = True     '加外框线,版本:5.2.2.2没有此功能,5.2.3.2有此功能. 
        DgVprint1.Print(dgv1, False)
    End Sub
End Class