Dim font14 As Font = New Font("AngsanaUPC", 14, FontStyle.Regular, GraphicsUnit.Point)
Try
Static TotalLine As Integer = 1
Dim PageLine As Integer = 1
Dim X As Integer = 60
Dim Y As Integer = 0
Dim MaxY As Integer = e.MarginBounds.Height + 50
Dim PrintString As String
Dim colHead() As String = {"ที่", "กลุ่มสินค้า", "รายการสินค้า", "จำนวน", "หน่วยนับ"}
Dim TopMargin As Single = 30
Dim LeftMargin As Single = 40
Dim x1 As Single = 0.0
Dim xc As Single = 0.0
Dim y1 As Single = 0.0
Dim wdh As Single = 0.0
Dim ch As Single = 0.0
Dim rows_total As Integer
Dim rows_per_page As Integer = 34
Dim wd As Integer = PrnDoc.DefaultPageSettings.PaperSize.Width
Dim font14B As Font = New Font("AngsanaUPC", 14, FontStyle.Bold, GraphicsUnit.Point)
Dim font18B As Font = New Font("AngsanaUPC", 18, FontStyle.Bold, GraphicsUnit.Point)
With e.Graphics
y1 = 100
Dim l1 As Integer = 45
Dim l2 As Integer = 140
Dim l3 As Integer = 420
Dim l4 As Integer = 55
Dim l5 As Integer = 80
Dim rect As Rectangle = New Rectangle(CInt(LeftMargin), CInt(y1), CInt(740), CInt(.MeasureString("ก", font14).Height * rows_per_page))
Dim rhead As Rectangle = New Rectangle(CInt(LeftMargin), CInt(y1), CInt(740), CInt(.MeasureString("ก", font14).Height * 1))
Dim rv1 As Rectangle = New Rectangle(CInt(LeftMargin), CInt(y1), CInt(l1), CInt(.MeasureString("ก", font14).Height * rows_per_page))
Dim rv2 As Rectangle = New Rectangle(CInt(LeftMargin + l1), CInt(y1), CInt(l2), CInt(.MeasureString("ก", font14).Height * rows_per_page))
Dim rv3 As Rectangle = New Rectangle(CInt(LeftMargin + l1 + l2), CInt(y1), CInt(l3), CInt(.MeasureString("ก", font14).Height * rows_per_page))
Dim rv4 As Rectangle = New Rectangle(CInt(LeftMargin + l1 + l2 + l3), CInt(y1), CInt(l4), CInt(.MeasureString("ก", font14).Height * rows_per_page))
Dim rv5 As Rectangle = New Rectangle(CInt(LeftMargin + l1 + l2 + l3 + l4), CInt(y1), CInt(l5), CInt(.MeasureString("ก", font14).Height * rows_per_page))
'Dim rectbottom As Rectangle = New Rectangle(CInt(LeftMargin), CInt(y1 + CInt(.MeasureString("ก", font14).Height * rows_per_page)), CInt(740), CInt(.MeasureString("ก", font14).Height * 1))
'.DrawRectangle(Pens.Black, rect)
.FillRectangle(Brushes.WhiteSmoke, rhead)
.DrawRectangle(Pens.Black, rhead)
.DrawRectangle(Pens.Black, rv1)
.DrawRectangle(Pens.Black, rv2)
.DrawRectangle(Pens.Black, rv3)
.DrawRectangle(Pens.Black, rv4)
.DrawRectangle(Pens.Black, rv5)
''.DrawRectangle(Pens.Black, rectbottom)
Dim xx0 As Single = LeftMargin + 5
Dim xx1 As Single = LeftMargin + l1 + 3
Dim xx2 As Single = LeftMargin + l1 + l2 + 3
Dim xx3 As Single = LeftMargin + l1 + l2 + l3 + 3
Dim xx4 As Single = LeftMargin + l1 + l2 + l3 + l4 + 3
.DrawString(colHead(0), font14B, Brushes.Black, xx0 + 8, y1 + 3)
.DrawString(colHead(1), font14B, Brushes.Black, xx1 + 38, y1 + 3)
.DrawString(colHead(2), font14B, Brushes.Black, xx2 + 170, y1 + 3)
.DrawString(colHead(3), font14B, Brushes.Black, xx3 + 3, y1 + 3)
.DrawString(colHead(4), font14B, Brushes.Black, xx4 + 8, y1 + 3)
xc = LeftMargin + ((wd - LeftMargin * 2) / 2) - (e.Graphics.MeasureString("รายงานสินค้าคงเหลือ", font18B).Width / 2)
e.Graphics.DrawString("รายงานสินค้าคงเหลือ", font18B, Brushes.Black, xc, 50)
rows_total = dgvallproduct.Rows.Count()
'e.Graphics.DrawRectangle(New Pen(Color.Silver), e.MarginBounds)
Static page As Integer = 1
Do
Y = CInt(135 + ((PageLine - 1) * font14.GetHeight(e.Graphics)))
PrintString = CInt(TotalLine).ToString()
e.Graphics.DrawString(PrintString, font14, Brushes.Black, xx0, Y)
PrintString = dgvallproduct.Rows(TotalLine - 1).Cells(2).Value.ToString()
e.Graphics.DrawString(PrintString, font14, Brushes.Black, xx1, Y)
PrintString = dgvallproduct.Rows(TotalLine - 1).Cells(3).Value.ToString()
e.Graphics.DrawString(PrintString, font14, Brushes.Black, xx2, Y)
PrintString = dgvallproduct.Rows(TotalLine - 1).Cells(4).Value.ToString()
e.Graphics.DrawString(PrintString, font14, Brushes.Black, xx3, Y)
PrintString = dgvallproduct.Rows(TotalLine - 1).Cells(5).Value.ToString()
e.Graphics.DrawString(PrintString, font14, Brushes.Black, xx4, Y)
PageLine += 1
TotalLine += 1
Loop While (Y <= MaxY) And (TotalLine <= rows_total)
Dim xno As Single
xno = e.Graphics.MeasureString(page.ToString, font14).Width
e.Graphics.DrawString("หน้า", font14, Brushes.Black, 680, 35)
e.Graphics.DrawString(page.ToString, font14, Brushes.Black, wd - xno - 40, 35)
xno = e.Graphics.MeasureString(Now.ToShortDateString, font14).Width
e.Graphics.DrawString("วันที่", font14, Brushes.Black, 680, 60)
e.Graphics.DrawString(Now.ToShortDateString, font14, Brushes.Black, wd - xno - 40, 60)
page += 1
If (TotalLine <= rows_total) Then
e.HasMorePages = True
e.Graphics.DrawString("มีต่อหน้า " & page, font14, Brushes.Black, 710, MaxY + 60)
Else
e.HasMorePages = False
End If
End With
Catch ex As Exception
End Try