Printing a Pos Receipt in Vb.net

  • Thread starter Thread starter madmabs
  • Start date Start date
M

madmabs

Guest
Hello guys I have a question and its on this project am working on . I have developed the application and I wish for the application to print out receipts via a pos printer.

I can generate and preview the receipt which turns up fine and I can send it directly to the printer, but when I send it to the printer all the characters are all scattered on the paper roll and there isn't any alignment as compared to when I try previewing it.

Note: am using a star printer (SP742), the system communicates well with printer, although I used I used a Generic/Text only to driver the printer which still runs it well.


Here is what I have in code to generate the receipt and send it directly to the Pos Printer.


(



PrivateSubpreparedlgprint()



' Print immediately.

' Make a PrintDocument object.

DimPrint_Document AsPrintDocument= PreparePrintDocument()

' Print immediately.

Print_Document.Print()



EndSub



'creating the print format


PrivateFunctionPreparePrintDocument() AsPrintDocument

'this functions prepares and handle the print format, size and structure and print document object

'Make the printDocument Object

DimPrint_Document AsNewPrintDocument

Using(Print_Document)

Dimmm2hin AsDouble= 25.4 * 100

'mm2hin - This Function Translates Millimeters to Half inches

DimpkCustomSize1 AsNewPaperSize("Snap Paper", CInt(80 / mm2hin), 0)

'paper size is set for any measurment my paper was 80mm wide with no end...

Dimmargins AsNewMargins(CInt(5 / mm2hin), CInt(5 / mm2hin), 0, CInt(10 / mm2hin))

'sets the margins t0 5mill left and right and 10mill at the top

Print_Document.PrinterSettings.DefaultPageSettings.Margins = margins

Print_Document.PrinterSettings.PrinterName = "EPSON TM-T70 Receipt"

Print_Document.DocumentName = "Ticket"

Print_Document.PrinterSettings.DefaultPageSettings.PaperSize = pkCustomSize1

 

AddHandlerPrint_Document.PrintPage, AddressOfprint_Printpage

EndUsing

'install the printpage event handler

'Return the Object

ReturnPrint_Document

EndFunction


then I created a private function setting up the page alignments and position of the contents




'setting up the page and contents ,
Private Sub print_Printpage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)
companyPhone1Height = Convert.ToSingle(CompanyPhone1.GetHeight)
Dim Textbox1font As New Drawing.Font("Tahoma", 14, Drawing.FontStyle.Bold)
'-----Set the Picture at the top line
Dim xPos As Single = 0 '(horizontal positioning/alignment)
Dim xPoscenter As Single = 0 '(for centering text)
Dim yPos As Single = 20 '(vertial positioning/alignmemt margin with a 70units start off in this case for the image or logo)
Dim Size As System.Drawing.SizeF '(this variable measures the specified string by using the drawing font variable attached to the text. Mainly used in the case of text positioning)
Dim xPosmargin As Single = 15 '(horizontal positioning/alignment margins)
Size = e.Graphics.MeasureString(companyNameText1, CompanyName1)
 


'Now lets center this text
xPoscenter = ((e.PageBounds.Width * e.Graphics.PageScale) / 2) - (Size.Width / 2)
 
e.Graphics.DrawString(companyNameText1, CompanyName1, Drawing.Brushes.Black, xPoscenter, yPos)
'Now lets space the next line below..... adding a *1.0 to the end will blow out the ypos and will give a larger blank space
yPos = CSng(yPos + Size.Height * 1.0) 'takes the height of the string and adds the 70units to it
Size = e.Graphics.MeasureString(CompanyNameText2, CompanyName2)
'Now lets center this text
xPoscenter = ((e.PageBounds.Width * e.Graphics.PageScale) / 2) - (Size.Width / 2)
'Now this line draws the text as string and sets a font to use
e.Graphics.DrawString(CompanyNameText2, CompanyName2, Drawing.Brushes.Black, xPoscenter, yPos)
 
yPos = CSng(yPos + Size.Height * 1.0)
 


Size = e.Graphics.MeasureString(CompanyAddText1, CompanyAdd1)
'Now lets center this text
xPoscenter = ((e.PageBounds.Width * e.Graphics.PageScale) / 2) - (Size.Width / 2)
'Now this line draws the text as string and sets a font to use
e.Graphics.DrawString(CompanyAddText1, CompanyAdd1, Drawing.Brushes.Black, xPoscenter, yPos)
'Now lets space the next line below..... adding a *1.0 to the end will blow out the ypos and will give a larger blank space
yPos = CSng(yPos + Size.Height * 1.0)
Size = e.Graphics.MeasureString(CompanyAddText2, CompanyAdd2)
'Now lets center this text
xPoscenter = ((e.PageBounds.Width * e.Graphics.PageScale) / 2) - (Size.Width / 2)
'Now this line draws the text as string and sets a font to use
e.Graphics.DrawString(CompanyAddText2, CompanyAdd2, Drawing.Brushes.Black, xPoscenter, yPos)
'Now lets space the next line below..... adding a *1.0 to the end will blow out the ypos and will give a larger blank space
yPos = CSng(yPos + Size.Height * 1.0)
Size = e.Graphics.MeasureString(CompanyCity, City)
'Now lets center this text
xPoscenter = ((e.PageBounds.Width * e.Graphics.PageScale) / 2) - (Size.Width / 2)
'Now this line draws the text as string and sets a font to use
e.Graphics.DrawString(CompanyCity, City, Drawing.Brushes.Black, xPoscenter, yPos)
'Now lets space the next line below..... adding a *1.0 to the end will blow out the ypos and will give a larger blank space
yPos = CSng(yPos + Size.Height * 1.0)
Size = e.Graphics.MeasureString(CompanyState, State)
'Now lets center this text
xPoscenter = ((e.PageBounds.Width * e.Graphics.PageScale) / 2) - (Size.Width / 2)
'Now this line draws the text as string and sets a font to use
e.Graphics.DrawString(CompanyState, State, Drawing.Brushes.Black, xPoscenter, yPos)
'Now lets space the next line below..... adding a *1.0 to the end will blow out the ypos and will give a larger blank space
yPos = CSng(yPos + Size.Height * 1.0)
'
Size = e.Graphics.MeasureString(CompanyCountry, Country)
'Now lets center this text
xPoscenter = ((e.PageBounds.Width * e.Graphics.PageScale) / 2) - (Size.Width / 2)
'Now this line draws the text as string and sets a font to use
e.Graphics.DrawString(CompanyCountry, Country, Drawing.Brushes.Black, xPoscenter, yPos)
'Now lets space the next line below..... adding a *1.0 to the end will blow out the ypos and will give a larger blank space
yPos = CSng(yPos + Size.Height * 1.0)
Size = e.Graphics.MeasureString(CompanyPhoneNumber1, CompanyPhone1)
'Now lets center this text
xPoscenter = ((e.PageBounds.Width * e.Graphics.PageScale) / 2) - (Size.Width / 2)
'Now this line draws the text as string and sets a font to use
e.Graphics.DrawString(CompanyPhoneNumber1, CompanyPhone1, Drawing.Brushes.Black, xPoscenter, yPos)
yPos = CSng(yPos + Size.Height * 1.0)
Size = e.Graphics.MeasureString(CompanyPhoneNumber2, CompanyPhone1)
'Now lets center this text
xPoscenter = ((e.PageBounds.Width * e.Graphics.PageScale) / 2) - (Size.Width / 2)
'Now this line draws the text as string and sets a font to use
e.Graphics.DrawString(CompanyPhoneNumber2, CompanyPhone1, Drawing.Brushes.Black, xPoscenter, yPos)
'Now lets space the next line below..... adding a *1.0 to the end will blow out the ypos and will give a larger blank space
yPos = CSng(yPos + Size.Height * 2.0)
Size = e.Graphics.MeasureString(InvoiceDate, CompanyPhone1)
e.Graphics.DrawString(InvoiceDate, CompanyPhone1, Drawing.Brushes.Black, Xdate, yPos)
Size = e.Graphics.MeasureString(TextBox1.Text, CompanyPhone1)
'Now lets center this text
xPoscenter = ((e.PageBounds.Width * e.Graphics.PageScale) / 2) - (Size.Width / 2)
'Now this line draws the text as string and sets a font to use
e.Graphics.DrawString(TextBox1.Text, CompanyPhone1, Drawing.Brushes.Black, Xdate + 375, yPos)
Size = e.Graphics.MeasureString(FrmOrders.lblTNOP.Text, CompanyPhone1)
'Now lets center this text
xPoscenter = ((e.PageBounds.Width * e.Graphics.PageScale) / 2) - (Size.Width / 2)
'Now this line draws the text as string and sets a font to use
e.Graphics.DrawString(FrmOrders.lblTNOP.Text, CompanyPhone1, Drawing.Brushes.Black, Xdate + 750, yPos)
 


yPos = CSng(yPos + Size.Height * 1.0)
'This is the Line stretching from width to width
Size = e.Graphics.MeasureString(Breakline1, BreakFonts)
e.Graphics.DrawLine(Drawing.Pens.Black, xPos, yPos, e.Graphics.PageScale * e.PageBounds.Width - xPos, yPos)
'Now lets space the next line below..... adding a *4.0 to the end will blow out the ypos and will give a larger blank space
yPos = CSng(yPos + Size.Height * 2.0) + companyPhone1Height
'This inserts the Quantity header
Size = e.Graphics.MeasureString("QUANTITY", Headerfonts)
e.Graphics.DrawString("QUANTITY", Headerfonts, Drawing.Brushes.Black, xPosmargin, yPos)
 
Dim XUnitPrice As Single = xPos + Convert.ToSingle(e.Graphics.MeasureString("PRODUCT NAME", Headerfonts).Width) + 25
Size = e.Graphics.MeasureString("ITEM DESCRIPTION", Headerfonts)
e.Graphics.DrawString("ITEM DESCRIPTION", Headerfonts, Drawing.Brushes.Black, XUnitPrice, yPos)
 
Dim XUnitcost As Single = xPos + Convert.ToSingle(e.Graphics.MeasureString("PRODUCT NAME", Headerfonts).Width) + 135
Size = e.Graphics.MeasureString("UNIT COST", Headerfonts)
e.Graphics.DrawString("UNIT COST", Headerfonts, Drawing.Brushes.Black, XUnitcost, yPos)
 
Dim Xitemtotal As Single = XUnitPrice + Convert.ToSingle(e.Graphics.MeasureString("PRODUCT NAME", Headerfonts).Width) + 135
 
Size = e.Graphics.MeasureString("ITEM-TOTAL", Headerfonts)
e.Graphics.DrawString("ITEM-TOTAL", Headerfonts, Drawing.Brushes.Black, Xitemtotal, yPos)
 
 
'Now lets space the next line below..... adding a *1.0 to the end will blow out the ypos and will give a larger blank space
yPos = CSng(yPos + Size.Height * 1.0)
Size = e.Graphics.MeasureString(breakline2, BreakFonts)
yPos = CSng(yPos + Size.Height * 2.0)
e.Graphics.DrawLine(Drawing.Pens.Black, xPos, yPos, e.Graphics.PageScale * e.PageBounds.Width - xPos, yPos)
yPos = CSng(yPos + Size.Height * 1.0) + companyPhone1Height
 
 
 
 
For i As Integer = 0 To FrmOrders.ListView1.Items.Count - 1
 




cellvalue = FrmOrders.ListView1.Items(i).SubItems(2).Text 'Quantity values
e.Graphics.DrawString(cellvalue, Itemfonts, Drawing.Brushes.Black, xPosmargin + 20, yPos)
 
 
cellvalue = String.Format("{0:0:00}", FrmOrders.ListView1.Items(i).SubItems(0).Text) 'Description values
e.Graphics.DrawString(cellvalue, Headerfonts, Drawing.Brushes.Black, XUnitPrice, yPos)
 
 
cellvalue = FrmOrders.ListView1.Items(i).SubItems(0).Text
e.Graphics.DrawString(cellvalue, Headerfonts, Drawing.Brushes.Black, XUnitPrice, yPos)
 
 
cellvalue = FrmOrders.ListView1.Items(i).SubItems(1).Text 'unit cost values
e.Graphics.DrawString(cellvalue, Headerfonts, Drawing.Brushes.Black, XUnitcost + 2, yPos)
 
 
cellvalue = FrmOrders.ListView1.Items(i).SubItems(3).Text 'Total cost values
e.Graphics.DrawString(cellvalue, Headerfonts, Drawing.Brushes.Black, Xitemtotal + 10, yPos)
yPos = CSng(yPos + Size.Height * 1.0) + companyPhone1Height
 
 
Next
yPos = CSng(yPos + Size.Height * 1.0) + companyPhone1Height
 


 
yPos = CSng(yPos + Size.Height * 1.0)
Size = e.Graphics.MeasureString(breakline2, BreakFonts)
yPos = CSng(yPos + Size.Height * 2.0)
e.Graphics.DrawLine(Drawing.Pens.Black, xPos, yPos, e.Graphics.PageScale * e.PageBounds.Width - xPos, yPos)
yPos = CSng(yPos + Size.Height * 1.0) + companyPhone1Height
Size = e.Graphics.MeasureString(FrmOrders.LblServicepriceTotal.Text, CompanyAdd2)
xPoscenter = ((e.PageBounds.Width * e.Graphics.PageScale) / 2) - (Size.Width / 2)
e.Graphics.DrawString(FrmOrders.LblServicepriceTotal.Text, CompanyAdd2, Drawing.Brushes.Black, Xitemtotal + 7, yPos)
 
 
End Sub












am posting the code in the code block as recommended Karen)

So what am I doing wrong , why are the characters scattered all over the page, some characters are even printed on others, why are they not aligned on the roll like when I preview it


Thanks in advance


ade

Continue reading...
 
Back
Top