code 39 Barcode not printing out of correctly????

Posted on 2004-09-27
Last Modified: 2011-10-03
We have returns employees who scan in specific barcodes and need other barcodes printed back out to them.  I am programming the export of the new barcodes that they will place on the return boxes.  I am having however problems formatting the barcode to sit well on a 2.25X2.5 label.  We are using a Zebra LP 2844 printer for the printing of the labels.  I haven't messed around much with printer objects in so I am fairly new to the whole thing.  Are there settings in my code that I should change to make the barcode fit??  Here is a sample barcode number: 6103766213SUNDT004425103.  I need that number to print out in the form of a barcode on the 2.25X2.5 label, but I am having all types of problems with the formatting.  Can someone please help me.  You guys are the best.  Here is my code:

Imports System.Drawing.Printing
Imports System.Drawing.Text

Public Class Form1
    Inherits System.Windows.Forms.Form
    Public ptrFont As Font
    Public strBarcode As String
    Public newBarcode As String

#Region " Windows Form Designer generated code "

    Public Sub New()

        'This call is required by the Windows Form Designer.

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
            End If
        End If
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents txtBarcode As System.Windows.Forms.TextBox
    Friend WithEvents lblPrevious As System.Windows.Forms.Label
    Friend WithEvents txtPrevious As System.Windows.Forms.TextBox
    Friend WithEvents btnExit As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.txtBarcode = New System.Windows.Forms.TextBox
        Me.lblPrevious = New System.Windows.Forms.Label
        Me.txtPrevious = New System.Windows.Forms.TextBox
        Me.btnExit = New System.Windows.Forms.Button
        Me.txtBarcode.CharacterCasing = System.Windows.Forms.CharacterCasing.Upper
        Me.txtBarcode.Font = New System.Drawing.Font("Verdana", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.txtBarcode.ForeColor = System.Drawing.Color.FromArgb(CType(255, Byte), CType(128, Byte), CType(0, Byte))
        Me.txtBarcode.Location = New System.Drawing.Point(96, 32)
        Me.txtBarcode.MaxLength = 100
        Me.txtBarcode.Name = "txtBarcode"
        Me.txtBarcode.Size = New System.Drawing.Size(400, 27)
        Me.txtBarcode.TabIndex = 0
        Me.txtBarcode.Text = ""
        Me.lblPrevious.Font = New System.Drawing.Font("Rockwell", 12.0!, CType((System.Drawing.FontStyle.Bold Or System.Drawing.FontStyle.Italic), System.Drawing.FontStyle), System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.lblPrevious.ForeColor = System.Drawing.Color.Red
        Me.lblPrevious.Location = New System.Drawing.Point(56, 152)
        Me.lblPrevious.Name = "lblPrevious"
        Me.lblPrevious.Size = New System.Drawing.Size(160, 32)
        Me.lblPrevious.TabIndex = 1
        Me.lblPrevious.Text = "Previous Serial #:"
        Me.lblPrevious.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        Me.txtPrevious.Font = New System.Drawing.Font("Verdana", 9.75!, CType((System.Drawing.FontStyle.Bold Or System.Drawing.FontStyle.Italic), System.Drawing.FontStyle), System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.txtPrevious.ForeColor = System.Drawing.SystemColors.ControlText
        Me.txtPrevious.Location = New System.Drawing.Point(224, 160)
        Me.txtPrevious.MaxLength = 100
        Me.txtPrevious.Name = "txtPrevious"
        Me.txtPrevious.ReadOnly = True
        Me.txtPrevious.Size = New System.Drawing.Size(272, 23)
        Me.txtPrevious.TabIndex = 2
        Me.txtPrevious.Text = ""
        Me.btnExit.Location = New System.Drawing.Point(512, 208)
        Me.btnExit.Name = "btnExit"
        Me.btnExit.TabIndex = 3
        Me.btnExit.Text = "Exit"
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.BackColor = System.Drawing.Color.FromArgb(CType(192, Byte), CType(192, Byte), CType(255, Byte))
        Me.ClientSize = New System.Drawing.Size(600, 238)
        Me.ForeColor = System.Drawing.SystemColors.ControlText
        Me.Name = "Form1"
        Me.Text = "Newgistics Barcode Application"

    End Sub

#End Region

    Private Sub txtBarcode_KeyDown(ByVal sender As Object, ByVal keyPress As System.Windows.Forms.KeyEventArgs) Handles txtBarcode.KeyDown
        If keyPress.KeyValue = Keys.Enter Then
            Dim ptrLabel As New PrintDocument

            strBarcode = txtBarcode.Text

            txtPrevious.Text = strBarcode
            ptrFont = New Font("IDAutomationHC39M", 12)
            newBarcode = "*" & strBarcode & "*"

            AddHandler ptrLabel.PrintPage, AddressOf Me.ptrLabel_PrintPage

        End If
    End Sub

    Private Sub ptrLabel_PrintPage(ByVal sender As Object, ByVal ptr As PrintPageEventArgs)
        Dim linesPerPage As Single = 0
        Dim yPos As Single = 0
        Dim leftMargin As Single = ptr.MarginBounds.Left
        Dim topMargin As Single = ptr.MarginBounds.Top
        Dim line As String = Nothing

        linesPerPage = ptr.MarginBounds.Height / ptrFont.GetHeight(ptr.Graphics)

        yPos = 100

        line = newBarcode

        ptr.Graphics.DrawString(line, ptrFont, Brushes.Black, leftMargin, yPos, New StringFormat)

    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
    End Sub
End Class
Question by:icarus2256
  • 2
LVL 22

Expert Comment

ID: 12165690
Have you not tried it using the printer's own code sequences for bar code printing?

Get the EPL2 manual here:

Look at the B command:

Syntax Bp1,p2,p3,p4,p5,p6,p7,p8,“DATA”
p1 = Horizontal start position (X) in dots
p2 = Ver ti cal start po si tion (Y) in dots
p3 = Rotation
   0 No rotation
   1 90 degrees
   2 180 degrees
   3 270 degrees
p4 = Bar Code selection (see Table 2-1)
p5 = Narrowbar width in dots. (see Ta ble 2-1).
p6 = Wide bar width in dots.
p7 = Bar code height in dots.
p8 = Print human readable code.
   B=yes or N=no.

Author Comment

ID: 12170525
I am a little confused with the printers own code sequence you suggested using.  Is this something that I must code in or something like textpad and send it to the printer??  Can't I configure print properties inside .net??  If not just help me through the printers code sequence you listed above.  Thanks
LVL 22

Accepted Solution

cookre earned 250 total points
ID: 12171155
I don't have one of those printers to test on, I'm just reading the above linked to manual, but it would appear that, as long as the printer is in AutoSense mode, you can sen EPL2 commands directly to the printer.  For example, sending the string:


will display the Code 39 barcode for 123456-789 according to the parameters:

p1=2 - horizontal dot position 2
p2=3 - vertical dot position 3
p3=1 - rotate 90 degrees
p4=3 - use Code 39
p5=4 - narrow bar width 4
p6=7 - wide bar width 7
p7=50 - bar height 50
p8=B - display human readable code

You'll have to fiddle with the various parameters to get it to fit on the stock you're using.

Also, take a look at:

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This is about my first experience with programming Arduino.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question