'=============================第一个单元格,画斜线和内容======================================================================================================
e.Graphics.DrawLine(Pens.Black, x + 40, y, x + FirstColumnWidth, y + 60)
e.Graphics.DrawLine(Pens.Black, x, y + 20, x + FirstColumnWidth, y + 60)
_RectangleF = New RectangleF(x + 66, y + 6, 130, 60)
e.Graphics.DrawString("衣服颜色", tableFont, Brushes.Black, _RectangleF)
_RectangleF = New RectangleF(x + 3, y + 2, 40, 19)
e.Graphics.DrawString("物料", tableFont, Brushes.Black, _RectangleF)
_RectangleF = New RectangleF(x + 30, y + 15, 40, 60)
e.Graphics.DrawString("颜色", tableFont, Brushes.Black, _RectangleF)
_RectangleF = New RectangleF(x + 2, y + 44, 40, 60)
e.Graphics.DrawString("部位", tableFont, Brushes.Black, _RectangleF)
_RectangleF = New RectangleF(x + 43, y + 44, 80, 60)
e.Graphics.DrawString("物料", tableFont, Brushes.Black, _RectangleF)
'=====================================画边框==================================================
'画第一列
For i = 0 To _FirstColumnHeight.Length - 1
_RectangleF = New RectangleF(y, x, _FirstColumnHeight(i), FirstColumnWidth)
e.Graphics.DrawRectangle(Pens.Black, _RectangleF.Top, _RectangleF.Left, _RectangleF.Height, _RectangleF.Width)
y += _FirstColumnHeight(i)
Next
x = _left + FirstColumnWidth
y = _top + CON_SPACE_CAPTION_TOP + CON_SPACE_HEADER_BODY
'画第二列以上
If _Widths.Length > 0 Then
For i = 0 To _Height.Length - 1
For j = 1 To _Widths.Length - 1
_RectangleF = New RectangleF(y, x, _Height(i), _Widths(j))
e.Graphics.DrawRectangle(Pens.Black, _RectangleF.Top, _RectangleF.Left, _RectangleF.Height, _RectangleF.Width)
x += _Widths(j)
Next
y += _Height(i)
x = _left + FirstColumnWidth
Next
End If
If dtCross.Columns.Count > 2 Then
For j = CurrentColumn - 1 To dtCross.Columns.Count - 2
If j > GColumnCount * GColumnCounts Then
Exit For
End If
_RectangleF = New RectangleF(x, y, _Widths(1), _Height(0))
Me.DrawText(dtCross.Columns(j + 1).ColumnName, tableFont, e.Graphics, _RectangleF, _Align)
x += _Widths(1)
Next
End If
'添加部位、物料、物料颜色等的
x = _left
y = _top + CON_SPACE_CAPTION_TOP + CON_SPACE_HEADER_BODY + _Height(0)
If _isForceToChangePage Then
rowCount = initRowCount
End If
Do While dtCross.Rows.Count > rowCount
rowCount += 1
_RectangleF = New RectangleF(x, y, FirstColumnWidth, _FirstColumnHeight(1))
Me.DrawText(dtCross.Rows(rowCount - 1).Item("Description") & "--" & dtCross.Rows(rowCount - 1).Item("MatType"), tableFont, e.Graphics, _RectangleF, _Align)
x = _left + FirstColumnWidth
For j = CurrentColumn - 1 To dtCross.Columns.Count - 2
If j > GColumnCount * GColumnCounts Then
Exit For
End If
Dim _RectangleF1 As RectangleF = New RectangleF(x, y, _Widths(1), _Height(0))
Me.DrawText(dtCross.Rows(rowCount - 1).Item(j + 1).ToString, tableFont, e.Graphics, _RectangleF1, _Align)
x += _Widths(1)
Next
x = _left
y += _FirstColumnHeight(1)
'强制换页
If rowCount Mod 4 = 0 AndAlso dtCross.Columns.Count - 2 > GColumnCount * GColumnCounts Then
CurrentColumn = j + 1
GColumnCounts += 1
initRowCount = rowCount - 4
_isForceToChangePage = True
e.HasMorePages = True
Exit Sub
ElseIf rowCount Mod 4 = 0 Then
Exit Do
End If
Loop
'强制换页
'当它打印到最后的行时,但还有列需要换页打印
If dtCross.Rows.Count <= rowCount Then
If dtCross.Columns.Count - 2 > GColumnCount * GColumnCounts Then
_islastPage = True
CurrentColumn = j + 1
GColumnCounts += 1
initRowCount = rowCount - (rowCount Mod 4)
_isForceToChangePage = True
e.HasMorePages = True
Exit Sub
End If
End If
'正常的换页
If rowCount Mod 4 = 0 And dtCross.Rows.Count > rowCount And Not _islastPage Then
_isForceToChangePage = False
CurrentColumn = 2
GColumnCounts = 1
e.HasMorePages = True
Else
e.HasMorePages = False
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
下面的代码你参考一下吧:
Dim _islastPage As Boolean = False '是否是最后一页
Dim initRowCount As Integer = 0 '强制换页时的当前行
Dim _isForceToChangePage As Boolean = False '识别该页是否被强制换页
Dim GColumnCounts As Integer = 1 'dtCross的列数是GColumnCount的几倍
Dim CurrentColumn As Integer = 2 '当前列,dtCross默认有两列(description和MatType),所以当前列设置为从第三列开始(以0开始计算)
Const FirstColumnWidth As Integer = 130 '第一单格的宽度
Dim GColumnCount As Integer = 7 '默认是打印7列
#Region "打印事件"
Private Sub ppd_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles pd.PrintPage
Dim i As Integer
Dim j As Integer
Dim _top As Integer
Dim _left As Integer
'===================================画标题=================================================================
_Align.Alignment = StringAlignment.Center
_RectangleF = New RectangleF(0, y, Me.pd.DefaultPageSettings.PaperSize.Height, Me.pd.DefaultPageSettings.PaperSize.Width)
e.Graphics.DrawString("物料使用对照表", titleFont, Brushes.Black, _RectangleF, _Align)
y += CON_SPACE_CAPTION_TOP
'====================================TOP====================================================================
_Align.Alignment = StringAlignment.Near
Dim space As Integer = _width / 3
_RectangleF = New RectangleF(x, y, space, Me.pd.DefaultPageSettings.PaperSize.Height)
Me.DrawText("客户:", tableFont, e.Graphics, _RectangleF, _Align)
_RectangleF = New RectangleF(x + _width / 2, y, space, Me.pd.DefaultPageSettings.PaperSize.Height)
Me.DrawText("款号:", tableFont, e.Graphics, _RectangleF, _Align)
_RectangleF = New RectangleF(x + _width / 5 * 4, y, space, Me.pd.DefaultPageSettings.PaperSize.Height)
Me.DrawText("办单号:", tableFont, e.Graphics, _RectangleF, _Align)
If dtTop.Rows.Count > 0 Then
Dim _textWidth As Integer
With dtTop.Rows(0)
_textWidth = e.Graphics.MeasureString("客户:", tableFont).Width
_RectangleF = New RectangleF(x + _textWidth, y, space, Me.pd.DefaultPageSettings.PaperSize.Height)
Me.DrawText(dtTop.Rows(0).Item("BuyerName").ToString, tableFont, e.Graphics, _RectangleF, _Align)
_textWidth = e.Graphics.MeasureString("办单号:", tableFont).Width
_RectangleF = New RectangleF(x + _width / 5 * 4 + _textWidth, y, space, Me.pd.DefaultPageSettings.PaperSize.Height)
Me.DrawText(dtTop.Rows(0).Item("sampleNO").ToString, tableFont, e.Graphics, _RectangleF, _Align)
End With
End If