How to create and print Avery address labels with Postnet barcode images in Crystal R
How to create and print Avery address labels with Postnet barcode images in Crystal Reports for ASP.NET - PDF Support
- Neodynamic Barcode Professional 2.5 (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 Crystal Reports web report for Avery address labels including Postnet barcode images generated by Barcode Professional for ASP.NET. In addition, this FAQ states how to export the report to PDF format maintaining the barcode images into it!
Note: This guide was made using Visual Studio 2005 but you can use any other version of it needing in some cases minor changes.
Follow these steps:
1- Open your .NET development tool. We're going to use Microsoft Visual Studio .NET. Create a new ASP.NET Web Application.
2- 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.
3- Add to the project the XML file called AdventureWorksEmplayees.xml (you'll find it at the end of this guide ready for downloading)
4- With this file opened, go to XML menu and click on Create Schema.
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.
5- 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 show in the following figure:
6- Now it's time to create our CR report.
6.1- Right-click on the project and go to Add/Add New Item.
6.2- Select Crystal Report in the list and name it as AveryMailLabels.rpt.
6.3- In the Crystal Reports Gallery dialog box, select Mail Label and click on OK button as is show in the following figure:
7- 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.
10- Open the Webform1.aspx of your project and drag & drop a CrystalReportViewer control onto it. Set the DisplayGroupTree property to False.
11- Open the Solution Explorer window and add a reference to the Neodynamic.WebControls.BarcodeProfessional.dll assembly. We're going to use Barcode Professional for ASP.NET as a component library. Please, see the following FAQ:
Visual C# .NET
13- Finally, write the following code in the Page_Load event procedure:
Visual Basic .NET
'Create a DataSet from the XML file
'REMEMBER to copy the XML file in the application root
Dim xmlFile As String = Server.MapPath("AdventureWorksEmployees.xml")
Dim ds As DataSet = New DataSet
'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
'Create a BarcodeProfessional object to fill
'the BarcodeImage column
Dim bc As BarcodeProfessional = New BarcodeProfessional
'We are going to use Postnet Barcode Symbology
'but you could use other
bc.Symbology = Symbology.Postnet
bc.PostnetHeightShortBar = 0.06F 'Set 5 if you use Barcode Professional 2.5
bc.PostnetHeightTallBar = 0.135F 'Set 12 if you use Barcode Professional 2.5
bc.QuietZoneWidth = 0
bc.AddChecksum = False
bc.DisplayCode = False
bc.BarWidth = 0.02F 'Set 2 if you use Barcode Professional 2.5
bc.BarRatio = 1
bc.Text = ""
For Each dr As DataRow In ds.Tables(0).Rows
'We're going to encode the PostalCode column
bc.Code = CType(dr("PostalCode"), String)
dr("BarcodeImage") = bc.GetBarcodeImage(System.Drawing.Imaging.ImageFormat.Png)
'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")
'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
Visual C# .NET
//Create a DataSet from the XML file
//REMEMBER to copy the XML file in the application root
string xmlFile = Server.MapPath("AdventureWorksEmployees.xml");
DataSet ds = new DataSet();
//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
//Create a BarcodeProfessional object to fill
//the BarcodeImage column
BarcodeProfessional bc = new BarcodeProfessional();
//We are going to use Postnet Barcode Symbology
//but you could use other
bc.Symbology = Symbology.Postnet;
bc.PostnetHeightShortBar = 0.06f; //Set 5 if you use Barcode Professional 2.5
bc.PostnetHeightTallBar = 0.135f; //Set 12 if you use Barcode Professional 2.5
bc.QuietZoneWidth = 0;
bc.AddChecksum = false;
bc.DisplayCode = false;
bc.BarWidth = 0.02f; //Set 2 if you use Barcode Professional 2.5
bc.BarRatio = 1;
bc.Text = "";
//Now, generate and fill barcode images
foreach (DataRow dr in ds.Tables.Rows)
//We're going to encode the PostalCode column
bc.Code = (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
report = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
string rptFile = Server.MapPath("AveryMailLabels.rpt");
//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;