How to print using vb.net on paper with fixed-width and dynamic height
I'm developing a Point Of Sale (POS) app in VB.NET and .NET Framework 3.5 where multiple items can bought at a time. I need to print all items: their code, name quantity, price in a row-column manner.
SHOP NAME date ========== ===== SL CODE NAME QTY PRICE == ===== ===== === ===== 1 ANC-059 Pencil 1 $2.00 2 ASNC-009 Pencil 1 $2.00 3 ASNC-09 Pencil 1 $2.00 4 ASNC-009 Pencil 1 $2.00
The width of page is fixed but the height will be dynamic.
The printout will be printed on rolled paper normally used in a POS system.
How can this be done?
Standard winforms printing:
Try 'Set up the document for printing and create a new PrintDocument object. Dim pd As New Printing.PrintDocument 'Set the event handler for the printpage event of the PrintDocument. AddHandler pd.PrintPage, AddressOf pd_PrintPage 'Set the printer name. pd.PrinterSettings.PrinterName = PrintDialog1.PrinterSettings.PrinterName 'Print the document by using the print method for the PrintDocument, which triggers the PrintPage event pd.Print() Catch ex As Exception MessageBox.Show(ex.Message) End Try 'The PrintPage event is raised for each page to be printed. Private Sub pd_PrintPage(ByVal sender As Object, ByVal ev As Printing.PrintPageEventArgs) 'Set up the default graphics object that will be used to do the actual printing. Dim g As Graphics g = ev.Graphics Dim tHeight as Double Dim txt as String = "My text goes here" g.DrawString(txt, myFont, myBrush, xPosition, yPosition, StringAlignment.Near) 'Measure the height (on the page) of the item that you have just drawn, so that 'you can place the next item below it. tHeight = g.MeasureString("Customer", fntBlue).Height() txt = "My new line of text" g.DrawString(txt, myFont, myBrush, xPosition, yPosition + tHeight, StringAlignment.Near) '.....continue printing other items End Sub