//
you're reading...
IT-Center, VB.NET

POS Print in VB.NET ( Raw Print )

Are you understand for Raw print…? hmm this is print to paper in ancient age…
If you have COM printer like in parking system or general ledger printing, may be this will helping you. Before i see this code, the printer (COM) only write but didn’t have ink on paper. After using this code that problem is clear and I can write with format text like bold, double pixel ,italic style and other.

Create new class :

Imports System.IO
Imports System.Drawing.Printing
Imports System.Runtime.InteropServices

Public Class RawPrinterHelper
' Structure and API declarions:
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)> _
Structure DOCINFOW
<MarshalAs(UnmanagedType.LPWStr)> Public pDocName As String
<MarshalAs(UnmanagedType.LPWStr)> Public pOutputFile As String
<MarshalAs(UnmanagedType.LPWStr)> Public pDataType As String
End Structure

<DllImport("winspool.Drv", EntryPoint:="OpenPrinterW", _
SetLastError:=True, CharSet:=CharSet.Unicode, _
ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
Public Shared Function OpenPrinter(ByVal src As String, ByRef hPrinter As IntPtr, ByVal pd As Long) As Boolean
End Function
<DllImport("winspool.Drv", EntryPoint:="ClosePrinter", _
SetLastError:=True, CharSet:=CharSet.Unicode, _
ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
Public Shared Function ClosePrinter(ByVal hPrinter As IntPtr) As Boolean
End Function
<DllImport("winspool.Drv", EntryPoint:="StartDocPrinterW", _
SetLastError:=True, CharSet:=CharSet.Unicode, _
ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
Public Shared Function StartDocPrinter(ByVal hPrinter As IntPtr, ByVal level As Int32, ByRef pDI As DOCINFOW) As Boolean
End Function
<DllImport("winspool.Drv", EntryPoint:="EndDocPrinter", _
SetLastError:=True, CharSet:=CharSet.Unicode, _
ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
Public Shared Function EndDocPrinter(ByVal hPrinter As IntPtr) As Boolean
End Function
<DllImport("winspool.Drv", EntryPoint:="StartPagePrinter", _
SetLastError:=True, CharSet:=CharSet.Unicode, _
ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
Public Shared Function StartPagePrinter(ByVal hPrinter As IntPtr) As Boolean
End Function
<DllImport("winspool.Drv", EntryPoint:="EndPagePrinter", _
SetLastError:=True, CharSet:=CharSet.Unicode, _
ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
Public Shared Function EndPagePrinter(ByVal hPrinter As IntPtr) As Boolean
End Function
<DllImport("winspool.Drv", EntryPoint:="WritePrinter", _
SetLastError:=True, CharSet:=CharSet.Unicode, _
ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
Public Shared Function WritePrinter(ByVal hPrinter As IntPtr, ByVal pBytes As IntPtr, ByVal dwCount As Int32, ByRef dwWritten As Int32) As Boolean
End Function

' SendBytesToPrinter()
' When the function is given a printer name and an unmanaged array of
' bytes, the function sends those bytes to the print queue.
' Returns True on success or False on failure.
Public Shared Function SendBytesToPrinter(ByVal szPrinterName As String, ByVal pBytes As IntPtr, ByVal dwCount As Int32) As Boolean
Dim hPrinter As IntPtr      ' The printer handle.
Dim dwError As Int32        ' Last error - in case there was trouble.
Dim di As DOCINFOW          ' Describes your document (name, port, data type).
Dim dwWritten As Int32      ' The number of bytes written by WritePrinter().
Dim bSuccess As Boolean     ' Your success code.

' Set up the DOCINFO structure.
With di
.pDocName = "My Visual Basic .NET RAW Document"
.pDataType = "RAW"
End With
' Assume failure unless you specifically succeed.
bSuccess = False
If OpenPrinter(szPrinterName, hPrinter, 0) Then
If StartDocPrinter(hPrinter, 1, di) Then
If StartPagePrinter(hPrinter) Then
' Write your printer-specific bytes to the printer.
bSuccess = WritePrinter(hPrinter, pBytes, dwCount, dwWritten)
EndPagePrinter(hPrinter)
End If
EndDocPrinter(hPrinter)
End If
ClosePrinter(hPrinter)
End If
' If you did not succeed, GetLastError may give more information
' about why not.
If bSuccess = False Then
dwError = Marshal.GetLastWin32Error()
End If
Return bSuccess
End Function ' SendBytesToPrinter()

' SendFileToPrinter()
' When the function is given a file name and a printer name,
' the function reads the contents of the file and sends the
' contents to the printer.
' Presumes that the file contains printer-ready data.
' Shows how to use the SendBytesToPrinter function.
' Returns True on success or False on failure.
Public Shared Function SendFileToPrinter(ByVal szPrinterName As String, ByVal szFileName As String) As Boolean
' Open the file.
Dim fs As New FileStream(szFileName, FileMode.Open)
' Create a BinaryReader on the file.
Dim br As New BinaryReader(fs)
' Dim an array of bytes large enough to hold the file's contents.
Dim bytes(fs.Length) As Byte
Dim bSuccess As Boolean
' Your unmanaged pointer.
Dim pUnmanagedBytes As IntPtr

' Read the contents of the file into the array.
bytes = br.ReadBytes(fs.Length)
' Allocate some unmanaged memory for those bytes.
pUnmanagedBytes = Marshal.AllocCoTaskMem(fs.Length)
' Copy the managed byte array into the unmanaged array.
Marshal.Copy(bytes, 0, pUnmanagedBytes, fs.Length)
' Send the unmanaged bytes to the printer.
bSuccess = SendBytesToPrinter(szPrinterName, pUnmanagedBytes, fs.Length)
' Free the unmanaged memory that you allocated earlier.
Marshal.FreeCoTaskMem(pUnmanagedBytes)
Return bSuccess
End Function ' SendFileToPrinter()

' When the function is given a string and a printer name,
' the function sends the string to the printer as raw bytes.
Public Shared Function SendStringToPrinter(ByVal szPrinterName As String, ByVal szString As String)
Dim pBytes As IntPtr
Dim dwCount As Int32
' How many characters are in the string?
dwCount = szString.Length()
' Assume that the printer is expecting ANSI text, and then convert
' the string to ANSI text.
pBytes = Marshal.StringToCoTaskMemAnsi(szString)
' Send the converted ANSI string to the printer.
SendBytesToPrinter(szPrinterName, pBytes, dwCount)
Marshal.FreeCoTaskMem(pBytes)
End Function
End Class

How to using the class ?

     Public Sub PrintingOut()
        Dim s As String

        Dim pd As New PrintDialog()

        ' You need a string to send.
        s = "Hello, this is a test"
        ' Open the printer dialog box, and then allow the user to select a printer.
        pd.PrinterSettings = New PrinterSettings()
        If (pd.ShowDialog() = DialogResult.OK) Then
            RawPrinterHelper.SendStringToPrinter(pd.PrinterSettings.PrinterName, s)

        End If
    End Sub

Varible “s” can be assign POS code, this is example :

Public sess_ESC As String = ChrW(27) + "!"
Public sess_EPSON_BOLD_ON As String = sess_ESC + Chr(16) + sess_ESC + Chr(32)
Public sess_EPSON_BOLD_OFF As String = sess_ESC + Chr(0) + sess_ESC + Chr(0)
Public sess_STAR_BOLD_ON As String = sess_ESC + Chr(14) + sess_ESC + Chr(15)
Public sess_STAR_BOLD_OFF As String = sess_ESC + Chr(4) + sess_ESC + Chr(5)

Public sess_PRINT_BOLD_ON As String = sess_ESC + Chr(17)
Public sess_PRINT_BOLD_OFF As String = sess_ESC + Chr(0)

Example making bold text :

s = "  .: Parking System :. " & vbCrLf
s &= dtInfo(1) & vbCrLf
s &= dtInfo(2) & ", " & dtInfo(3) & vbCrLf
s &= dtInfo(5) & vbCrLf
s &= "================================" & vbCrLf
s &= "   GATE   OPERATOR    " & vbCrLf
s &= "   " & sess_gate & "   " & sess_fullname & " - " & sess_username & vbCrLf
s &= "IN  : " & dtTable(6) & vbCrLf
s &= "    " & dtTable(0) & vbCrLf

's &= FontStyle.Bold
s &= sess_PRINT_BOLD_ON

s &= "    " & dtTable(1) & vbCrLf
s &= "    Rp. " & Format(total, "#,##0.00") & vbCrLf

s &= sess_PRINT_BOLD_OFF

s &= "OUT : " & getnow & vbCrLf
s &= "    " & dtTable(12) & vbCrLf
s &= "    " & dtTable(2) & vbCrLf
s &= "================================" & vbCrLf
s &= "Terima Kasih Atas Kunjungan Anda" & vbCrLf
s &= "" & vbCrLf
s &= "" & vbCrLf
s &= "" & vbCrLf
s &= "" & vbCrLf
s &= "" & vbCrLf
s &= "" & vbCrLf
s &= "" & vbCrLf
s &= "" & vbCrLf
s &= "_" & vbCrLf

About berbagisolusi

Berbagi merupakan sebuah bentuk simbol keikhlasan untuk membantu dan menolong, sedangkan solusi adalah cara menyelesaikan masalah. Setiap manusia pasti mengalami masalah, tetapi kita tidak perlu mengalami masalah yang sama jika orang lain pernah mengalami dan kita tahu hal tersebut.

Discussion

11 thoughts on “POS Print in VB.NET ( Raw Print )

  1. hello, this post was helpful but i have a question.

    how do I implement centering?

    i like to center the first line of the print out.

    and next question is, how do I do paper feed and then partial cut?

    thanks!

    Posted by skemb321 | April 17, 2012, 3:07 pm
    • to centering the text, last day i only set manually using space ” ”
      ” ” & text & ” ”
      because only for park ticket, that easly.
      If paper feed cut you must declare text at bottom of ticket like this

      xxxxxx
      xxxxx
      xxxx

      _

      so printer will read “_” in the bottom, and you must setting in printer properties / read Printer manual POS (if you buy new printer, you getting POS manual code)

      Posted by berbagisolusi | April 17, 2012, 3:23 pm
  2. hello, nice post!

    I have 2 questions.

    is it possible to center raw data? I want to center the text printed on the paper

    and last question how do I tell the printer to do a paper feed cut?

    Im using a EPSON TM-T88IVP printer.

    additional info:

    escape sequences doesn’t work —> http://msdn.microsoft.com/en-us/library/microsoft.pointofservice.posprinter%28WinEmbedded.11%29.aspx#Y109

    thanks in advance!

    Posted by skemb321 | April 17, 2012, 3:10 pm
    • to centering the text, last day i only set manually using space ” ”
      ” ” & text & ” ”
      because only for park ticket, that easly.
      If paper feed cut you must declare text at bottom of ticket like this

      xxxxxx
      xxxxx
      xxxx

      _

      so printer will read “_” in the bottom, and you must setting in printer properties / read Printer manual POS (if you buy new printer, you getting POS manual code)

      Posted by berbagisolusi | April 17, 2012, 3:21 pm
      • Hello poster!

        I found the solution of cutting the paper…

        here it is:

        Dim output As String = Chr(&H1D) & “V” & Chr(66) & Chr(0) ‘Feeds paper & cut

        RawPrinterHelper.SendStringToPrinter(pd.PrinterSettings.PrinterName, output)

        WHERE output is found in a printer’s manual;

        btw,
        I used your centering style by just using spaces! it saves me a bunch of time

        THanks a lot!!!

        cheers from philippines🙂

        Posted by skemb321 | April 17, 2012, 4:52 pm
  3. Hi,

    I have problem using this code on WIN7 64.
    When this peace of code execute I got error

    If OpenPrinter(szPrinterName, hPrinter, 0) Then
    ….

    A call to PInvoke function ‘PRMNX01!PRMNX01.RawPrinterHelper::OpenPrinter’ has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.

    Anyone know why

    Miran

    Posted by Miran | June 5, 2012, 3:52 am
  4. hello change the code block of openprinter() to :

    _
    Public Shared Function OpenPrinter(ByVal src As String, ByRef hPrinter As Integer, ByVal pd As Integer) As Boolean
    End Function

    AS you might noticed, the parameter value is now “integer” not “IntPtr”. This might as well solves your problem. Hit back to keep me updated!

    ^__^
    chow!

    Posted by skemb321 | June 5, 2012, 9:37 am
    • I’m sorry before.
      This article only for Print Spooler 32bit
      see reference info at http://support.microsoft.com/kb/322090

      More information
      To send raw data to a printer from the Microsoft .NET Framework, your program must work with Win32 spooler functions. Through the .NET Framework, you can print by using PrintDocument, PrintController, and associated classes. However, with the .NET Framework, you cannot send preformatted printer-ready data to a printer.

      May be you can finding the article in google, end raw data to a printer with spooler 64but

      thank you

      Posted by berbagisolusi | June 5, 2012, 9:46 am
  5. no, im using W7 too!

    probably my above solution would work.

    Posted by skemb321 | June 6, 2012, 7:49 am
  6. Hi
    This is a nice code. I tried it and it works nicely.

    But I have 1 question.

    . I don’t know how to set followings to change font size

    ********************************************************************
    Public sess_ESC As String = ChrW(27) + “!”
    Public sess_EPSON_BOLD_ON As String = sess_ESC + Chr(16) + sess_ESC + Chr(32)
    Public sess_EPSON_BOLD_OFF As String = sess_ESC + Chr(0) + sess_ESC + Chr(0)
    Public sess_STAR_BOLD_ON As String = sess_ESC + Chr(14) + sess_ESC + Chr(15)
    Public sess_STAR_BOLD_OFF As String = sess_ESC + Chr(4) + sess_ESC + Chr(5)

    Public sess_PRINT_BOLD_ON As String = sess_ESC + Chr(17)
    Public sess_PRINT_BOLD_OFF As String = sess_ESC + Chr(0)

    ********************************************************************

    ex:- I want to print Company Name in Large Font
    And then Items in Small Font
    After all “Thank You. Come Again” in Normal Font

    thanks.

    Posted by Dinesh | October 22, 2013, 6:49 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 9 other followers

December 2011
M T W T F S S
« Nov   Jan »
 1234
567891011
12131415161718
19202122232425
262728293031  

Archives

Web Statistic

Blog Stats

  • 149,580 hits
%d bloggers like this: