How to create and print Avery address labels with USPS OneCode 4-State Customer Barco

Neodynamic

Well-known member
Joined
Dec 5, 2005
Messages
137
Programming Experience
10+
How to create and print Avery address labels with USPS OneCode 4-State Customer Barcode (4-CB) in Crystal Reports for ASP.NET

Technologies used
- Neodynamic Barcode Professional 3.0 (or greater) for ASP.NET (WebControl)
- Crystal Reports for .NET (any version)
- Microsoft .NET Framework (any version)
- Microsoft Visual Studio .NET (any version)

In the following Step-By-Step Guide we're going to create an ASP.NET Web Application which includes a CR report for Avery address labels including USPS OneCode 4-State Customer Barcode (4-CB) images generated by Barcode Professional.
For simplicity, we're going to use an XML file as the Data Source for our CR report. This XML file, available for downloading at the end of this guide, includes information of employees generated from Microsoft's AdventureWorks database sample.
Note: This guide was made using Visual Studio 2005 but you can use any other version of it needing in some cases minor changes.

USPS OneCode 4-State Customer Barcode (4-CB)
USPS OneCode 4-State Customer Barcode (4-CB) is the next generation in the evolution of USPS barcode technology. The OneCode Barcode Data Fields is divided in two main fields, the Tracking Code and the Routing Code. The 20-digit tracking code consists of the barcode identifier (2 digits), special services (3 digits), customer identifier (6 digits), and sequence number (9 digits) fields, while the routing code consists of the delivery point ZIP Code which can be 0, 5, 9 or 11 digits long.
In this sample demo we will use a fictitious Tracking Code, for instance, 00123123456123456789. For a real world usage of this sample, you should contact USPS Customer Service in order they can assist you on getting on a valid Tracking Code for your specific case.
The Routing Code will be specified by using the ZIP Code stored in the XML file that we are going to use as our report's data source.

Please follow these steps:
- Open Microsoft Visual Studio .NET and create a new ASP.NET Web Application or Website.
- IMPORTE NOTE:

This Guide uses an XML Schema (XSD files) as data source for the report. Please note that this XML Schema will only be used to design the report in the Crystal Reports Designer.
You can easily generate an XML Schema using an ADO.NET DataSet object. This object has the WriteXmlSchema() method that creates an XML Schema file for you.
So, if you need to create a report for a Table in a SQL Server Database, you can create a DataSet object for that Table and then call the WriteXmlSchema() method to get the XML Schema file associated to it. However, in this sample demo we're going to use an XML file instead of a SQL Server Database for simplicity only.

REMEMBER that this is required to DESIGN the reports. Once created the report you won't need it at RUNTIME when you deploy your application.

For sample demos using Typed DataSet or SQL Stored Procedures as Data Sources please refer to the following Guides instead:
+ How to create Crystal Reports featuring barcode images using Typed DataSet in ASP.NET
+ How to create Crystal Reports featuring barcode images using SQL Stored Procedure in ASP.NET

- Add to the project the XML file called AdventureWorksEmployees.xml (you'll find it at the end of this guide ready for downloading)
- With this file opened, go to the XML menu and click on Create Schema.

figure1Small.gif


VS.NET will create an XML Schema file called AdventureWorksEmployees.xsd. You'll find it in the Solution Explorer.

Remember that this file only will be use to design the report.

- Once we have the XML Schema file, we'll need to modify it and add a NEW element that will represent our barcode images. To do this, double-click on the AdventureWorksEmployees.xsd and add a new element whose name will be BarcodeImage and whose type will be base64Binary as is shown in the following figure:

figure2Small.gif


After that, save the XML Schema file!

- Now it's time to create our CR report.
+ Right-click on the project and go to Add/Add New Item.
+ Select Crystal Report in the list and name it as AveryMailLabels.rpt
+ In the Crystal Reports Gallery dialog box, select Mail Label and click on OK button as is shown in the following figure:

figure3.gif


+ The next step is to set the Data Source for the report. On the Wizard dialog box, click on Create New Connection > ADO.NET. In that moment a dialog box will ask you for a file. Specify the AdventureWorksEmployees.xsd file in the XML File Path textbox.

figure4Small.gif


figure5Small.gif


+ Choose the fields to display on the report as is shown in the following figure:

figure6Small.gif


+ Now, choose the Avery label type. In this case we will choose "Address (Avery 5162)".

figure7Small.gif


Click on Finish button to close the Wizard.

For more information about all Avery labels provided by Crystal Report as well as other useful data associated to each of them, see the following table:

Avery Labels Type--------------------Labels Size---------Labels per Sheet
Return Address (Avery 5267)----------1/2" x 1-3/4"-------80
Address (Avery 5160)-----------------1" x 2-5/8"---------30
Address (Avery 5260)-----------------1" X 2-5/8"---------30
Address (Avery 5161)-----------------1" X 4"-------------20
Address (Avery 5261)-----------------1" X 4"-------------20
Address (Avery 5162)-----------------1-1/3" X 4"---------14
Address (Avery 5262)-----------------1-1/3" X 4"---------14
Address/Shipping (Avery 5163)--------2" X 4"-------------10
Address/Shipping (Avery 5164)--------3-1/3" X 4"---------6
Clear Address (Avery 5660)-----------1" X 2-3/4"---------30
Clear Address (Avery 5662)-----------1-1/3" X 4-1/8"-----14
Clear Address (Avery 5663)-----------2" X 4-1/8"---------10
Full Sheet (Avery 5165)--------------8-1/2" X 11"--------1
File Folder (Avery 5266)-------------2/3" X 3-7/16"------30
�3-1/2� Diskette� (Avery 5196)---------2-3/4" X 2-3/4"-----9
�3-1/2� Diskette - Red� (Avery 5096)---
�3-1/2� Diskette - Blue� (Avery 5896)--
5-1/4 Diskette (Avery 5197)---------- -------------12
5-1/4 Diskette - Red (Avery 5097)----
5-1/4 Diskette - Blue (Avery 5897)---
Audio Cassette (Avery 5198)----------1-5/8" X 3-1/2"-----12
Video Cassette Face (Avery 5199)-----
Video Cassette Spine-----------------
Name Badge - Red (Avery 5095)--------2-1/3" X 3-3/8"-----8
Name Badge (Avery 5395)--------------2-1/3" X 3-3/8"-----8
Name Badge - Blue (Avery 5895)-------2-1/3" X 3-3/8"-----8
Name Tag (Avery 5383)----------------
Name Tag - Blue (Avery 5883)---------
Name Tag (Avery 5384)----------------3" X 4"-------------6
Rotary Index Card (Avery 5385)-------2-1/6" X 4"---------8
Rotary Index Card (Avery 5386)-------3" X 5"-------------4
Index Card (Avery 5388)
Index/Post Card (Avery 5389)---------4" X 6"-------------2
Round (Avery 5294)-------------------2-1/2" Diameter-----12


The Mail Label Wizard configures the report according to the Avery Label you have selected.

figure8Small.gif


- Now, it's time to design the report. Drag & drop the fields as is show in the following figures:

figure9Small.gif


Change the fields and groups so the report looks like the following figure:

figure10Small.gif


IMPORTANT: right-click on the BarcodeImage Blod field, select Format and check Can Grow!

figure11Small.gif


Click OK. Save the report and close it.

- Open your WebForm Default.aspx at design time and drag & drop a CrystalReportViewer control onto it. Set its DiplayGroupTree property to False.

- Open the Solution Explorer window and add a reference to the Neodynamic.WebControls.BarcodeProfessional.dll assembly. Were going to use Barcode Professional as a component library.

Please, see the following FAQ:
How to include the license information in an Application that uses a Neodynamic Product

- In the code-behind class of the WebForm, add the following namespace reference:

Visual Basic .NET
Imports Neodynamic.WebControls.BarcodeProfessional

Visual C# .NET
using Neodynamic.WebControls.BarcodeProfessional;

- Finally, write the following code in the Page_Load event procedure:

Visual Basic .NET
'Create a DataSet from the XML file
Dim xmlFile As String = Server.MapPath("AdventureWorksEmployees.xml")
Dim ds As New DataSet
ds.ReadXml(xmlFile)
'The XML File with the data DOES NOT include
'the BarcodeImage "column" or element we've included
'in the XML Schema file.
'So, create the BarcodeImage column
'WARNING: you must name it as you did in the XML Schema
Dim dc As DataColumn = New DataColumn("BarcodeImage", GetType(Byte()))
'Add the BarcodeImage column to the DataTable
ds.Tables(0).Columns.Add(dc)
'Create a BarcodeProfessional object to fill
'the BarcodeImage column
Dim bc As BarcodeProfessional = New BarcodeProfessional
'Use USPS OneCode 4-State Customer Barcode Symbology
bc.Symbology = Symbology.UspsOneCode4CB
'Barcode Dimension Settings (specified in inches)
bc.Postal4StateTrackerBarWidth = 0.02 'a value ranging from 0.015 to 0.025
bc.Postal4StateTrackerBarHeight = 0.06 'a value ranging from 0.04 to 0.08
bc.Postal4StateBarsSpacing = 0.02 'a value ranging from 0.012 to 0.040
bc.QuietZoneWidth = 0
bc.DisplayCode = False
bc.Text = ""

'USPS OneCode tracking code
Dim trackingCode As String = "00123123456123456789" 'Fictitious (See comments at the beginning of this guide)

'Now, generate and fill barcode images
For Each dr As DataRow In ds.Tables(0).Rows
'Compose USPS OneCode: tracking code + PostalCode column
bc.Code = trackingCode + CType(dr("PostalCode"), String)
dr("BarcodeImage") = bc.GetBarcodeImage(System.Drawing.Imaging.ImageFormat.Png)
Next
'Create a AveryMailLabels report object
'and set its data source with the DataSet
Dim report As CrystalDecisions.CrystalReports.Engine.ReportDocument
report = New CrystalDecisions.CrystalReports.Engine.ReportDocument()
Dim rptFile As String = Server.MapPath("AveryMailLabels.rpt")
report.Load(rptFile)
report.SetDataSource(ds)

If Not Me.IsPostBack Then
'Export report to PDF format into C:\Temp folder
'NOTE: Give write permission to ASPNET account to Temp folder
Dim DiskOptions As CrystalDecisions.Shared.DiskFileDestinationOptions = New CrystalDecisions.Shared.DiskFileDestinationOptions
DiskOptions.DiskFileName = "C:\\TEMP\\AveryMailLabelsWithBarcodeImages.pdf"
report.ExportOptions.DestinationOptions = DiskOptions
report.ExportOptions.ExportDestinationType = CrystalDecisions.Shared.ExportDestinationType.DiskFile
report.ExportOptions.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.PortableDocFormat
report.Export()
End If

'Show it!
CrystalReportViewer1.ReportSource = report

Visual C# .NET
//Create a DataSet from the XML file
string xmlFile = Server.MapPath("AdventureWorksEmployees.xml");
DataSet ds = new DataSet();
ds.ReadXml(xmlFile);
//The XML File with the data DOES NOT include
//the BarcodeImage "column" or element we've included
//in the XML Schema file.
//So, create the BarcodeImage column
//WARNING: you must name it as you did in the XML Schema
DataColumn dc = new DataColumn("BarcodeImage", typeof(byte[]));
//Add the BarcodeImage column to the DataTable
ds.Tables[0].Columns.Add(dc);
//Create a BarcodeProfessional object to fill
//the BarcodeImage column
BarcodeProfessional bc = new BarcodeProfessional();
//Use USPS OneCode 4-State Customer Barcode Symbology
bc.Symbology = Symbology.UspsOneCode4CB;
//Barcode Dimension Settings (specified in inches)
bc.Postal4StateTrackerBarWidth = 0.02f; //a value ranging from 0.015 to 0.025
bc.Postal4StateTrackerBarHeight = 0.06f; //a value ranging from 0.04 to 0.08
bc.Postal4StateBarsSpacing = 0.02f; //a value ranging from 0.012 to 0.040
bc.QuietZoneWidth = 0;
bc.DisplayCode = false;
bc.Text = "";

//USPS OneCode tracking code
string trackingCode = "00123123456123456789"; //Fictitious (See comments at the beginning of this guide)

//Now, generate and fill barcode images
foreach (DataRow dr in ds.Tables[0].Rows)
{
//Compose USPS OneCode: tracking code + PostalCode column
bc.Code = trackingCode + (string)dr["PostalCode"];
dr["BarcodeImage"] = bc.GetBarcodeImage(System.Drawing.Imaging.ImageFormat.Png);
}
//Create a AveryMailLabels report object
//and set its data source with the DataSet
CrystalDecisions.CrystalReports.Engine.ReportDocument report;
report = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
string rptFile = Server.MapPath("AveryMailLabels.rpt");
report.Load(rptFile);
report.SetDataSource(ds);

if (!this.IsPostBack)
{
//Export report to PDF format into C:\Temp folder
//NOTE: Give write permission to ASPNET account to Temp folder
CrystalDecisions.Shared.DiskFileDestinationOptions DiskOptions = new CrystalDecisions.Shared.DiskFileDestinationOptions();
DiskOptions.DiskFileName = "C:\\TEMP\\AveryMailLabelsWithBarcodeImages.pdf";
report.ExportOptions.DestinationOptions = DiskOptions;
report.ExportOptions.ExportDestinationType = CrystalDecisions.Shared.ExportDestinationType.DiskFile;
report.ExportOptions.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.PortableDocFormat;
report.Export();
}
//Show it!
CrystalReportViewer1.ReportSource = report;

- That's it. Build the Windows Application and run it.

figure12Small.gif


If you export the report to PDF format, MS Word Doc or MS Excel (xls), then the USPS OneCode barcodes are maintained!

figure13Small.gif


figure14Small.gif


figure15Small.gif


Sample Files Download
AdventureWorksEmployees.xml (45 KB)
AdventureWorksEmployees.xsd (1 KB)<


Links:
This Demo
More Demos
Download Barcode Professional for ASP.NET
More Information about Neodynamic Barcode Professional for ASP.NET


Neodynamic
.NET Components & Controls
Neodynamic
Neodynamic Barcode Professional for ASP.NET
 
Back
Top