1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > VB.NET小报表(收据)打印

VB.NET小报表(收据)打印

时间:2023-06-26 20:23:56

相关推荐

VB.NET小报表(收据)打印

''' <summary> ''' 打印收据 ''' </summary> ''' <remarks></remarks> Private Sub PrintReceipt() Try '调用更改默认打印机 Call changeDefaultPrinter(PrintDocType.Receipt)

Dim PrintDoc As New Printing.PrintDocument

AddHandler PrintDoc.PrintPage, AddressOf DrawReceipt'关键是这句话,当使用Print方法时,会自动调用DrawReceipt方法,这个方法是画小收据的过程,可以自定义. PrintDoc.Print()

Catch ex As Exception MsgBox("Printing error!" & vbCrLf & "Please make sure your printer is valid!", 48) End Try End Sub

'这个函数的参数是固定的.用e变量来画出任何图形或文字,这样就可以打印到打印机去了.这个例子有点复杂,有空我再简化一下.

''' <summary> ''' 画出Receipt,用于打印.收据, ''' 临时使用,最后完善一个打印类 ''' </summary> ''' <param name="sender"></param> ''' <param name="e"></param> ''' <remarks></remarks> Private Sub DrawReceipt(ByVal sender As Object, ByVal e As Printing.PrintPageEventArgs)

Dim MyTitformat As New StringFormat MyTitformat.Alignment = StringAlignment.Center Dim MyTitformat_item As New StringFormat

Dim MyConformat As New StringFormat MyConformat.Alignment = StringAlignment.Near

'e.Graphics.PageUnit = GraphicsUnit.Millimeter e.Graphics.PageUnit = GraphicsUnit.Point 'e.Graphics.PageUnit = GraphicsUnit.Pixel Dim MyTitFont1 As New Font("Arial", 21, FontStyle.Bold) '标题字体 Dim MyTitFont2 As New Font("Arial", 12, FontStyle.Bold) '标题字体 Dim MyConFont As New Font("Arial", 8, FontStyle.Regular) '正文字体 Dim MyConFont1 As New Font("Arial", 9, FontStyle.Bold) '正文字体 Dim MyLinePen As New Pen(Color.Black, 1) Dim RecRow As Single = 0 RecRow = 1 Dim nPageH As Double = 0 Dim nPageW As Double = 0

nPageH = nPh nPageW = nPw nPageW = 180 topW = nPageW

''左半侧三例的X坐标 Dim XL0 As Double = 0 Dim XL1 As Double = 0 Dim XL2 As Double = 0 Dim XL3 As Double = 0 Dim XL4 As Double = 0

''当前的Y坐标 Dim YCurrent As Double = 20 ''中间竖线的X坐标 Dim XLine As Double = 0

''每行文字的高度 Dim dHline As Double = 0

''当前打印的区域,主要用于控制描述打印的自动换行和换行后的高度 Dim CurRecF As RectangleF

''当前打印的字符 Dim sCurTPrintStr As String = ""

''用于保存打印公司信息时的打印宽度 Dim companyInfoW As Double = 230

XLine = nPageW / 2 - 25 Me.itemnumW = 90 dItemDesW = 2 dItemQuantityW = 23 dItemPriceW = 52 allPriceW = 60

XL0 = 0 XL1 = XL0 + Me.itemnumW XL2 = XL1 + dItemDesW XL3 = XL2 + Me.dItemQuantityW XL4 = XL3 + Me.dItemPriceW

'e.Graphics.DrawLine(Pens.Black, CInt(XL0), CInt(YCurrent), CInt(XL0 + nPageW), CInt(YCurrent)) 'YCurrent = YCurrent + 1 'e.Graphics.DrawLine(Pens.Black, CInt(XL0), CInt(YCurrent), CInt(XL4 + dItemPriceW), CInt(YCurrent))

Dim LogoIsExistence As Boolean = True '先判断是否要在收据上打印Logo If objSetup.Logo_Type = 1 Or objSetup.Logo_Type = 3 Then If puter.FileSystem.FileExists(Application.StartupPath.Trim & "/Pictures/LogoOnReceipt.TIF") Then LogoIsExistence = True

Dim printLogo As Image = Image.FromFile(Application.StartupPath.Trim & "/Pictures/LogoOnReceipt.TIF") YCurrent = 10 '画左侧Logo Dim xL As Single = 10.0F Dim yL As Single = Convert.ToSingle(YCurrent) e.Graphics.DrawImage(printLogo, xL, yL) Else LogoIsExistence = False End If End If 'companyInfoW = e.PageSettings.PaperSize.Width

'判断是否要在收据上打印公司信息 If objSetup.Logo_Type = 2 Or objSetup.Logo_Type = 3 Then '打印公司信息

'判断标题是否要打印:如果打印Logo的话公司名就不要打印 If objSetup.Logo_Type = 3 And LogoIsExistence Then '重新计算Y坐标值,空出Logo的高度 dHline = 48 YCurrent = YCurrent + dHline Else If pany_Info_1.Trim <> "" Then sCurTPrintStr = pany_Info_1.Trim 'companyInfoW = e.Graphics.MeasureString(sCurTPrintStr, MyTitFont1).Width '计算打印字符串的宽度 'XL1 = Convert.ToDouble((nPageW - companyInfoW) / 2)

CurRecF = New RectangleF(XL0, YCurrent, companyInfoW, 100) e.Graphics.DrawString(sCurTPrintStr, MyTitFont1, Brushes.Black, CurRecF, MyTitformat) dHline = e.Graphics.MeasureString(sCurTPrintStr, MyTitFont1, companyInfoW, MyTitformat).Height YCurrent = YCurrent + dHline

XL1 = XL0 + itemnumW End If End If

If pany_Info_2.Trim <> "" Then sCurTPrintStr = pany_Info_2.Trim 'companyInfoW = e.Graphics.MeasureString(sCurTPrintStr, MyTitFont2).Width 'XL1 = Convert.ToDouble((nPageW - companyInfoW) / 2)

CurRecF = New RectangleF(XL0, YCurrent, companyInfoW, 100) e.Graphics.DrawString(sCurTPrintStr, MyTitFont2, Brushes.Black, CurRecF, MyTitformat) dHline = e.Graphics.MeasureString(sCurTPrintStr, MyTitFont2, companyInfoW, MyTitformat).Height YCurrent = YCurrent + dHline

XL1 = XL0 + itemnumW End If

If pany_Info_3.Trim <> "" Then sCurTPrintStr = pany_Info_3.Trim 'companyInfoW = e.Graphics.MeasureString(sCurTPrintStr, MyTitFont2).Width 'XL1 = Convert.ToDouble((nPageW - companyInfoW) / 2)

CurRecF = New RectangleF(XL0, YCurrent, companyInfoW, 100) e.Graphics.DrawString(sCurTPrintStr, MyTitFont2, Brushes.Black, CurRecF, MyTitformat) dHline = e.Graphics.MeasureString(sCurTPrintStr, MyTitFont2, companyInfoW, MyTitformat).Height YCurrent = YCurrent + dHline

XL1 = XL0 + itemnumW End If

If pany_Info_4.Trim <> "" Then sCurTPrintStr = pany_Info_4.Trim 'companyInfoW = e.Graphics.MeasureString(sCurTPrintStr, MyTitFont2).Width 'XL1 = Convert.ToDouble((nPageW - companyInfoW) / 2)

CurRecF = New RectangleF(XL0, YCurrent, companyInfoW, 100) e.Graphics.DrawString(sCurTPrintStr, MyTitFont2, Brushes.Black, CurRecF, MyTitformat) dHline = e.Graphics.MeasureString(sCurTPrintStr, MyTitFont2, companyInfoW, MyTitformat).Height YCurrent = YCurrent + dHline

XL1 = XL0 + itemnumW End If End If

'如果只打印Logo则需要计算Y坐标值 If objSetup.Logo_Type = 1 Then dHline = 48 YCurrent = YCurrent + dHline End If

sCurTPrintStr = "INVOICE #:" e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XL0, YCurrent) sCurTPrintStr = thisInvoice_Number.ToString.Trim e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XL0 + topW / 2, YCurrent)

dHline = e.Graphics.MeasureString(sCurTPrintStr, MyConFont, topW / 2, MyTitformat).Height YCurrent = YCurrent + dHline

sCurTPrintStr = "DATE/TIME:" e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XL0, YCurrent) sCurTPrintStr = FormatDateTime(Now, DateFormat.ShortDate) & " " & FormatDateTime(Now, DateFormat.LongTime).ToString e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XL0 + topW / 2, YCurrent)

dHline = e.Graphics.MeasureString(sCurTPrintStr, MyConFont, topW / 2, MyTitformat).Height YCurrent = YCurrent + dHline

sCurTPrintStr = "CASHIER: " & sys_employee.Cashier_ID.Trim e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XL0, YCurrent) sCurTPrintStr = "STATION: " & objStation.Station_ID e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XL0 + topW / 2, YCurrent)

dHline = e.Graphics.MeasureString(sCurTPrintStr, MyConFont, topW / 2, MyTitformat).Height YCurrent = YCurrent + dHline

sCurTPrintStr = "CUSTOMER:" e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XL0, YCurrent) sCurTPrintStr = oCustomer.First_Name.Trim & " " & oCustomer.Last_Name.Trim e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XL0 + topW / 2, YCurrent)

dHline = e.Graphics.MeasureString(sCurTPrintStr, MyConFont, topW / 2, MyTitformat).Height YCurrent = YCurrent + dHline * 2

Dim i As Integer

'''''''''''''''''''''''''''''''''''''''''''''''''''''' '表头 sCurTPrintStr = "ITEM" e.Graphics.DrawString(sCurTPrintStr, MyConFont1, Brushes.Black, XL0, YCurrent)

CurRecF = New RectangleF(XL1, YCurrent, itemnumW, 100)

'e.Graphics.DrawString(" DESCRIPTION", MyConFont1, Brushes.Black, XL1, YCurrent) 'dHline = e.Graphics.MeasureString(sCurTPrintStr, MyConFont, itemnumW, MyConformat).Height

e.Graphics.DrawString("QTY", MyConFont1, Brushes.Black, XL2, YCurrent)

e.Graphics.DrawString("PRICE", MyConFont1, Brushes.Black, XL3, YCurrent)

e.Graphics.DrawString("TOTAL", MyConFont1, Brushes.Black, XL4, YCurrent) dHline = e.Graphics.MeasureString(sCurTPrintStr, MyConFont1, dItemDesW + itemnumW, MyTitformat).Height YCurrent = YCurrent + dHline

'YCurrent = YCurrent + 10 ''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ''表头下面画一条线 e.Graphics.DrawLine(MyLinePen, New PointF(XL0, YCurrent), New PointF(XL4 + Me.allPriceW, YCurrent)) dHline = 2 YCurrent = YCurrent + dHline

''画出每个产品 Dim nLenItems As Integer = 0 nLenItems = oSalesInv.InvoiceItems.Length

For i = 0 To nLenItems - 1 If oSalesInv.InvoiceItems(i).Print_On_Receipt = True Then

''<左侧>一个产品:三个字段,数量:描述:单价 sCurTPrintStr = oSalesInv.InvoiceItems(i).DiffItemName.Trim CurRecF = New RectangleF(XL0, YCurrent, Me.itemnumW, 100) e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, CurRecF, MyTitformat_item) dHline = e.Graphics.MeasureString(sCurTPrintStr, MyConFont, itemnumW, MyConformat).Height CurRecF = New RectangleF(XL1, YCurrent, itemnumW, 100)

''sCurTPrintStr = oSalesInv.InvoiceItems(i).DiffItemName '商品名称 ' ''e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XL1, YCurrent) ''e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, CurRecF, MyTitformat) ' ''e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XR1, YCurrent)

sCurTPrintStr = " " & oSalesInv.InvoiceItems(i).Quantity '数量 e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XL2, YCurrent)

sCurTPrintStr = FormatCurrency(oSalesInv.InvoiceItems(i).PricerPer.money).ToString '单价 e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XL3, YCurrent)

sCurTPrintStr = FormatCurrency(oSalesInv.InvoiceItems(i).Quantity * oSalesInv.InvoiceItems(i).PricerPer.money).ToString e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XL4, YCurrent)

YCurrent = YCurrent + dHline End If

Next

''产品结束下面画一条线 e.Graphics.DrawLine(MyLinePen, New PointF(XL0, YCurrent), New PointF(XL4 + Me.allPriceW, YCurrent)) dHline = 2 YCurrent = YCurrent + dHline

'用于控制尾部的汇总描述宽度和高度。 Dim nEndDesW As Double nEndDesW = XL0 + topW / 2 '如果整单有折扣,则显示折扣前和折扣信息 If oSalesInv.Discount > 0 Then CurRecF = New RectangleF(XL0, YCurrent, nEndDesW, 100) sCurTPrintStr = "UNDISCOUNTED TOTAL:" e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XL0, YCurrent) dHline = e.Graphics.MeasureString(sCurTPrintStr, MyConFont, nEndDesW, MyConformat).Height

sCurTPrintStr = FormatCurrency(oSalesInv.Total_Price.money / (1 - oSalesInv.Discount)).ToString '总应收钱数 e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XL2, YCurrent)

YCurrent = YCurrent + dHline

CurRecF = New RectangleF(XL0, YCurrent, nEndDesW, 100) sCurTPrintStr = "-" & FormatPercent(oSalesInv.Discount) & "Discount:" e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XL0, YCurrent) dHline = e.Graphics.MeasureString(sCurTPrintStr, MyConFont, nEndDesW, MyConformat).Height

sCurTPrintStr = FormatCurrency(oSalesInv.Total_Price.money / (1 - oSalesInv.Discount) * oSalesInv.Discount).ToString '总应收钱数 e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XL2, YCurrent)

YCurrent = YCurrent + dHline

End If ''产品总费用 CurRecF = New RectangleF(XL0, YCurrent, nEndDesW, 100) sCurTPrintStr = "SUBTOTAL:" e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XL0, YCurrent) dHline = e.Graphics.MeasureString(sCurTPrintStr, MyConFont, nEndDesW, MyConformat).Height

sCurTPrintStr = FormatCurrency(oSalesInv.Total_Price.money).ToString '总应收钱数 e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XL4, YCurrent)

YCurrent = YCurrent + dHline

''产品总税 CurRecF = New RectangleF(XL0, YCurrent, nEndDesW, 100) sCurTPrintStr = "TAX:" e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XL0, YCurrent) dHline = e.Graphics.MeasureString(sCurTPrintStr, MyConFont, nEndDesW, MyConformat).Height

sCurTPrintStr = FormatCurrency(oSalesInv.Total_Tax1.money + oSalesInv.Total_Tax2.money + oSalesInv.Total_Tax3.money).ToString '总税 e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XL4, YCurrent)

YCurrent = YCurrent + dHline

''GRAND TOTAL CurRecF = New RectangleF(XL0, YCurrent, nEndDesW, 100) sCurTPrintStr = "GRAND TOTAL:" e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XL0, YCurrent) dHline = e.Graphics.MeasureString(sCurTPrintStr, MyConFont, nEndDesW, MyConformat).Height

sCurTPrintStr = FormatCurrency(oSalesInv.Grand_Total.money).ToString e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XL4, YCurrent)

YCurrent = YCurrent + dHline

'' ''这里要加上多种支付方式。 ''先空出一行 YCurrent = YCurrent + dHline

''判断现金支付 If oSalesInv.CA_Amount.money > 0 Then CurRecF = New RectangleF(XL0, YCurrent, nEndDesW, 100) sCurTPrintStr = "AMOUNT:" e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XL0, YCurrent) dHline = e.Graphics.MeasureString(sCurTPrintStr, MyConFont, nEndDesW, MyConformat).Height

sCurTPrintStr = FormatCurrency(oSalesInv.CA_Amount.money).ToString e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XL4, YCurrent)

YCurrent = YCurrent + dHline End If ''判断信用卡支付 If _Amount.money > 0 Then CurRecF = New RectangleF(XL0, YCurrent, nEndDesW, 100) sCurTPrintStr = "CREDIT CARD:" e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XL0, YCurrent) dHline = e.Graphics.MeasureString(sCurTPrintStr, MyConFont, nEndDesW, MyConformat).Height

sCurTPrintStr = FormatCurrency(_Amount.money).ToString e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XL4, YCurrent)

YCurrent = YCurrent + dHline End If ''判断支票支付 If oSalesInv.CH_Amount.money > 0 Then CurRecF = New RectangleF(XL0, YCurrent, nEndDesW, 100) sCurTPrintStr = "TRAVELLER CHECK AMOUNT:" e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XL0, YCurrent) dHline = e.Graphics.MeasureString(sCurTPrintStr, MyConFont, nEndDesW, MyConformat).Height

sCurTPrintStr = FormatCurrency(oSalesInv.CH_Amount.money).ToString e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XL4, YCurrent)

YCurrent = YCurrent + dHline End If

''判断Room Card支付 If oSalesInv.RC_Amount.money > 0 Then CurRecF = New RectangleF(XL0, YCurrent, nEndDesW, 100) sCurTPrintStr = "ROOM CARD AMOUNT:" e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XL0, YCurrent) dHline = e.Graphics.MeasureString(sCurTPrintStr, MyConFont, nEndDesW, MyConformat).Height

sCurTPrintStr = FormatCurrency(oSalesInv.RC_Amount.money).ToString e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XL4, YCurrent)

YCurrent = YCurrent + dHline End If

''判断Gift Card支付 If oSalesInv.GC_Amount.money > 0 Then CurRecF = New RectangleF(XL0, YCurrent, nEndDesW, 100) sCurTPrintStr = "GIFT CARD AMOUNT:" e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XL0, YCurrent) dHline = e.Graphics.MeasureString(sCurTPrintStr, MyConFont, nEndDesW, MyConformat).Height

sCurTPrintStr = FormatCurrency(oSalesInv.GC_Amount.money).ToString e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XL4, YCurrent)

YCurrent = YCurrent + dHline End If ''判断Debit Card支付 If oSalesInv.DC_Amount.money > 0 Then CurRecF = New RectangleF(XL0, YCurrent, nEndDesW, 100) sCurTPrintStr = "DEBIT CARD AMOUNT:" e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XL0, YCurrent) dHline = e.Graphics.MeasureString(sCurTPrintStr, MyConFont, nEndDesW, MyConformat).Height

sCurTPrintStr = FormatCurrency(oSalesInv.DC_Amount.money).ToString e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XL4, YCurrent)

YCurrent = YCurrent + dHline End If ''判断On Account支付 If oSalesInv.OA_Amount.money > 0 Then CurRecF = New RectangleF(XL0, YCurrent, nEndDesW, 100) sCurTPrintStr = "STORE CREDIT:" e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XL0, YCurrent) dHline = e.Graphics.MeasureString(sCurTPrintStr, MyConFont, nEndDesW, MyConformat).Height

sCurTPrintStr = FormatCurrency(oSalesInv.OA_Amount.money).ToString e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XL4, YCurrent)

YCurrent = YCurrent + dHline End If ''判断Food Stamps支付 If oSalesInv.FS_Amount.money > 0 Then CurRecF = New RectangleF(XL0, YCurrent, nEndDesW, 100) sCurTPrintStr = "FOOD STAMPS :" e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XL0, YCurrent) dHline = e.Graphics.MeasureString(sCurTPrintStr, MyConFont, nEndDesW, MyConformat).Height

sCurTPrintStr = FormatCurrency(oSalesInv.FS_Amount.money).ToString e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XL4, YCurrent)

YCurrent = YCurrent + dHline End If

''AMT TENDERED CurRecF = New RectangleF(XL0, YCurrent, nEndDesW, 100) sCurTPrintStr = "PAYMENT:" e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XL0, YCurrent) dHline = e.Graphics.MeasureString(sCurTPrintStr, MyConFont, nEndDesW, MyConformat).Height

sCurTPrintStr = FormatCurrency(Me.AmtTendered.money).ToString e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XL4, YCurrent)

YCurrent = YCurrent + dHline

''Change due CurRecF = New RectangleF(XL0, YCurrent, nEndDesW, 100) sCurTPrintStr = "CHANGE DUE:" e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XL0, YCurrent) dHline = e.Graphics.MeasureString(sCurTPrintStr, MyConFont, nEndDesW, MyConformat).Height

sCurTPrintStr = FormatCurrency(Me.AmtChange.money).ToString e.Graphics.DrawString(sCurTPrintStr, MyConFont, Brushes.Black, XL4, YCurrent)

'YCurrent = YCurrent + dHline

end sub

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。