J4L-One Code for .NET
Copyright J4L Components (http://www.java4less.com) 2006.
Introduction
The J4L.OneCode package contains the classes you need to create One Code (also known as the USPS 4-State Customer Barcode , 4CB or 4-CB) barcodes within your .NET applications (windows and web applications).
One Code barcodes consist of 65 bars. Each bar can be printed in one of four states; full, track, ascender or descender.
![]()
One Code barcodes can encode the following information:
The component allows you to configure the dimensions of the barcode:
Sample Application
In order to run the sample application you must execute OneCodeDemo20.exe, OneCodeDemo11.exe, or OneCodeDemo10.exe.
In the sample application you can set all properties of the OneCode 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.
OneCode class
The main class for creating OneCode barcodes is J4L.OneCode.OneCode. The class OneCodeControl 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 OneCode class:
- BarcodeIdentifier: (see description in Introduction section)
- SpecialServices: (see description in Introduction section)
- CustomerIdentifier: (see description in Introduction section)
- SequenceNumber. (see description in Introduction section)
- RoutingCode. (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 OneCode symbol.
If you need to created a image file you can do it like this:
[C#]
using J4L.OneCode;
...
// define variable
OneCode bc;
// create instance of the objact
bc = new OneCode();
// set barcode properties
bc.BarcodeIdentifier=2;
...
// set size and write to file
bc.Width = 400;
bc.Height = 200;
bc.saveToFile("onecode.gif","GIF");
[VBNET]
Imports J4L.OneCode
......
' define variable
Dim bc as OneCode
'create instance of the object
bc = new OneCode()
' set barcode properties
bc.BarcodeIdentifier=2
...
' set size and write to file
bc.Width = 400
bc.Height = 200
bc.SaveToFile("onecode.gif","GIF")
You can also use the paint() method for rendering the barcode onto an external Graphics object:
[C#]
using J4L.OneCode;
using System.Drawing;...
// create image and graphics
Bitmap inMemoryImage =new Bitmap(300,300) ;
Graphics g= Graphics.FromImage(inMemoryImage) ;
// create barcode
OneCode bc=new OneCode();// set barcode properties
bc.Width = 300;
bc.Height = 300;
bc.BarcodeIdentifier=2;
...
// render barcode on "g"
bc.paint(g);[VBNET]
Imports J4L.OneCode
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 OneCode =new OneCode()' 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.OneCode;
...
// define variable
OneCodeControl bc;
// create instance of the objact
bc = new OneCodeControl();
// 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.BarcodeIdentifier=2;
....
// add it to the form "this" is the current form.
this.Controls.Add(bc);[VBNET]
Imports J4L.OneCode
.....
' define variable
dim bc as OneCodeControl
'create instance of the objact
bc = new OneCodeControl()
'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.BarcodeIdentifier=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);
}OneCode class in ASPX pages
You can use the OneCode 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.OneCode" %>
<%@ OutputCache Duration="100" VaryByParam="none" %>
<%
// define variable
OneCode bc;
// create instance of the object
bc = new OneCode();// set barcode properties
bc.BarcodeIdentifier=2;
...// 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.OneCode" %>
<%@ OutputCache Duration="100" VaryByParam="none" %>
<%
' define variable
dim bc as OneCode = new OneCode()' set barcode properties
bc.BarcodeIdentifer=2
...' 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 OneCode symbol 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 OneCode symbol:
<HTML>
<HEAD><TITLE>Servlet Example META http-equiv=Content-Type content="text/html; charset=windows-1252">
</HEAD>
<BODY bgColor=#ffffff>This is your OneCode:
<IMG height=100 width=100 src="onecode.aspx" ></BODY>
</HTML>