Create DataMatrix barcode in Crystal Reports with a User Function Library (UFL)
This tutorial illustrates the use of a User Function Library for Crystal Reports with a True Type Font (DataMatrix Barcode Font), provided in ConnectCode DataMatrix package, to create an ISO/IEC 16022:2006 standard-compliant DataMatrix barcode in Crystal Reports. A User Function Library is a dynamic link library that enables Crystal Reports to add customized functions to Formula Workshop.
Prerequisites
Crystal Reports 2016
- Crystal Reports 2016 (the UFL works on earlier versions as well)
- ConnectCode DataMatrix package is installed
- CRUFL_DataMatrixBarcode.dll in the Resource\CrystalReportsUFL subdirectory of ConnectCode DataMatrix package.
- x86 Native Tools Command Prompt
- Administrator Rights
Crystal Reports 2020
- Crystal Reports 2020
- ConnectCode DataMatrix package is installed
- CRUFL_DataMatrixBarcode.dll in the Resource\CrystalReportsUFL\x64 subdirectory of ConnectCode DataMatrix package.
- x64 Native Tools Command Prompt
- Administrator Rights
Note – x86 / x64 Native Tools Command Prompt is available in the Desktop Development with C++ Workload of Visual Studio.
Tutorial
1. Launch Windows Explorer and go to the ConnectCode DataMatrix package folder. The QR Code package is installed in "C:\Program Files (x86)\ConnectCodeDataMatrix" folder by default.
Crystal Reports 2016
In the "Resource\CrystalReportsUFL" subfolder, locate the "CRUFL_DataMatrixBarcode.dll" and "crw32.exe.config" files, and copy the files to the Crystal Reports Library folder. The Crystal Reports Library folder is in a folder similar to the one below.
C:\Program Files (x86)\SAP BusinessObjects\SAP BusinessObjects Enterprise XI 4.0\win32_x86
The "crw32.exe.config" file contains the following to enable mixed-mode assembly.
<?xml version ="1.0"?> <configuration> <startup useLegacyV2RuntimeActivationPolicy="true" > <supportedRuntime version="v4.0" /> </startup> </configuration>
Crystal Reports 2020
In the "Resource\CrystalReportsUFL\x64" subfolder, locate the "CRUFL_DataMatrixBarcode.dll" file, and copy the file to the Crystal Reports Library folder. The Crystal Reports Library folder is in a folder similar to the one below.
C:\Program Files (x86)\SAP BusinessObjects\SAP BusinessObjects Enterprise XI 4.0\win64_x64
2. For Crystal Reports 2016, launch the "x86 Native Tools Command Prompt for VS 2017/2019" (the 32-bit prompt is required for loading a UFL to Crystal Reports) as Administrator from the Windows Start Menu.
For Crystal Reports 2020, launch the "x64 Native Tools Command Prompt for VS 2017/2019" (the 64-bit prompt is required for loading a UFL to Crystal Reports) as Administrator from the Windows Start Menu.
3. In the Command Prompt, use the "cd" command to go to the Crystal Reports Library folder.
Crystal Reports 2016
cd C:\Program Files (x86)\SAP BusinessObjects\SAP BusinessObjects Enterprise XI 4.0\win32_x86
Crystal Reports 2020
cd C:\Program Files (x86)\SAP BusinessObjects\SAP BusinessObjects Enterprise XI 4.0\win64_x64
4. Enter the following command in Command Prompt to load the UFL.
gacutil -i CRUFL_DataMatrixBarcode.dll Regasm CRUFL_DataMatrixBarcode.dll
Ensure that the UFL is loaded successfully as shown in the screenshot below.
5. Launch Crystal Report and create a new "Standard Report". Click on the Finish button when prompted to select the data that you want to report on.
6. When the report is created, right click on "Formula Fields" in the "Field Explorer" and select "New" to create a new formula.
7. Name the formula as "datamatrix".
8. In the "Formula Workshop", expand "Functions->Additional Functions->COM and .NET UFLs (u212com.dll)". Check that you see the "ConnectCodeClassDataMatrixEncode" formula. If you do not see this formula, please ensure that you have run steps 2-4 successfully.
9. Double click on the formula, change “Crystal Syntax”to “Basic Syntax” and enter the following VBA programming codes below:
ConnectCodeClassDataMatrixEncode("12345678",0,0) Dim x As Number Dim Result As String For x = 1 To ConnectCodeClassDataMatrixNumBlocks() Result=Result + ConnectCodeClassDataMatrixGetBlocks(x) Next x formula = Result
The formula uses "12345678" as the input data and a second parameter "0" to specify a Square DataMatrix barcode. To generate a Rectangle DataMatrix barcode, set the second parameter as "1". The final parameter "0" specifies a DataMatrix barcode without a Prefix.
First Parameter: Input Data
ASCII - ANSI X3.4 and ISO8859-1
Second Parameter: Shape
0 - Square
1 - Rectangle
0 - None
1 - GS1 FNC1 character
2 - 05 Macro
3 - 06 Macro
4 - Reader Programming
The UFL generates an ECC200 DataMatrix code and uses all the different encodation schemes: ASCII. C40. Text, X12, EDIFACT, and Base 256. After scanning the input data, the UFL automatically switches between different encodation schemes to optimize the number of characters into DataMatrix codewords. The Reed-Solomon algorithm is also applied to generate Error Correction codewords. The size of the DataMatrix barcode is automatically determined and if the number of codes cannot be packed into a DataMatrix barcode, an empty string will be returned.
After the DataMatrix is encoded, a loop is required to return the output in blocks. The reason is because Crystal Reports enforces a 255 characters length limit on the output returned by a UFL formula.
10. When ready, click on the "Save and close" button. In the designer, drag the "datamatrix" formula onto the details section of the report. On the Design tab, select the object created and change the font to "CCodeDataMatrix" (or "CCodeDataMatrix_Trial"). Change the Font Size to 10 to fit the barcode nicely on the report.
11. Click on "View -> Print Preview" to preview the report with the DataMatrix barcode.