ARTICLE

Printing directly to the Printer

Posted by John O Donnell Articles | Visual Basic 2010 June 25, 2003
Using this code enables you to print directly to the printer using WIN32 api calls and therefore should enable you to print at maximum speed rather than relying in the Windows Printing subsystems. Additionally I have added code to show how to send PCL codes to the printer.
Download Files:
 
Reader Level:

Using this code enables you to print directly to the printer using WIN32 API calls and therefore should enable you to print at maximum speed rather than relying in the Windows Printing subsystems. Additionally I have added code to show how to send PCL codes to the printer.

The code has been compiled using the Everett beta of Visual Studio.NET however if you have not got this just coy and paste the code into a console project and recompile.

' PrintDirect.vb
' Shows how to write data directly to the printer using Win32 API's
'Adapted from Microsoft Support article Q298141
'This code assumes you have a printer at share file://192.168.1.101/hpl
'This code sends Hewlett Packard PCL5 codes to the printer to print
' out a rectangle in the middle of the page.
Imports System
Imports System.Text
Imports System.Runtime.InteropServices
<StructLayout(LayoutKind.Sequential)> _
Public Structure DOCINFO<MarshalAs(UnmanagedType.LPWStr)>
Public pDocName As String<MarshalAs(UnmanagedType.LPWStr)>
Public pOutputFile As String<MarshalAs(UnmanagedType.LPWStr)>
Public pDataType As String
End
Structure 'DOCINFO
Public Class PrintDirect
<DllImport("winspool.drv", CharSet := CharSet.Unicode, ExactSpelling :=
False, CallingConvention := CallingConvention.StdCall)> _
Public Shared Function OpenPrinter(pPrinterName As String, ByRef phPrinter As IntPtr, pDefault As Integer) As Long
<DllImport("winspool.drv", CharSet := CharSet.Unicode, ExactSpelling := False, CallingConvention := CallingConvention.StdCall)> _
Public Shared Function StartDocPrinter(hPrinter As IntPtr, Level As Integer, ByRef pDocInfo As DOCINFO) As Long
<DllImport("winspool.drv", CharSet := CharSet.Unicode, ExactSpelling := True, CallingConvention := CallingConvention.StdCall)> _
Public Shared Function StartPagePrinter(hPrinter As IntPtr) As Long
<DllImport("winspool.drv", CharSet := CharSet.Ansi, ExactSpelling := True, CallingConvention := CallingConvention.StdCall)> _
Public Shared Function WritePrinter(hPrinter As IntPtr, data As String, buf As Integer, ByRef pcWritten As Integer) As Long
<DllImport("winspool.drv", CharSet := CharSet.Unicode, ExactSpelling := True, CallingConvention := CallingConvention.StdCall)> _
Public Shared Function EndPagePrinter(hPrinter As IntPtr) As Long
<DllImport("winspool.drv", CharSet := CharSet.Unicode, ExactSpelling := True, CallingConvention := CallingConvention.StdCall)> _
Public Shared Function EndDocPrinter(hPrinter As IntPtr) As Long
<DllImport("winspool.drv", CharSet := CharSet.Unicode, ExactSpelling := True, CallingConvention := CallingConvention.StdCall)> _
Public Shared Function ClosePrinter(hPrinter As IntPtr) As Long
End
Class 'PrintDirect
Public Class App
Public Shared Sub Main()
Dim lhPrinter As New System.IntPtr()
Dim di As New DOCINFO()
Dim pcWritten As Integer = 0
Dim st1 As String
' text to print with a form feed character
st1 = "This is an example of printing directly to a printer" + ControlChars.FormFeed
di.pDocName = "my test document"
di.pDataType = "RAW"
' the \x1b means an ascii escape character
st1 = ChrW(27) + "*c600a6b0P" + ControlChars.FormFeed
'lhPrinter contains the handle for the printer opened
'If lhPrinter is 0 then an error has occured
PrintDirect.OpenPrinter("\\192.168.1.101\hpl", lhPrinter, 0)
PrintDirect.StartDocPrinter(lhPrinter, 1, di)
PrintDirect.StartPagePrinter(lhPrinter)
Try
' Moves the cursor 900 dots (3 inches at 300 dpi) in from the left margin, and
' 600 dots (2 inches at 300 dpi) down from the top margin.
st1 = ChrW(27) + "*p900x600Y"
PrintDirect.WritePrinter(lhPrinter, st1, st1.Length, pcWritten)
' Using the print model commands for rectangle dimensions, "600a" specifies a rectangle
' with a horizontal size or width of 600 dots, and "6b" specifies a vertical
' size or height of 6 dots. The 0P selects the solid black rectangular area fill.
st1 = ChrW(27) + "*c600a6b0P"
PrintDirect.WritePrinter(lhPrinter, st1, st1.Length, pcWritten)
' Specifies a rectangle with width of 6 dots, height of 600 dots, and a
' fill pattern of solid black.
st1 = ChrW(27) + "*c6a600b0P"
PrintDirect.WritePrinter(lhPrinter, st1, st1.Length, pcWritten)
' Moves the current cursor position to 900 dots, from the left margin and
' 1200 dots down from the top margin.
st1 = ChrW(27) + "*p900x1200Y"
PrintDirect.WritePrinter(lhPrinter, st1, st1.Length, pcWritten)
' Specifies a rectangle with a width of 606 dots, a height of 6 dots and a // fill pattern of solid black.
st1 = ChrW(27) + "*c606a6b0P"
PrintDirect.WritePrinter(lhPrinter, st1, st1.Length, pcWritten)
' Moves the current cursor position to 1500 dots from the left margin and
' 600 dots down from the top margin.
st1 = ChrW(27) + "*p1500x600Y"
PrintDirect.WritePrinter(lhPrinter, st1, st1.Length, pcWritten)
' Specifies a rectangle with a width of 6 dots, a height of 600 dots and a
' fill pattern of solid black.
st1 = ChrW(27) + "*c6a600b0P"
PrintDirect.WritePrinter(lhPrinter, st1, st1.Length, pcWritten)
' Send a form feed character to the printer
st1 = ControlChars.FormFeed
PrintDirect.WritePrinter(lhPrinter, st1, st1.Length, pcWritten)
Catch e As Exception
Console.WriteLine(e.Message)
End Try
PrintDirect.EndPagePrinter(lhPrinter)
PrintDirect.EndDocPrinter(lhPrinter)
PrintDirect.ClosePrinter(lhPrinter)
End Sub 'Main
End Class 'App

share this article :
post comment
 

Imports System.Runtime.InteropServices ''' <summary> ''' Shows how to write data directly to the printer using Win32 API's ''' </summary> ''' <remarks>Adapted from Microsoft Support article Q298141 ''' and http://www.vbdotnetheaven.com/UploadFile/jodonnell/PrintingDirectly04202005010211AM/PrintingDirectly.aspx ''' </remarks> ''' Public Class PrintDirect Public Structure DOCINFO Dim pDocName As String Dim pOutputFile As String Dim pDatatype As String End Structure <DllImport("winspool.drv", CharSet:=CharSet.Unicode, ExactSpelling:=False, CallingConvention:=CallingConvention.StdCall)> _ Public Shared Function OpenPrinter(ByVal pPrinterName As String, ByRef phPrinter As IntPtr, ByVal pDefault As Integer) As Long End Function <DllImport("winspool.drv", CharSet:=CharSet.Unicode, ExactSpelling:=False, CallingConvention:=CallingConvention.StdCall)> _ Public Shared Function StartDocPrinter(ByVal hPrinter As IntPtr, ByVal Level As Integer, ByRef pDocInfo As DOCINFO) As Long End Function <DllImport("winspool.drv", CharSet:=CharSet.Unicode, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _ Public Shared Function StartPagePrinter(ByVal hPrinter As IntPtr) As Long End Function <DllImport("winspool.drv", CharSet:=CharSet.Ansi, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _ Public Shared Function WritePrinter(ByVal hPrinter As IntPtr, ByVal data As String, ByVal buf As Integer, ByRef pcWritten As Integer) As Long End Function <DllImport("winspool.drv", CharSet:=CharSet.Unicode, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _ Public Shared Function EndPagePrinter(ByVal hPrinter As IntPtr) As Long End Function <DllImport("winspool.drv", CharSet:=CharSet.Unicode, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _ Public Shared Function EndDocPrinter(ByVal hPrinter As IntPtr) As Long End Function <DllImport("winspool.drv", CharSet:=CharSet.Unicode, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _ Public Shared Function ClosePrinter(ByVal hPrinter As IntPtr) As Long End Function Public Shared Sub Print(ByVal StringToPrint As String, ByVal PrinterName As String) Dim lhPrinter As New System.IntPtr() Dim di As New DOCINFO() Dim pcWritten As Integer = 0 di.pDocName = "Impresion Desde AsVentas" di.pDatatype = "RAW" 'lhPrinter contains the handle for the printer opened 'If lhPrinter is 0 then an error has occured OpenPrinter(PrinterName, lhPrinter, 0) StartDocPrinter(lhPrinter, 1, di) StartPagePrinter(lhPrinter) Try WritePrinter(lhPrinter, StringToPrint, StringToPrint.Length, pcWritten) Catch ex As Exception MsgBox("Error al Imprimir: " & vbCr & ex.Message) Finally EndPagePrinter(lhPrinter) EndDocPrinter(lhPrinter) ClosePrinter(lhPrinter) End Try End Sub End Class

Posted by Enel Almonte Feb 03, 2011

sir where i place this code in form

Posted by saritha praveen Jan 07, 2011

Hi Thanks this work fine for me..
pls tell me how can i make the text bold underline or italic??

Posted by Yesudasan Moses Sep 09, 2010

Change the format from RAW to Text

Posted by Yesudasan Moses Sep 09, 2010

Its possible to print image using Winspool drv

Posted by jeno winning Sep 15, 2009
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Become a Sponsor