Wednesday, July 15, 2015

vb: Line by line printing

I just wanna share this piece of code that will help some of my fellow programmer's out there that are having a problem in printing something line by line.

Mostly, this kind of printing is done on printer that uses a carbon/ribbon printer like Epson Lx300+. Also, if you want the printer to stop right after the last character then this code will help you a lot.

This will also use the RawPrinterHelp class found at https://support.microsoft.com/en-us/kb/322090 and some ASCII code to send command on the printer. The ASCII command that I used in this code is for Epson printer.


 Dim FN As Long = FreeFile() ' get the next id to use

 FileOpen(FN, "C:\report.prn", OpenMode.Output)

PrintLine(FN, Chr(27) + Chr(64))  ' Initialize Printer

PrintLine(FN, TAB(3), "-------------------- -------------------------- ------")
PrintLine(FN, TAB(3), "         Item              Description            Qty")
PrintLine(FN, TAB(3), "-------------------- -------------------------- ------")

PrintLine(FN) ' empty row

PrintLine(FN,Tab(3),"Item1",Tab(24),"Item1 Description",Tab(51),12) 
PrintLine(FN,Tab(3),"Item2",Tab(24),"Item2 Description",Tab(51),10) 
PrintLine(FN,Tab(3),"Item3",Tab(24),"Item3 Description",Tab(51),35) 

PrintLine(FN, chr(12)) ' Eject
FileClose(FN)

The above code will create the report and save it at C directory with "report.prn" filename. Open this report using notepad application.

It's also much better to print directly the report right after it has been made, the below code will be use for this.

To print the report created:


Dim SR As New StreamReader("C:\report.prn", System.Text.Encoding.Default)
Dim dataRead As [String] = SR.ReadToEnd()
SR.Close()
SR.Dispose()
RawPrinterHelper.SendStringToPrinter(printer name, dataRead )



No comments:

Post a Comment