Я пытаюсь распечатать многострочный текстовый блок и несколько разных datagridviews после этого. Я надеюсь, что некоторые из добрых душ здесь смогут мне помочь.VB.net Печать текста из многострочного текстового поля и нескольких datagridviews после этого
До сих пор он хорошо работает, когда есть только одна страница из datagridview, после чего последняя страница текста повторяется.
Как так:
All Fine for Page 1 Problems afoot for every other page after that
Для всех, кто собирается читать мой код-беспорядок, у вас есть мое предельное спасибо.
Вот код, который я использую:
Dim mRow As Integer = 0
Dim newpage As Boolean = True
Dim page As Integer = 0
Dim tablepage As Integer = 0
Dim content1 As String
Dim numChars As Integer
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
PrintPreviewDialog1.Document = PrintDocument1
PrintPreviewDialog1.ShowDialog()
mRow = 0
newpage = True
page = 0
tablepage = 0
End Sub
Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim numLines As Integer
'1 Page
'TEXT
If page = 0 Then
content1 = TextBox6.Text & vbLf & TextBox7.Text & vbLf & TextBox8.Text
Dim stringForPage As String
Dim strFormat As New StringFormat()
Dim PrintFont As Font
PrintFont = TextBox6.Font
Dim rectDraw As New RectangleF(e.MarginBounds.Left, e.MarginBounds.Top, e.MarginBounds.Width, e.MarginBounds.Height)
Dim sizeMeasure As New SizeF(e.MarginBounds.Width, e.MarginBounds.Height - PrintFont.GetHeight(e.Graphics))
strFormat.Trimming = StringTrimming.Word
e.Graphics.MeasureString(content1, PrintFont, sizeMeasure, strFormat, numChars, numLines)
stringForPage = content1.Substring(0, numChars)
Dim rectDraw1 As New RectangleF(e.MarginBounds.Left, e.MarginBounds.Top, e.MarginBounds.Width, PrintFont.GetHeight(e.Graphics) * numLines)
e.Graphics.DrawString(stringForPage, PrintFont, Brushes.Black, rectDraw1, strFormat)
If numChars < content1.Length Then
content1 = content1.Substring(numChars)
e.HasMorePages = True
page += 1
Else
e.HasMorePages = False
'End If
'DATAGRIDVIEW
If tablepage > 0 Then
With TableDataGridView
Dim fmt As StringFormat = New StringFormat(StringFormatFlags.LineLimit)
fmt.LineAlignment = StringAlignment.Center
fmt.Trimming = StringTrimming.EllipsisCharacter
Dim y As Single = e.MarginBounds.Top '
Do While mRow < .RowCount
Dim row As DataGridViewRow = .Rows(mRow)
Dim x As Single = e.MarginBounds.Left
Dim h As Single = 0
For Each cell As DataGridViewCell In row.Cells
Dim rc As RectangleF = New RectangleF(x, y, cell.Size.Width, cell.Size.Height)
e.Graphics.DrawRectangle(Pens.Black, rc.Left, rc.Top, rc.Width, rc.Height)
If (newpage) Then
e.Graphics.DrawString(TableDataGridView.Columns(cell.ColumnIndex).HeaderText, .Font, Brushes.Black, rc, fmt)
Else
e.Graphics.DrawString(TableDataGridView.Rows(cell.RowIndex - 1).Cells(cell.ColumnIndex).FormattedValue.ToString(), .Font, Brushes.Black, rc, fmt)
End If
x += rc.Width
h = Math.Max(h, rc.Height)
Next
newpage = False
y += h
mRow += 1
If y + h > e.MarginBounds.Bottom Then ' 800 Then '
e.HasMorePages = True
newpage = True
mRow -= 1
Exit Sub
End If
Loop
End With
Else
'DATAGRIDVIEW < 2 page
With TableDataGridView
Dim fmt As StringFormat = New StringFormat(StringFormatFlags.LineLimit)
fmt.LineAlignment = StringAlignment.Center
fmt.Trimming = StringTrimming.EllipsisCharacter
Dim y As Single = 20 + rectDraw1.Bottom 'e.MarginBounds.Top '
Do While mRow < .RowCount
Dim row As DataGridViewRow = .Rows(mRow)
Dim x As Single = e.MarginBounds.Left
Dim h As Single = 0
For Each cell As DataGridViewCell In row.Cells
Dim rc As RectangleF = New RectangleF(x, y, cell.Size.Width, cell.Size.Height)
e.Graphics.DrawRectangle(Pens.Black, rc.Left, rc.Top, rc.Width, rc.Height)
If (newpage) Then
e.Graphics.DrawString(TableDataGridView.Columns(cell.ColumnIndex).HeaderText, .Font, Brushes.Black, rc, fmt)
Else
e.Graphics.DrawString(TableDataGridView.Rows(cell.RowIndex - 1).Cells(cell.ColumnIndex).FormattedValue.ToString(), .Font, Brushes.Black, rc, fmt)
End If
x += rc.Width
h = Math.Max(h, rc.Height)
Next
newpage = False
y += h
mRow += 1
If y + h > e.MarginBounds.Bottom Then '800 Then '
e.HasMorePages = True
newpage = True
mRow -= 1
tablepage += 1
Exit Sub
End If
Loop
End With
End If
End If
'TEXT
'More than 1 Page
ElseIf page > 0 Then
Dim stringForPage As String
Dim strFormat As New StringFormat()
Dim PrintFont As Font
PrintFont = TextBox6.Font
Dim rectDraw As New RectangleF(e.MarginBounds.Left, e.MarginBounds.Top, e.MarginBounds.Width, e.MarginBounds.Height)
Dim sizeMeasure As New SizeF(e.MarginBounds.Width, e.MarginBounds.Height - PrintFont.GetHeight(e.Graphics))
strFormat.Trimming = StringTrimming.Word
e.Graphics.MeasureString(content1, PrintFont, sizeMeasure, strFormat, numChars, numLines)
stringForPage = content1.Substring(0, numChars)
Dim rectDraw1 As New RectangleF(e.MarginBounds.Left, e.MarginBounds.Top, e.MarginBounds.Width, PrintFont.GetHeight(e.Graphics) * numLines)
e.Graphics.DrawString(stringForPage, PrintFont, Brushes.Black, rectDraw1, strFormat)
If numChars < content1.Length Then
content1 = content1.Substring(numChars)
e.HasMorePages = True
page += 1
Else
e.HasMorePages = False
'DATAGRIDVIEW
If tablepage > 0 Then
With TableDataGridView
Dim fmt As StringFormat = New StringFormat(StringFormatFlags.LineLimit)
fmt.LineAlignment = StringAlignment.Center
fmt.Trimming = StringTrimming.EllipsisCharacter
Dim y As Single = e.MarginBounds.Top '
Do While mRow < .RowCount
Dim row As DataGridViewRow = .Rows(mRow)
Dim x As Single = e.MarginBounds.Left
Dim h As Single = 0
For Each cell As DataGridViewCell In row.Cells
Dim rc As RectangleF = New RectangleF(x, y, cell.Size.Width, cell.Size.Height)
e.Graphics.DrawRectangle(Pens.Black, rc.Left, rc.Top, rc.Width, rc.Height)
If (newpage) Then
e.Graphics.DrawString(TableDataGridView.Columns(cell.ColumnIndex).HeaderText, .Font, Brushes.Black, rc, fmt)
Else
e.Graphics.DrawString(TableDataGridView.Rows(cell.RowIndex - 1).Cells(cell.ColumnIndex).FormattedValue.ToString(), .Font, Brushes.Black, rc, fmt)
End If
x += rc.Width
h = Math.Max(h, rc.Height)
Next
newpage = False
y += h
mRow += 1
If y + h > e.MarginBounds.Bottom Then ' 800 Then '
e.HasMorePages = True
newpage = True
mRow -= 1
Exit Sub
End If
Loop
End With
Else
'DATAGRIDVIEW < 2 page
With TableDataGridView
Dim fmt As StringFormat = New StringFormat(StringFormatFlags.LineLimit)
fmt.LineAlignment = StringAlignment.Center
fmt.Trimming = StringTrimming.EllipsisCharacter
Dim y As Single = 20 + rectDraw1.Bottom 'e.MarginBounds.Top '
Do While mRow < .RowCount
Dim row As DataGridViewRow = .Rows(mRow)
Dim x As Single = e.MarginBounds.Left
Dim h As Single = 0
For Each cell As DataGridViewCell In row.Cells
Dim rc As RectangleF = New RectangleF(x, y, cell.Size.Width, cell.Size.Height)
e.Graphics.DrawRectangle(Pens.Black, rc.Left, rc.Top, rc.Width, rc.Height)
If (newpage) Then
e.Graphics.DrawString(TableDataGridView.Columns(cell.ColumnIndex).HeaderText, .Font, Brushes.Black, rc, fmt)
Else
e.Graphics.DrawString(TableDataGridView.Rows(cell.RowIndex - 1).Cells(cell.ColumnIndex).FormattedValue.ToString(), .Font, Brushes.Black, rc, fmt)
End If
x += rc.Width
h = Math.Max(h, rc.Height)
Next
newpage = False
y += h
mRow += 1
If y + h > e.MarginBounds.Bottom Then '800 Then '
e.HasMorePages = True
newpage = True
mRow -= 1
tablepage += 1
Exit Sub
End If
Loop
End With
End If
End If
End If
End Sub
Try сузить проблему немного себя, нет один будет проходить через все это и выполнять работу за вас. –
Спасибо, я буду продолжать бить. – Josh