我正在使用Vb中的“打印预览”设置打印功能。那会打印Datagridview中的所有数据,但是问题是在预览模式下输入的数据很长时,它不会自动添加新页面。
这是示例图片: PrintPriview
在图像中您可以看到,前提是数据超出了页边空白区域
这是代码。
Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Try
Static intStart As Integer
Dim texthyt As Integer
Dim x As Integer = 170
Dim y As Integer = 360
Dim xWidth As Integer = 190
Dim yHeight As Integer = 20
Dim CellWidth As Integer = 300
Dim CellHeight As Integer = 370
Dim intcounter As Integer
Dim fon As New Font(FontFamily.GenericSerif, 12, FontStyle.Regular)
Dim fon2 As New Font(FontFamily.GenericSerif, 10, FontStyle.Regular)
Dim fon3 As New Font(FontFamily.GenericSerif, 14, FontStyle.Bold)
Dim Headerfon As New Font(FontFamily.GenericSansSerif, 16, FontStyle.Regular)
Dim ColHeaderY As Integer = 320
Dim ColCellY As Integer = 320
Dim ColCellSpace As Integer = 320
e.Graphics.DrawRectangle(Pens.Red, e.MarginBounds)
Dim LinePerPage As Integer = CInt(Math.Round(texthyt / (fon.Height + 0.025)))
'e.Graphics.DrawString("Report of: " & ReportForm.BunifuDropdown1.selectedValue.ToString & " " & ReportForm.BunifuDropdown2.selectedValue.ToString, fon3, Brushes.Black, 300, 350)
' e.Graphics.DrawString("ID Number: " & AdminPanel.TextBox1.Text, fon, Brushes.Black, 60, 260)
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("Printed by: " & AdminPanel.TextBox3.Text & " " & AdminPanel.TextBox4.Text, fon, Brushes.Black, 60, 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, 50.0F, 160.0F, 100.0F)
' e.Graphics.DrawImage(PictureBox2.Image, 600.0F, 50.0F, 160.0F, 100.0F)
For i As Integer = 0 To Form12.timelog.Rows.Count - 1
Dim ColReaderSpace As Integer = 1
For Each GridCol As DataGridViewColumn In Form12.timelog.Columns
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(i).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
Next
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
End Sub