473,395 Members | 1,931 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Help with printing a barcode

Hi,
I found this code to send print direct to printer. It works perfect.

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

I am trying now to print a barcode on the same page using a control from
http://www.idautomation.com/formscon...rolManual.html. How can I
use it with the above code to send the print to the printer? I tried this
Dim pbox As New PictureBox
Dim NewBarcode As IDAutomation.Windows.Forms.LinearBarCode.Barcode =
New Barcode
NewBarcode.Size = New System.Drawing.Size(148, 64)
NewBarcode.Location = New System.Drawing.Point(176, 7)
NewBarcode.Name = "NewBarcode"
'Me.Controls.AddRange(New System.Windows.Forms.Control() {NewBarcode})
NewBarcode.DataToEncode = "999928829"
NewBarcode.RefreshImage()
pbox.Image = NewBarcode.Picture
and insert it in the above code with

st1 = pbox.toString

PrintDirect.WritePrinter(lhPrinter, st1, st1.Length, pcWritten)

st1 = ChrW(27) + "*p50x400Y"
PrintDirect.WritePrinter(lhPrinter, st1, st1.Length, pcWritten)
It didn't work. Please help.

Thanks

Jul 21 '05 #1
0 2306

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Murray Elliot | last post by:
Just wondering if anyone has any ideas on how to solve a particular problem. A client wants to print barcode labels from their (web/php based application). The barcode labels are very small, so I'm...
4
by: Chris | last post by:
Hi, I found this code that can solve a problem I have. It sends print direct to printer. Imports System Imports System.Text Imports System.Runtime.InteropServices ...
2
by: qumpus | last post by:
My program right now generates USPS style shipping label using System.Drawing.Graphics. It works fine except that the printer prints really slowly. I want to make my program take advantage of true...
5
by: Nathan Bloomfield | last post by:
Does anyone know of any freeware addins that enable printing barcodes in Access 2k3? Regards, Nathan
1
by: Aaron Bronow | last post by:
I have an asp.net application which loads a Crystal Reports ReportDocument, passes in parameters for selecting data from the report's database connection, renders the report for previewing on the...
2
by: Mika M | last post by:
Hi! I have barcode printer attached to the printing server's COM1-port, and this printer is shared in path like \\MyServer\Intermec I have text-file containing barcode script in file...
3
by: Mika M | last post by:
Hi all! I have made an application for printing simple barcode labels using PrintDocument object, and it's working fine. Barcode printer that I use is attached to the computer, and this...
0
by: Chris | last post by:
Hi, I found this code to send print direct to printer. It works perfect. Imports System Imports System.Text Imports System.Runtime.InteropServices <StructLayout(LayoutKind.Sequential)> _...
15
by: =?Utf-8?B?cGF0cmlja2RyZA==?= | last post by:
Hi everyone! Does anyone know how can I do barcode printing? First of all, I know I have to write a client executable, but how do I communicate with the thermal printer? Using software the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.