Write AAMVA Driver's License Barcode - Console C#

This tutorial shows how to create a C# Windows Console application that utilizes writing PDF417 AAMVA standard barcodes using the LEADTOOLS SDK.

Overview  
Summary This tutorial covers how to use LEADTOOLS AAMVA Builder SDK technology in a C# Windows Console application.
Completion Time 30 minutes
Visual Studio Project Download tutorial project (4 KB)
Platform Windows Console C# Application
IDE Visual Studio 2017, 2019
Development License Download LEADTOOLS

Required Knowledge

Get familiar with the basic steps of creating a project by reviewing the Add References and Set a License tutorial, before working on the Write AAMVA Driver's License Barcode - Console C# tutorial.

Create the Project and Add LEADTOOLS References

In Visual Studio, create a new C# Windows Console project, and add the following necessary LEADTOOLS references.

If using NuGet references, this tutorial requires the following NuGet package:

If local DLL references are used, the following DLLs are needed. The DLLs are located at <INSTALL_DIR>\LEADTOOLS21\Bin\Dotnet4\x64:

Note

How to properly add LEADTOOLS NuGet and local references is covered in the Add References and Set a License tutorial.

For a complete list of which Codec DLLs are required for specific formats, refer to File Format Support.

Create the AAMVAID Class

Ensure that all of the necessary set license and loading code are added into the Window Console C# application as covered in the Add References and Set a License and the Load and Save Images tutorials. Loading the image is not necessary in this tutorial as it will be generated as a new blank RasterImage to write the barcode on, so comment out the according code.

With the project created, the references added, and the license set, coding can begin.

In Solution Explorer, open Program.cs. Add statements to the using block at the top of Program.cs to become like the following:

C#
// Using block at the top 
using System; 
using System.IO; 
using Leadtools; 
using Leadtools.Barcode; 
using Leadtools.Codecs; 

Add a method named SetAAMVAIDInformation() in the Program class and call the method inside the Main method below the line of code above. Add the below code inside the SetAAMVAIDInformation() method:

C#
static AAMVAID SetAAMVAIDInformation() 
{ 
   const int subFilesCount = 1; 
 
   DateTime birthday = new DateTime(DateTime.Now.Year - 16, DateTime.Now.Month, DateTime.Now.Day); 
 
   string[,] driversLicenseData = new string[,] {{"DDF", "N"},          // First name truncation (N = not truncated)  
                                             {"DDE", "N"},                // Last name truncation (N = not truncated)  
                                             {"DCG", "USA"},              // Country identification  
                                             {"DCF", "NONE"},             // Document discriminator  
                                             {"DAQ", "1234567890"},       // ID Number  
                                             {"DAK", "123456     "},      // Postal code  
                                             {"DAJ", "PA"},               // Address Jurisdiction Code  
                                             {"DAI", "Any Town"},         // Address City  
                                             {"DAG", "123 Main Street"},  // Address Street 1  
                                             {"DAU", "072 in"},           // Height (inches or centimeters)  
                                             {"DAY", "BRO"},              // Eye color  
                                             {"DBC", "1"},                // Sex (Male = 1, Female = 2, 9 = Not specified)  
                                             {"DBB", birthday.ToString("MMddyyyy")},         // Date of Birth  
                                             {"DBD", DateTime.Now.ToString("MMddyyyy")},     // Document Issue Date  
                                             {"DAD", "NONE"},             // Middle Name  
                                             {"DAC", "John"},             // First name  
                                             {"DCS", "Doe"},              // Last name  
                                             {"DBA", DateTime.Now.AddYears(6).ToString("MMddyyyy")},// Expiration date  
                                             {"DCD", "M"},                // Jurisdiction-specific endorsement codes  
                                             {"DCB", "NONE"},             // Jurisdiction-specific restriction codes  
                                             {"DCA", "C"},                // Jurisdiction-specific vehicle class  
                                             {"DDJ", birthday.AddYears(21).ToString("MMddyyyy")},  // Under 21 until  
                                             {"DDI", birthday.AddYears(19).ToString("MMddyyyy")},  // Under 19 until  
                                             {"DDH", birthday.AddYears(18).ToString("MMddyyyy")},  // Under 18 until  
                                             {"DAZ", "BRO"}};             // Hair color  
 
   using (AAMVAIDBuilder builder = new AAMVAIDBuilder()) 
   { 
      builder.SetJurisdiction(AAMVAJurisdiction.NorthCarolina, AAMVAID.LookupIssuerIdentificationNumber(AAMVAJurisdiction.NorthCarolina)); 
      builder.SetVersion(AAMVAVersion.Version2016); 
      builder.SetJurisdictionVersion("00"); 
      builder.SetNumberOfEntries(subFilesCount); 
 
      builder.SetSubfileType(subFilesCount - 1, AAMVASubfileType.DL, "DL"); 
 
      for (int i = 0; i < driversLicenseData.GetLength(0); i++) 
         builder.AddDataElementToSubfile(0, driversLicenseData[i, 0], driversLicenseData[i, 1]); 
 
      return builder.Build(); 
   } 
} 

Add the AAMVA PDF417 Write Barcode Code

Once the above code has been added, create a new method WriteAAMVABarcode(RasterImage image, AAMVAID driversLicenseID). Add the below code inside the WriteAAMVABarcode(RasterImage image, AAMVAID driversLicenseID) method to write the barcode to a RasterImage.

C#
static void WriteAAMVABarcode(RasterImage image, AAMVAID driversLicenseID) 
{ 
   BarcodeEngine bcEngine = new BarcodeEngine(); 
 
   PDF417BarcodeData data = new PDF417BarcodeData 
   { 
      Symbology = BarcodeSymbology.PDF417 
   }; 
   data.SetData(driversLicenseID.GetBytes()); 
 
   PDF417BarcodeWriteOptions pdf417WriteOptions = (PDF417BarcodeWriteOptions)bcEngine.Writer.GetDefaultOptions(BarcodeSymbology.PDF417); 
   pdf417WriteOptions.XModule = 15;  
   pdf417WriteOptions.XModuleAspectRatio = 3; 
   pdf417WriteOptions.ECCLevel = PDF417BarcodeECCLevel.Level5; 
   pdf417WriteOptions.SymbolWidthAspectRatio = 4; 
 
   bcEngine.Writer.CalculateBarcodeDataBounds(LeadRect.Empty, image.XResolution, image.YResolution, data, pdf417WriteOptions); 
 
   bcEngine.Writer.WriteBarcode(image, data, pdf417WriteOptions); 
} 

Add the SaveImage method to save the resultant image to disk:

C#
static void SaveImage(RasterImage image, string outputFilename) 
{ 
    using (RasterCodecs codecs = new RasterCodecs()) 
    codecs.Save(image, outputFilename, RasterImageFormat.Png, 0); 
} 

Once the three methods are added, call them from the Main method using the following code:

C#
static void Main(string[] args) 
{ 
   SetLicense(); 
 
   using (RasterImage image = RasterImage.Create(2550, 3300, 24, 300, RasterColor.White)) 
   { 
      AAMVAID driversLicenseID = SetAAMVAIDInformation(); 
      WriteAAMVABarcode(image, driversLicenseID); 
      SaveImage(image, @"C:\LEADTOOLS21\Resources\Images\AAMVAPDF417Barcode.png"); 
      Console.WriteLine("AAMVAID written and saved to file!"); 
   } 
   Console.WriteLine("Press any key to exit..."); 
   Console.ReadKey(); 
} 

Run the Project

Run the project by pressing F5, or by selecting Debug -> Start Debugging.

If the steps were followed correctly, the application runs and a PDF417 barcode will be written on the blank RasterImage containing the specified AAMVA data and will be saved to a file. The output result should look like:

Output barcode image

Wrap-Up

This tutorial showed how to use the BarcodeWriter, AAMVAID and AAMVAIDBuilder classes.

See Also

Help Version 21.0.2020.9.15
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2020 LEAD Technologies, Inc. All Rights Reserved.