Write AAMVA Driver's License Barcode - C# .NET 6

This tutorial shows how to write PDF417 AAMVA standard barcodes in a C# Windows Console application using the LEADTOOLS SDK.

Overview  
Summary This tutorial covers how to use LEADTOOLS AAMVA Builder SDK technology in a C# .NET 6 application.
Completion Time 30 minutes
Visual Studio Project Download tutorial project (2 KB)
Platform C# .NET 6 Console Application
IDE Visual Studio 2022
Runtime Target .NET 6 or higher
Development License Download LEADTOOLS
Try it in another language

Required Knowledge

Get familiar with the basic steps of creating a project and saving images by reviewing the Add References and Set a License and Load and Save Images tutorials, before working on the Write AAMVA Driver's License Barcode tutorial.

Create the Project and Add LEADTOOLS References

Start with a copy of the project created in the Load and Save Images tutorial. If you do not have that project, follow the steps in that tutorial to create it. 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 code accordingly.

The references needed depend upon the purpose of the project.

This tutorial requires the following NuGet package:

Set the License File

The License unlocks the features needed for the project. It must be set before any toolkit function is called. For details, including tutorials for different platforms, refer to Setting a Runtime License.

There are two types of runtime licenses:

Create the AAMVAID Class

With the project created, the references added, the license set, and the save code added, 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 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); 
} 

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

C#
static void Main(string[] args) 
{ 
   InitLEAD(); 
 
   using RasterImage image = RasterImage.Create(2550, 3300, 24, 300, RasterColor.White); 
   AAMVAID driversLicenseID = SetAAMVAIDInformation(); 
   WriteAAMVABarcode(image, driversLicenseID); 
   SaveImage(image, @"C:\LEADTOOLS23\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 23.0.2024.5.22
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2024 LEAD Technologies, Inc. All Rights Reserved.

Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.