PrintPreview很好,但是打印为空

时间:2019-05-05 16:03:16

标签: vb.net

我正在为程序添加打印功能。在打印预览中,它会显示所有要打印的数据,但会在纸张上空打印。

下面提供了图片

1st Preview

PrintPreview

这是PrintDocument1中的代码:

Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    Try
        If numpages = 1 Then
            'e.Graphics.DrawRectangle(Pens.Red, e.MarginBounds)
            e.Graphics.DrawString("Date: " & Date.Now.ToLongDateString, fon, Brushes.Black, 590, 260)
            e.Graphics.DrawString("Time: " & Date.Now.ToLongTimeString, fon, Brushes.Black, 590, 285)

            e.Graphics.DrawString("Republic of the Philippines", Headerfon, Brushes.Black, 295, 40)
            e.Graphics.DrawString("Cebu Technologcial University", Headerfon, Brushes.Black, 270, 70)
            e.Graphics.DrawString("Daanbantayan Campuse", Headerfon, Brushes.Black, 305, 100)
            e.Graphics.DrawString("( SUC Level IV, AACCUP Accredited and ISO 9001 Certified )", fon2, Brushes.Black, 220, 130)
            e.Graphics.DrawString("Agujo, Daanbantayan, Cebu", fon, Brushes.Black, 332, 150)
            e.Graphics.DrawString("http://daanbantayan.ctu.edu.ph", fon2, Brushes.SteelBlue, 80, 190)
            e.Graphics.DrawString("Email: ctudaanbantayan@yahoo.com", fon2, Brushes.SteelBlue, 560, 190)
            e.Graphics.DrawString("Fax No. (032) 437-8523", fon2, Brushes.SteelBlue, 560, 210)
            e.Graphics.DrawString("Telephone No. (032) 437-8526", fon2, Brushes.SteelBlue, 80, 210)
            e.Graphics.DrawLine(Pens.Black, 800, 240, 55, 240)
            e.Graphics.DrawLine(Pens.Black, 800, 244, 55, 244)
            e.Graphics.DrawImage(PictureBox1.Image, 60.0F, 52.0F, 120.0F, 120.0F)
            e.Graphics.DrawImage(PictureBox2.Image, 610.0F, 50.0F, 120.0F, 120.0F)
        End If
line1:
        e.Graphics.DrawImage(PictureBox3.Image, 50.0F, 1122.0F, 500.0F, 100.0F)
        While PrintI <= Form12.timelog.Rows.Count - 2
            Dim ColReaderSpace As Integer = 1
            For Each GridCol As DataGridViewColumn In Form12.timelog.Columns
                If ColHeaderY >= 1200 Then
                    If PrintI >= 1 Then
                        ColCellY = 50
                        ColHeaderY = 50
                        ColCellSpace = 52
                        numpages += 1
                        e.HasMorePages = True
                        e.Graphics.DrawImage(PictureBox3.Image, 50.0F, 1122.0F, 500.0F, 100.0F)
                        'GoTo line1
                        Exit Sub
                    End If
                End If
line2:
                Dim Rect As New Rectangle(x, ColHeaderY, xWidth, yHeight)
                Dim strings As New StringFormat

                strings.Alignment = StringAlignment.Center
                strings.LineAlignment = StringAlignment.Center
                e.Graphics.FillRectangle(Brushes.WhiteSmoke, Rect)
                e.Graphics.DrawRectangle(Pens.Black, Rect)
                e.Graphics.DrawString(GridCol.HeaderText, fon, Brushes.Black, Rect)
                If ColReaderSpace = 8 Then
                    ColHeaderY += 20
                End If
                ColHeaderY += 20
                ColReaderSpace += 2
            Next GridCol
            For k As Integer = 0 To 7
                Dim Rec As New Rectangle(y, ColCellY, CellWidth, yHeight)
                e.Graphics.DrawRectangle(Pens.Black, Rec)
                e.Graphics.DrawString(Form12.timelog.Rows(PrintI).Cells(k).Value.ToString, fon, Brushes.Black, CellHeight, ColCellSpace)
                If ColReaderSpace = 10 Then
                    ColCellY += 20
                    ColCellSpace += 20
                End If
                ColCellY += 20
                ColCellSpace += 20
                ColReaderSpace += 1
            Next

            e.HasMorePages = False
            PrintI += 1
        End While
    Catch ex As Exception
        MessageBox.Show(ex.Message & vbCrLf & vbCrLf & "Please Make Sure There is Data to print", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try
    e.Graphics.DrawImage(PictureBox3.Image, 50.0F, 1122.0F, 500.0F, 100.0F)
End Sub

这是打印中的代码:

Private Sub print_Click(sender As Object, e As EventArgs) Handles print.Click
    ColHeaderY = 320
    ColCellY = 320
    ColCellSpace = 320
    PrintI = 0
    numpages = 1
    PrintDialog1.PrinterSettings = PrintDocument1.PrinterSettings
    If PrintDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
        PrintDocument1.PrinterSettings = PrintDialog1.PrinterSettings
    End If
    PrintPreviewDialog1.Document = PrintDocument1
    PrintPreviewDialog1.TopMost = True
    PrintPreviewDialog1.ShowDialog()
End Sub

我希望有人可以帮助我解决这个错误。

0 个答案:

没有答案