Showing posts with label Printing and Reports. Show all posts
Showing posts with label Printing and Reports. Show all posts

Monday, April 12, 2010

Dot Matrix Printing - Part 2

How to use dot matrix printing in VB 6.0


In the first part of this tutorial we discussed the basics of printing in Vb 6.0. We discussed the lack of direct text

printing support in Windows. Now we will see the Win32 API functions used to print / emulate Dos mode text printing using

Visual Basic 6.0

First declare the following functions and types in your code. Better use a separate module to put these declarations.

Private Type DOCINFO
    pDocname As String
    pOutputFile As String
    pDatatype As String
End Type
Private Declare Function ClosePrinter Lib "winspool.drv" (ByVal hPrinter As Long) As Long
Private Declare Function EndDocPrinter Lib "winspool.drv" (ByVal hPrinter As Long) As Long
Private Declare Function EndPagePrinter Lib "winspool.drv" (ByVal hPrinter As Long) As Long
Private Declare Function OpenPrinter Lib "winspool.drv" Alias "OpenPrinterA" (ByVal pPrinterName As String, phPrinter As

Long, ByVal pDefault As Long) As Long
Private Declare Function StartDocPrinter Lib "winspool.drv" Alias "StartDocPrinterA" (ByVal hPrinter As Long, ByVal Level As

Long, pDocInfo As DOCINFO) As Long
Private Declare Function StartPagePrinter Lib "winspool.drv" (ByVal hPrinter As Long) As Long
Private Declare Function WritePrinter Lib "winspool.drv" (ByVal hPrinter As Long, pBuf As Any, ByVal cdBuf As Long, pcWritten

As Long) As Long

Thats all about declarations.

Then write the following function

Public Function RawPrint(Optional FileToPrint As String) As Boolean
   
    Dim lhPrinter As Long
    Dim lReturn As Long
    Dim lpcWritten As Long
    Dim lDoc As Long
    Dim sWrittenData As String
    Dim MyDocInfo As DOCINFO
    If Printers.Count <= 0 Then
        MsgBox "No printer(s) found!"
        Exit Function
    End If
    OpenTextFile ' Write a function to open the text file to be printed.
   
    lReturn = OpenPrinter(Printer.DeviceName, lhPrinter, 0)
    If lReturn = 0 Then
        MsgBox "Printing failed!"
        Exit Function
    End If
    MyDocInfo.pDocname = "Ultimate Solutions"
    MyDocInfo.pOutputFile = vbNullString
    MyDocInfo.pDatatype = "RAW"
    lDoc = StartDocPrinter(lhPrinter, 1, MyDocInfo)
    Call StartPagePrinter(lhPrinter)
    sWrittenData = "Test printing"
    While Not EOF(mRptPrint)
        Line Input #mRptPrint, sWrittenData
        sWrittenData = sWrittenData & vbLf
        lReturn = WritePrinter(lhPrinter, ByVal sWrittenData, Len(sWrittenData), lpcWritten)
    Wend
   
    lReturn = EndPagePrinter(lhPrinter)
    lReturn = EndDocPrinter(lhPrinter)
    lReturn = ClosePrinter(lhPrinter)
    Close #mRptPrint
End Function

This function takes a text file as parameter. It uses win32 printer functions declared above to print the text file line by

line. Because of RAW printing in text mode. The text will have standard size and will use standard printer font's like

courier, sans serif, etc. The bold, italic, underline as we do normally in a Ms-word file requires a bit of extra coding.

This requires control codes to be sent to the Dot Matrix printers.

You can also set CPI or characters per Inch. This will tell the printer to set the font width appropriately. You can also set

the paper size in inches or lines. A typical A4 size paper can hold about 72 lines.

To start bold printing use ESC + G (capital G) that is ASCII 27 + G, the actual code in VB is chr(27) +"M". This will the

tell the printer to start printing in bold letters. To stop bold printing just send chr(27) + "H"

sending means the WritePrinter function that sends output to printer. In the above printing function the sWrittenData

parameter should be set to this value (chr(27)+"H") for stop bold printing.

To set the characters per Inch,


To print 12 characters per Inch use Chr(27) + "g"
To print 17 characters per inch use Chr(27) + Chr(15) in VB
To Print 20 characters per inch use Chr(27) + "M" + Chr(27) + Chr(15)

To set paper size to 6 inch use Chr(27) + "C" + Chr(35)
For 8 inches use Chr(27) + "C" + Chr(47)
For 12 inch use Chr(27) + "C" + Chr(71)

For direct printing you can use another method instead of Win32 API. Just use the file IO in VB 6.0. Just open the printer port as if it were a file.

For file opening you will use the syntax.

Open 'filename' for output as 'filenumber'

This is the standard VB 6.0 syntax for opening a file for writing.

Commonly Dot matrix printers are connected to LPT ports. In most cases this will be LPT1:

so the following syntax can be used.

Open "LPT1:" for output as 1

to get the next available filenumber use FreeFile function.

Then use the Print FileNumber, Text syntax to print.

For example, Print 1, "HelloWorld!"

Then close the file using 'Close Filenumber' syntax, For example Close 1. Immediately after closing the file the printer will start printer whatever you printed using Print syntax. Thats all about Dot Matrix Fast printing using VB 6.0!

Wednesday, April 7, 2010

How to print using Win32 Printer functions in VB 6.0?

Dot Matrix Printing Part 1


Reporting and Printing are essential part any commercial application. Visual Basic offers many methods to create professional reports and print those reports. Seagate Crystal Reports (now business objects) is the most popular report authoring tool for Visual basic. It can fetch data from any kind of Database SQL, MS-Access, OLEDB and ODBC. VB 6.0 offers in-built reporting tool data reports.

But one draw back using these tools is observed while printing. When you print a report using dot-matrix line printer it takes a long time to print text reports. This is because the it uses Windows GDI printing technology. Windows prints everything as graphics not as text. In contrast DOS applications use text mode for printing. Thus the printing will be faster in DOS applications. For point-of-sale and counter billing purpose this speed is desired. Print Quality is not important in POS and billing softwares.

To achieve the fast printing in windows applications you must use the raw printing method. This is achieved by using Win32 API functions. There are standard Win32 printer functions to accomplish raw printing in Windows Environment.

OpenPrinter()
StartDocPrinter()
StartPagePrinter()
WritePrinter()

are the Win32 functions used to open a printer and get a handler and write data to the printer. In the next part of this tutorial we will see the declaration of these functions in VB code, passing of parameters to these functions, writing raw data and closing printer object. The examples should help you create your own printing solution for your VB 6.0 projects that use Dot matrix printing for POS or billing. You can also use text printing to print large Accounting statements etc.

 Dot matrix printing part 2