Live Chat Icon For mobile
Live Chat Icon

Bold Reports

Add Barcodes to Your Report Using Bold Reports Designer

References blog banner image

In this blog post, we are going to generate and add barcodes in RDL reports. In RDL-standard reporting, we can achieve this using a custom code or custom assembly feature. A custom assembly suits our scenario better, since it allows us to reuse the custom code in multiple reports.

First, let’s generate the custom barcode assembly using BarcodeLib, a third-party, open-source library, and integrate that custom assembly into RDL reports using the custom assembly option.

Create barcode extension assembly

We require the libraries BarcodeLib and System.Drawing as prerequisite for creating our custom assembly.

Note: To learn more about the BarcodeLib library, see this GitHub page.

We are using Visual Studio 2017 to create a custom assembly. Create a C# class library project. Enter the name of your project as BarcodeExtension and select the .NET Framework 4.5.

New Project
New Project

Download the NuGet package BarcodeLib v.1.3.0 and reference the System.Drawing.dll assembly in the project.

Include the namespaces for System.Drawing and BarcodeLib in the CS file and change the class name to BarcodeHelper.

using System.Drawing;
using BarcodeLib;

namespace BarcodeExtension
{
    public class BarcodeHelper
    {
    }
}

In the BarcodeHelper class, add a new method GetBarcode, which receives a string value as parameter. For the corresponding input text, the GetBarcode method generates the barcode and returns it in a byte array format.

Note: For demo purposes, we have generated a CODE39 type of barcode. See this page to learn more about the supported barcode types.

using System.Drawing;
using BarcodeLib;

namespace BarcodeExtension
{
    public class BarcodeHelper
    {
        public byte[] GetBarcode(string text)
        {
            Bitmap b;
            Barcode bar = new Barcode
            {
                Alignment = AlignmentPositions.CENTER, 
                IncludeLabel = true,
                RotateFlipType = RotateFlipType.RotateNoneFlipNone
            };
            b = (Bitmap)bar.Encode(TYPE.CODE39, text, 200, 80);
            byte[] bitmapData = null;
            using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
            {
                b.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
                bitmapData = ms.ToArray();
            }
            return bitmapData;
        }
    }
}

Save and build the project. The assembly (.dll) will be generated in the bin folder of the class library project.

Now, we are going to add a reference for the generated custom assembly to our report and embed the barcode as an image report item.

First, create a new report. Refer to Create Your First RDL Report blog and create a similar report design.

Report Design View
Report Design View

The next step is to add an image report item. First, drag a rectangle report item into the Product ID cell.

Add rectangle report item
Add rectangle report item

Note: The image report item covers the whole table cell if it is added directly. The rectangle report item is used as a container to resize the image report item.

Now, add an image report item to the ProductID cell and resize it.

Add image report item
Add image report item

You can download the report design here.

Add assembly reference

  1. In the properties panel, open report properties.

    Report property panel
    Report property panel
  2. Under the Code category, click Code to open the Code Module Dialog.

    Select Code module from property panel
    Select Code module from property panel
  3. In the References tab, specify the name and version of the custom assembly BarcodeExtension.dll, as seen in the following image. Then click OK.
    Add assembly reference in Code Module
    Add assembly reference in Code Module

    Our custom assembly BarcodeExtension.dll, along with the dependent assemblies BarcodeLib.dll and System.Drawing.dll, must be installed in the global assembly cache or present in the Report Designer’s bin location. Refer to this documentation for installing the assemblies in the global assembly cache.

  4. If the function GetBarcode is a static member, you can skip this class instance creation step. Here, for the non-static member function, we need to add a class instance for the BarcodeHelper class, which is available in the BarcodeExtension assembly. Switch to the Classes tab, add the class name BarcodeExtension.BarcodeHelper and instance name barcodeExt. Click OK.

    Add class and instance in Code Module
    Add class and instance in Code Module

Using custom assembly functions

Select the image report item and open the properties panel. The image properties will be listed in the properties panel.

Image properties
Image properties

Under the Basic Settings category, set the Source to Database.

Select Database
Select Database

Open the expression builder for the Value field.

Set expression for value
Set expression for value

We can access the GetBarcode function in our custom assembly using the previously created class instance barcodeExt.

Enter the following expression in the expression builder and click OK.

=barcodeExt.GetBarcode(Fields!ProductID.Value)

Here, we are going to encode the ProductID field value from the data set into a barcode. So, we pass the product ID value as input for GetBarcode function. The previous expression performs a call to GetBarcode function and returns the barcode as a byte array.

Function call to convert product ID to barcode
Function call to convert product ID to barcode

Preview report

You can preview the report at design time using the built-in Bold Reports Viewer to ensure the report design is as expected. Switch to the preview mode. The barcode, along with its label, is generated for the corresponding product ID.

Product details report preview
Product details report preview

Conclusion

In this blog, we have learned how to embed a barcode into an image report item using the custom assembly references feature. To explore more, go through our sample reports and Bold Reports documentation. You can download the zip folder containing the class library project and the report that we created in this blog.

If you have any questions, please post them in the comments section. You can also contact us by submitting your questions through our contact page or, if you already have an account, you can log in to submit your support question.

Bold Reports now comes with a 15-day free trial with no credit card information required. We welcome you to start a free trial and experience Bold Reports for yourself. Give it a try and let us know what you think!

Stay tuned to our official Twitter, Facebook, LinkedIn, Pinterest, and Instagram pages for announcements about releases.

Leave a Reply