在下面的代码中无法添加下一页。这有什么问题 当行数达到10行时,我已将e.hasmorepage设置为true。但是无法开始打印下一页
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim l, t, right, b As Integer
Dim companyName As String = "G.M.Agencies"
l = e.MarginBounds.Left
t = e.MarginBounds.Top
right = e.MarginBounds.Right
b = e.MarginBounds.Bottom
Dim bor As New Pen(Color.Black, 2)
Dim brushee As New SolidBrush(Color.Black)
Dim nameFont As New Font("Arial", 24, FontStyle.Bold)
Dim nameFont2 As New Font("Arial", 12, FontStyle.Bold)
Dim StringAlignment As New StringFormat()
StringAlignment.Alignment = Drawing.StringAlignment.Far
Dim formatednumber As String
Dim r As New Rectangle(l - 50, t - 50, right - 10, b - 10)
e.Graphics.DrawRectangle(bor, r)
'Draw line below the seller name
e.Graphics.DrawLine(bor, l - 50, t + 100, right + 40, t + 100)
'draw vertical line for seller and buyer
e.Graphics.DrawLine(bor, right - 350, t - 50, (right - 350), t + 100)
'draw the line below item description and below the header text
e.Graphics.DrawLine(bor, l - 50, t + 150, right + 40, t + 150)
e.Graphics.DrawLine(bor, l - 50, b - 150, right + 40, b - 150)
e.Graphics.DrawLine(bor, l - 50, b - 250, right + 40, b - 250)
'Draw the vertical line in item details table
e.Graphics.DrawLine(bor, l + 10, t + 100, l + 10, b - 250)
e.Graphics.DrawLine(bor, right - 370, t + 100, right - 370, b - 150)
e.Graphics.DrawLine(bor, right - 273, t + 100, right - 273, b - 250)
e.Graphics.DrawLine(bor, right - 205, t + 100, right - 205, b - 250)
e.Graphics.DrawLine(bor, right - 125, t + 100, right - 125, b - 250)
e.Graphics.DrawLine(bor, right - 70, t + 100, right - 70, b - 150)
'Printing Company and invoice details
e.Graphics.DrawString(companyName, nameFont, Brushes.Black, l - 40, t - 40)
e.Graphics.DrawString("Dharapuram", nameFont2, Brushes.Black, l - 40, t)
e.Graphics.DrawString("Invoice No", nameFont2, Brushes.Black, l - 40, t + 20)
e.Graphics.DrawString("Date", nameFont2, Brushes.Black, l - 40, t + 40)
'Printing header text
e.Graphics.DrawString("S.No", nameFont2, Brushes.Black, l - 40, t + 120)
e.Graphics.DrawString("Description", nameFont2, Brushes.Black, l + 40, t + 120)
e.Graphics.DrawString("HSN Code", nameFont2, Brushes.Black, right - 360, t + 120)
e.Graphics.DrawString("GST %", nameFont2, Brushes.Black, right - 270, t + 120)
e.Graphics.DrawString("Rate", nameFont2, Brushes.Black, right - 200, t + 120)
e.Graphics.DrawString("Qty", nameFont2, Brushes.Black, right - 120, t + 120)
e.Graphics.DrawString("Total", nameFont2, Brushes.Black, right - 60, t + 120)
'grand total
e.Graphics.DrawString("Grand Total", nameFont2, Brushes.Black, right - 170, b - 180, StringAlignment)
e.Graphics.DrawString("Total CGST", nameFont2, Brushes.Black, right - 170, b - 200, StringAlignment)
e.Graphics.DrawString("Total SGST", nameFont2, Brushes.Black, right - 170, b - 220, StringAlignment)
e.Graphics.DrawString("Total IGST", nameFont2, Brushes.Black, right - 170, b - 240, StringAlignment)
'printing item details
Dim dataGridViewRows As DataGridViewRowCollection = dg_sales_entry.Rows
Dim dataGridViewColumns As DataGridViewColumnCollection = dg_sales_entry.Columns
Dim numRows As Integer = dataGridViewRows.Count
Dim numCols As Integer = dataGridViewColumns.Count
Dim startX As Integer = l - 40
Dim startY As Integer = t + 170
Dim cellPadding As Integer = 10
Dim currentY As Integer = startY
' For Each column As DataGridViewColumn In dataGridViewColumns
' e.Graphics.DrawString(column.HeaderText, dg_sales_entry.Font, Brushes.Black, startX, currentY)
'startX += column.Width + cellPadding
' Next
'currentY += dg_sales_entry.Rows(0).Height
Dim grand_total As Double
Dim cgst As Double
Dim sgst As Double
Dim igst As Double
Dim page_number As Integer = 0
Dim i As Integer = 0
' Dim ii As Integer = 0
For i = ii To numRows - 1
'calculating grand total,cgst,igst
grand_total = grand_total + dg_sales_entry.Rows(i).Cells(10).Value
cgst = cgst + dg_sales_entry.Rows(i).Cells(15).Value
sgst = sgst + dg_sales_entry.Rows(i).Cells(17).Value
igst = igst + dg_sales_entry.Rows(i).Cells(13).Value
Dim formated_number As String = (Double.Parse(dg_sales_entry.Rows(i).Cells(10).Value)).ToString("F2")
Dim formatednuber2 As String = formated_number.PadLeft(25 - formated_number.ToString.Length)
Dim rate As String = (Double.Parse(dg_sales_entry.Rows(i).Cells(7).Value)).ToString("F2")
Dim rate2 As String = rate.PadLeft(10 - rate.Length)
e.Graphics.DrawString(i + 1, dg_sales_entry.Font, Brushes.Black, l - 40, currentY)
e.Graphics.DrawString(dataGridViewRows(i).Cells(4).Value.ToString(), dg_sales_entry.Font, Brushes.Black, l + 40, currentY)
e.Graphics.DrawString(dataGridViewRows(i).Cells(5).Value.ToString(), dg_sales_entry.Font, Brushes.Black, right - 360, currentY)
e.Graphics.DrawString(dataGridViewRows(i).Cells(12).Value.ToString(), dg_sales_entry.Font, Brushes.Black, right - 270, currentY)
e.Graphics.DrawString(rate2, dg_sales_entry.Font, Brushes.Black, right - 200, currentY)
e.Graphics.DrawString(dataGridViewRows(i).Cells(6).Value.ToString(), dg_sales_entry.Font, Brushes.Black, right - 120, currentY)
e.Graphics.DrawString(formatednuber2, dg_sales_entry.Font, Brushes.Black, right - 60, currentY)
'currentX += dataGridViewColumns(j).Width + cellPadding
currentY += dg_sales_entry.Rows(i).Height
page_number += 1
ii += 1
If page_number > 10 And page_number < 12 Then
e.HasMorePages = True
Exit For
End If
Next
'Printing total and taxes
e.Graphics.DrawString(grand_total, dg_sales_entry.Font, Brushes.Red, right - 60, b - 180)
e.Graphics.DrawString(sgst, dg_sales_entry.Font, Brushes.Black, right - 60, b - 220)
e.Graphics.DrawString(cgst, dg_sales_entry.Font, Brushes.Black, right - 60, b - 200)
e.Graphics.DrawString(igst, dg_sales_entry.Font, Brushes.Black, right - 60, b - 240)
e.HasMorePages = False
End Sub
这是一个完美的例子,说明了为什么在发布问题之前总是需要调试代码。如果你这样做了,问题就很明显了。你说:
当行数达到 10 时,我已启用 e.hasmorepage 为 true
确实如此,但是,如果您实际调试了代码,您会注意到在该事件处理程序中执行的最后一行代码是这样的:
e.HasMorePages = False
如果在方法完成之前再次将其设置回
True
,则将其设置为 False
也没有关系。
更多内容敬请关注...