none
Printing a Pos Receipt in Vb.net RRS feed

  • Question

  • 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

    Wednesday, May 29, 2019 2:04 PM

All replies

  • 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

    PrivateSubprint_Printpage(ByValsender AsObject, ByVale AsSystem.Drawing.Printing.PrintPageEventArgs)

    companyPhone1Height = Convert.ToSingle(CompanyPhone1.GetHeight)

    DimTextbox1font AsNewDrawing.Font("Tahoma", 14, Drawing.FontStyle.Bold)

    DimxPos AsSingle= 0 '(horizontal positioning/alignment)

    DimxPoscenter AsSingle= 0 '(for centering text)

    DimyPos AsSingle= 20 '(vertial positioning/alignmemt margin with a 70units start off in this case for the image or logo)

    DimSize AsSystem.Drawing.SizeF'

    DimxPosmargin AsSingle= 15 '(horizontal positioning/alignment margins)

    Size = e.Graphics.MeasureString(companyNameText1, CompanyName1)

     

    xPoscenter = ((e.PageBounds.Width * e.Graphics.PageScale) / 2) - (Size.Width / 2)

     

    e.Graphics.DrawString(companyNameText1, CompanyName1, Drawing.Brushes.Black, xPoscenter, yPos)


    yPos = CSng(yPos + Size.Height * 1.0)

    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)

    yPos = CSng(yPos + Size.Height * 1.0)

    ............ and so on

    EndSub

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

    Thanks in advance

    ade

    • Merged by Alex Li-MSFT Thursday, May 30, 2019 2:03 AM same case
    Tuesday, May 28, 2019 8:20 PM
  • Hello,

    I highly recommend placing your code into a code block, second button on the right of the toolbar.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, May 28, 2019 9:54 PM
    Moderator
  • Real POS printers have their only control language/system to avoid that you need to do the layout/bitmap calculus as you do.

    Thus you should look into the developer manual for your actual printer.

    Wednesday, May 29, 2019 2:21 PM
  • Real POS printers have their only control language/system to avoid that you need to do the layout/bitmap calculus as you do.

    Thus you should look into the developer manual for your actual printer.

    Stefan, 

    I could not have written this better.

    :-)


    Success
    Cor

    Wednesday, May 29, 2019 2:43 PM
  • In the first place, your printer hardware and printer driver may not match.

    The question wrote that it uses StarMicronics SP742, but the program source code is "Ticket" of "EPSON TM-T70 Receipt".
    Then you will not be able to print correctly.

    Install the correct printer driver and select and use that printer.

    Please refer to the following link.

    SP742 Impact Printers

    Star Micronics Support Database
    Product Type : Impact Dot Matrix
    Printer Family : SP700
    Printer Model : SP700
    Interface : USB (or Serial)
    Operating System : Windows 10 / 8 / 7 / Vista / XP
    Language : English  

    Star Micronics Developers Section  

    Wednesday, June 19, 2019 12:45 AM