Public Class printpapersize
    Dim printtype As Integer
   
    Private Sub printpapersize_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Me.客户TableAdapter.Fill(Me.NorthwindDataSet.客户)
        D1.DataSource = Me.NorthwindDataSet.客户

        '表格字段名特殊格式设置打印测试
        D1.Columns(1).HeaderCell.Style.ForeColor = Color.Red
        Dim mystyle As FontStyle
        mystyle = FontStyle.Bold + FontStyle.Strikeout + FontStyle.Underline + FontStyle.Italic
        D1.Columns(1).HeaderCell.Style.BackColor = Color.Blue
        D1.Columns(1).HeaderCell.Style.Font = New Font("黑体", 13, mystyle)
        D1.Columns(3).HeaderCell.Style.BackColor = Color.Red
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        myp.InvalidatePreview()

    End Sub



    Private Sub myp_PrintDocument() Handles myp.PrintDocument
        '第一种方式打印
        Dim mypapersize As New System.Drawing.Printing.PaperSize("自定义纸张大小", myp.ConvertMmToInch(Val(TextBox1.Text)) * 100, myp.ConvertMmToInch(Val(TextBox2.Text)) * 100)
        myp.NewPage(mypapersize, IIf(RadioButton1.Checked = True, False, True))
        myp.PrintDGV(D1, "使用自定义大小的纸张打印DataGridView演示", New Font("黑体", 18), "", Nothing, StringAlignment.Center, "1111", True, False, Nothing, "", "", "", Nothing, "", "", "", 6, True, True, 0, 0, False, Nothing, StringAlignment.Center, StringAlignment.Center, StringAlignment.Center, StringAlignment.Center, StringAlignment.Center, StringAlignment.Center)

    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        '第二种方式打印
        myp.NewDoc()
        Dim mypapersize As New System.Drawing.Printing.PaperSize("自定义纸张大小", myp.ConvertMmToInch(Val(TextBox1.Text)) * 100, myp.ConvertMmToInch(Val(TextBox2.Text)) * 100)
        myp.NewPage(mypapersize, IIf(RadioButton1.Checked = True, False, True))
        myp.PrintDGV(D1, "使用自定义大小的纸张打印DataGridView演示", New Font("黑体", 18), "", Nothing, StringAlignment.Center, "1111", True, False, Nothing, "", "", "", Nothing, "", "", "", 6, True, True, 0, 0, False, Nothing, StringAlignment.Center, StringAlignment.Center, StringAlignment.Center, StringAlignment.Center, StringAlignment.Center, StringAlignment.Center)
        myp.EndDoc()
    End Sub

    Private Sub myp_HeaderFooterOut(ByVal pages As System.Int32, ByVal curpage As System.Int32) Handles myp.HeaderFooterOut
        myp.PrintHeader("联系邮箱：myyouping@139.com", "", "", 0)
        myp.PrintFooter("", "第" & CStr(curpage) & " 共 " & CStr(pages) & " 页", "", 0)
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        DgVprint1.MainTitle = "使用自定义大小的纸张打印DataGridView演示"
        If RadioButton1.Checked = True Then
            DgVprint1.PaperLandscape = False
        Else
            '横向
            DgVprint1.PaperLandscape = True
        End If
        DgVprint1.IsShowMessageIfPrinterNotSupportPaper = False
        DgVprint1.PaperKind = Printing.PaperKind.Custom
        DgVprint1.PaperName = "我的自定义纸张"
        DgVprint1.PaperWidth = TextBox1.Text
        DgVprint1.PaperHeight = TextBox2.Text
        DgVprint1.Print(D1, True)
    End Sub
End Class