J4L-4 State Code for .NET

Copyright J4L Components (http://www.java4less.com) 2006.

 

Introduction

The package J4L.CC4State contains the classes you need to create 4 State Code barcodes (note the USPS 4-State Customer Barcode is a different product called OneCode ) within your .NET applications. Supported symbologies include the British Royal Mail customer barcode, dutch KIX barcode and australien customer barcode.

A 4 state barcode barcodes consist of a number of bars where each bar can be printed in one of four states; full, track, ascender or descender.

 

The package supports the following sybmologies:

The component allows you to configure the dimensions of the barcode:

 

 

Sample Application

In order to run the sample application you must execute Demo20.exe, Demo11.exe, or Demo10.exe.

In the sample application you can set all properties of the 4 State customer code symbology.

You can execute the following commands:

CC4StateCode class

The main class for creating 4 State barcodes is J4L.CC4State.CC4StateCode. The class CC4StateCodeControl is a subclass of System.Windows.Forms.Control and can therefore be used for placing a barcode on a windows form.

The following properties allows you to configurate the barcode in CC4StateCode class:

The following method can be used for painting the barcode on an external graphics object:

If you need to created a image file you can do it like this:

[C#]

using J4L.CC4State;

...

// define variable
CC4StateCode bc;
// create instance of the objact
bc = new CC4StateCode();

// set barcode properties
bc.Coder="12345678";

...


// set size and write to file
bc.Width = 400;
bc.Height = 200;
bc.saveToFile("code.gif","GIF");

[VBNET]

Imports J4L.CC4StateCode

......

' define variable
Dim bc as CC4StateCode
'create instance of the object
bc = new CC4StateCode()

' set barcode properties
bc.Code="123456678"
...

' set size and write to file
bc.Width = 400
bc.Height = 200
bc.SaveToFile("code.gif","GIF")

 

You can also use the paint() method for rendering the barcode onto an external Graphics object:

[C#]

using J4L.CC4State;
using System.Drawing;

...

// create image and graphics
Bitmap inMemoryImage =new Bitmap(300,300) ;
Graphics g= Graphics.FromImage(inMemoryImage) ;

// create barcode
CC4StateCode bc=new CC4StateCode();

// set barcode properties
bc.Width = 300;
bc.Height = 300;
bc.BarcodeIdentifier=2;
...

// render barcode on "g"
bc.paint(g);

[VBNET]

Imports J4L.CC4State
Imports System.Drawing

..............

' create image and graphics
dim inMemoryImage as new Bitmap(300,300)
dim g as Graphics = Graphics.FromImage(inMemoryImage)

'create barcode
dim bc as CC4StateCode =new CC4StateCode()

' set barcode properties
bc.Width = 300
bc.Height = 300
bc.BarcodeIdentifier=2
...

'render barcode on "g"
bc.paint(g)

The windows forms control can be placed on a form with your IDE by just adding our controls to the toolbox. You can also programatically add controls to your form with the following code:

[C#]

using J4L.CC4State;

...

// define variable
CC4StateCodeControl bc;
// create instance of the objact
bc = new CC4StateCodeControl();
// define position and size of the object on the form
bc.Location = new System.Drawing.Point(8, 8);
bc.Size = new System.Drawing.Size(368, 176);

// set barcode properties
bc.Code="2";
....

// add it to the form "this" is the current form.
this.Controls.Add(bc);

[VBNET]

Imports J4L.CC4State

.....

' define variable
dim bc as CC4StateCodeControl
'create instance of the objact
bc = new CC4StateCodeControl()
'define position and size of the object on the form
bc.Location = new System.Drawing.Point(8, 8)
bc.Size = new System.Drawing.Size(368, 176)

' set barcode properties
bc.Code="2";
...

'add it to the form "me" is the current form.
me.Controls.Add(bc)

You can print the barcode by rendering in on the Graphics objects of the PrintDocument:

[C#]

void btnPrintClick(object sender, System.EventArgs e)
{
// create PrintDocument and set handler
PrintDocument printDocument1=new PrintDocument();
printDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.printDocument1_PrintPage);
printDocument1.Print();
}

private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
// print barcode here
bc.getBarcodeObject().paint(e.Graphics);
}

CC4StateCode class in ASPX pages

You can use the CC4StateCode class in a aspx page to create an image on the fly. Your html page should contain a tag like this:

    <img SRC=barcode.aspx ALT=Barcode BORDER=0>

    which defines a image that must be read from barcode.aspx. The barcode.aspx page must then generate the barcode image in the following way:

    [C#]

    <%@ Page language="c#" AutoEventWireup="false" Trace="false" Debug="false" %>
    <%@Import Namespace="System.Drawing" %>
    <%@Import Namespace="System.IO" %>
    <%@Import Namespace="System.Drawing.Imaging" %>
    <%@Import Namespace="J4L.CC4State" %>
    <%@ OutputCache Duration="100" VaryByParam="none" %>
    <%
    // define variable
    CC4StateCode bc;
    // create instance of the object
    bc = new CC4StateCode();

    // set barcode properties
    bc.Code="1234567890";
    ...

    // create in memory image
    Bitmap inMemoryImage = new Bitmap( 200,200);
    Graphics g = Graphics.FromImage(inMemoryImage);
    // paint barcode
    bc.Width=200;
    bc.Height=200;

    bc.paint(g);

    MemoryStream tempStream = new MemoryStream();

    // output image to the memory stream in gif format
    inMemoryImage.Save(tempStream,ImageFormat.Gif);

    Response.ClearContent();
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.ContentType = "image/gif";
    Response.BinaryWrite(tempStream.ToArray());
    Response.End();
    %>

    [VBNET]

    <%@ Page language="VB" AutoEventWireup="false" Trace="false" Debug="false" %>
    <%@Import Namespace="System.Drawing" %>
    <%@Import Namespace="System.IO" %>
    <%@Import Namespace="System.Drawing.Imaging" %>
    <%@Import Namespace="J4L.CC4State" %>
    <%@ OutputCache Duration="100" VaryByParam="none" %>
    <%

    ' define variable
    dim bc as CC4StateCode = newCC4StateCode()

    ' set barcode properties
    bc.Code="1234567890"
    ...

    ' create in memory image
    dim inMemoryImage as Bitmap= new Bitmap( 200,200)
    dim g as Graphics = Graphics.FromImage(inMemoryImage)
    ' paint barcode
    bc.Width=300;
    bc.Height=300;

    bc.paint(g)

    dim tempStream as MemoryStream = new MemoryStream()

    ' output image to the memory stream in gif format
    inMemoryImage.Save(tempStream,ImageFormat.Gif)

    Response.ClearContent()
    Response.Cache.SetCacheability(HttpCacheability.NoCache)
    Response.ContentType = "image/gif"
    Response.BinaryWrite(tempStream.ToArray())
    Response.End()
    %>

 

Important Note:

In order to properly print a barcodel embedded in a HTML page you must use the <IMG> tag. Note that you will need to use the attibutes height and width in order to achieve the correct size of the symbol on the printed page.

This is a simple HTML page that contains a 4 State Code symbol:

<HTML>
<HEAD>

<TITLE>Servlet Example META http-equiv=Content-Type content="text/html; charset=windows-1252">

</HEAD>
<BODY bgColor=#ffffff>

This is your Barccode:
<IMG height=100 width=100 src="barcode.aspx" >

</BODY>
</HTML>