A SSRS report with a barcode

Barcode generation in SQL Server Reporting Services

An example how to create barcode in SQL Server reports (SSRS) with StrokeScribe barcode generator. The barcodes shown in this article are GIFs - we do not use barcode fonts.

The SSRS library is free of charge, but you'll need to purchase a license for the barcode generator after testing the demo version.

How to Proceed

NOTE: When upgrading or activating the barcode generator or the SSRS library, you need to stop and then restart the SQL Server Reporting service.

Preparing the Report

1. In the Report Builder, click Insert->Image. Then, click anywhere on the report to place the image.

SSRS ribbon, Insert->Image

2. In the Image Properties dialog, set the image source to Database and the MIME type to image/gif:

Selecting the barcode picture source

3. Click the fx button near the Use this field and paste the following code into the expression builder:

=code.CreateBarcode("123ABCDEF")

Or this, if you want to encode a database field content in the barcode:

=code.CreateBarcode(Fields!A.Value) SSRS expression builder

Press OK and OK.

4. Right-click anywhere on the workspace outside of the report page. Select the Report Properties:

The report's context menu

5. Switch to the References tab.

Adding a reference to the barcode generator assembly

5.1. Click Add at the Add or remove assemblies. Paste the following into the reference field:

StrokeScribeSSRS, Version=1.0.0.0, Culture=neutral, PublicKeyToken=000ec0c517a54332

5.2 Click Add at the Add or remove classes. Paste the following into the Class Name field:

StrokeScribeSSRS.Barcode

5.3. Paste the following into the Instance Name field:

barcode

6. Switch to the Code tab.

Writing a custom code for the report

6.1. Paste the following into the Custom code field:

public rc as long Function CreateBarcode(data as String) as Byte() barcode.Initialize rc = barcode.SetLongProp("Alphabet", 25) ' 25 = QR CODE if rc<>0 then return nothing rc = barcode.SetTextProp("Text", data) if rc<>0 then return nothing dim w as long rc = barcode.GetLongProp("BitmapW", w) ' The min. barcode width in pixels if rc<>0 then return nothing dim arr as byte() rc = barcode.GetPictureArray(1, w*5, w*5, arr) ' A square barcode picture with 5x5px modules if rc<>0 then return nothing return arr end function

7. Run the report.

8. To debug your code, place a Text Box below the barcode image and use this as an expression:

=code.rc

Programming Examples

Here are some examples how to modify the above code to produce barcodes of different types.

All Barcode Types

Encoding ASCII Control Characters in the Barcode

rc = barcode.SetTextProp("Text", "AB" & chr(9) & "CD")

The barcode contains the following string: AB<TAB>CD.

How to Modify the Human-readable Label Below the Barcode

rc = barcode.SetTextProp("TextBelow", "XYZ")

This setting works with CODE128, CODE 39, I2OF5 barcodes.

How to Rotate the Barcode

rc = barcode.SetLongProp("Rotation", 90) dim w as long barcode.GetLongProp("BitmapW", w) dim arr as byte() rc = barcode.GetPictureArray(1, w*2, w*5, arr) if rc<>0 then return nothing return arr

You can rotate the barcode to 90, 180, 270 degrees.

QR CODE in SSRS

QR Code

How to Create a QR Code Readable by Mobile Devices

To make barcodes with non-ASCII characters readable by mobile barcode readers, use the UTF-8 encoding.

The UTF-8 encoding can be enabled by setting the CodePage property to 65001.

public rc as long Function CreateBarcode(data as String) as Byte() barcode.Initialize rc = barcode.SetLongProp("Alphabet", 25) ' 25 = QRCODE if rc<>0 then return nothing rc = barcode.SetLongProp("CodePage", 65001) ' 65001 = UTF8 if rc<>0 then return nothing rc = barcode.SetTextProp("Text", "ΣΤΥΦΧΨΩ") if rc<>0 then return nothing dim w as long rc = barcode.GetLongProp("BitmapW", w) if rc<>0 then return nothing dim arr as byte() rc = barcode.GetPictureArray(1, w*2, w*2, arr) if rc<>0 then return nothing return arr end function

How to Change the QR Code Error Correction Level

Use the QrECL property:

rc = barcode.SetLongProp("QrECL", 3) ' 3 = the highest error correction level DATA MATRIX barcode in SSRS

Data Matrix

How to Create a Data Matrix Barcode

public rc as long Function CreateBarcode(data as String) as Byte() barcode.Initialize rc = barcode.SetLongProp("Alphabet", 8) ' 8 = DATAMATRIX if rc<>0 then return nothing rc = barcode.SetTextProp("Text", data) if rc<>0 then return nothing dim w as long rc = barcode.GetLongProp("BitmapW", w) if rc<>0 then return nothing dim arr as byte() rc = barcode.GetPictureArray(1, w*2, w*2, arr) if rc<>0 then return nothing return arr end function

How to Force the Minimum Matrix Size to a Specific Value

To limit the minimum size of Data Matrix barcodes, use the DataMatrixMinSize property:

rc = barcode.SetLongProp("DataMatrixMinSize", 72)

How to Rotate the Barcode

rc = barcode.SetLongProp("Rotation", 90) GS1 DATA MATRIX barcode in SSRS

GS1 Data Matrix

An example of a GS1 barcode that contains Batch Number (AI 10) and Production Date (AI 11) fields.

Chr(29) is the ASCII <GS> control character that must be placed after a variable-length field.

More GS1 examples are available here.

public rc as long Function CreateBarcode(data as String) as Byte() barcode.Initialize barcode.SetLongProp("Alphabet", 30) ' 30 = GS1DATAMATRIX ' Batch number + GS + Production Date rc = barcode.SetTextProp("Text", "10123ABCD" & chr(29) & "11120201") if rc<>0 then return nothing dim w as long rc = barcode.GetLongProp("BitmapW", w) if rc<>0 then return nothing dim arr as byte() rc = barcode.GetPictureArray(1, w*3, w*3, arr) if rc<>0 then return nothing return arr end function PDF-417 barcode in SSRS

PDF417

How to Create a PDF417 Barcode

public rc as long Function CreateBarcode(data as String) as Byte() barcode.Initialize barcode.SetLongProp("Alphabet", 6) ' 6 = PDF417 rc = barcode.SetTextProp("Text", data) if rc<>0 then return nothing dim w as long rc = barcode.GetLongProp("BitmapW", w) if rc<>0 then return nothing dim h as long rc = barcode.GetLongProp("BitmapH", h) if rc<>0 then return nothing dim arr as byte() rc = barcode.GetPictureArray(1, w*3, h*3, arr) if rc<>0 then return nothing return arr end function

How to Fix the Number of Rows or Columns in PDF417

barcode.SetLongProp("PDF417Cols", 10) barcode.SetLongProp("PDF417Rows", 5)

You don't need to specify both values at the same time - the encoder calculates the second dimension to fit your data. But if you specify both and your data does not fit into the specified size, you will receive an error.

CODE-128 barcode in SSRS

Code 128

public rc as long Function CreateBarcode(data as String) as Byte() barcode.Initialize rc = barcode.SetLongProp("Alphabet", 5) ' 5 = CODE128 if rc<>0 then return nothing rc = barcode.SetTextProp("Text", data) if rc<>0 then return nothing dim w as long rc = barcode.GetLongProp("BitmapW", w) if rc<>0 then return nothing dim arr as byte() rc = barcode.GetPictureArray(1, w*5, w*2, arr) if rc<>0 then return nothing return arr end function