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:
- British Royal Mail 4 State Customer Barcode. Encodes delivery mail code (post code and delivery point) made of digits and upper case characters.
- Dutch KIX barcode. Modified version of the Royal Mail 4 State Barcode without start/stop and checksum bars. The value to be encoded has the following format (from left to right):
- post code: 4 digits and 2 letters ( total of 6 characters fixed length ).
- house, post box or answer number: maximum of 5 digits.
- separation character "X" (only required if the additional house number is present).
- additional house number , maximum 6 characters variable length.
- Australian 4-State customer barcode . It encodes the following fields:
- Format code. Can be "11" (Standard customer barcode), "45" (Reply Paid Barcode) , "59" (Customer barcode 2) and "62" (Customer barcode 3).
- Sorting code. This is a 8 digit string which represents a delivery point identifier (DPID). A DPID is a point to which Australia Post delivers the mail.
- Customer information. Available only for formats 59 and 62. It enables customers to include their own information in the barcode.
- Customer information table. Whether table N (only digits) or C (alphanumeric characters) should be used for encoding the customer information.
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:
- Refresh: repaint the symbol using the new properties.
- Print: send image to printer.
- Save: save the symbol in gif format.
- Exit: terminate application.
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:
- Symbology: Can be CC4StateCode.SYMBOL_RM4SCC (British Royal Mail) , CC4StateCode.SYMBOL_KIX (Dutch KIX barcode) or CC4StateCode.SYMBOL_AUSTRALIAN (Australian 4-state barcode).
- Code: value to be encoded in RM4SCC amd KIX symbologies
- AustralianFormatCode: (see description in Introduction section)
- AustralianSortingCode. (see description in Introduction section)
- AustralianCustomerInformation. (see description in Introduction section)
- AustralianCustomerInformationTableM: Customer information table can be N or C (see description in Introduction section)
- BarWidth: (see description in Introduction section)
- BarSpacing: (see description in Introduction section)
- BarHeight: (see description in Introduction section)
- ExtendedBarHeight: (see description in Introduction section)
- QuiteZone: (see description in Introduction section)
- UserTextTop: (see description in Introduction section)
- UserTextBottom: (see description in Introduction section)
- TextFont: font of user text above and below the barcode.
- FontColor: color of user text above and below the barcode.
- BarBackColor: background color.
- BarForeColor: foreground color (color of the bars).
- LeftMargin: left margin in pixels.
- TopMargin: top margin in pixels
- Width:size of the image.
- Height: size of the image.
The following method can be used for painting the barcode on an external graphics object:
- public void paint(Graphics g): paints the 4 State Code symbol.
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>