Aspose.BarCode for JasperReports

Generate BarCodes using BarCodeAttributesFactory

“BarCodeAttributesFactory” class is also included in com.aspose.barcode.jr package. This class provides a static overloaded method Create(), which accepts arguments like codetext, symbology type, etc. You can choose the best overloaded method that suites you, for rendering barcodes in your reports. “BarCodeAttributesFactory.Create()” method can be called directly in jrxml file for generating barcodes. It’s used in <image> tag, as shown below:

 

<image hAlign="Center">

    <reportElement x="0" y="100" width="500" height="250" />

    <imageExpression class="net.sf.jasperreports.engine.JRRenderable"><![CDATA[new com.aspose.barcode.jr.BarCodeRenderer(com.aspose.barcode.jr.BarCodeAttributesFactory.Create("codetext","DataMatrix",java.awt.Color.RED))]]></imageExpression>

</image>

 

The above code sample taken from a jrxml file will render a barcode image whose codetext is “codetext”, symbology type is “DataMatrix” and fore color is red.

 

Generating barcodes using BarCodeAttributesFactory.Create() method is feasible in those cases when you only need to pass codetext, symbology type and forecolor as parameters. If you want to change other properties of barcode image e.g. border color, back color, bar height, caption, xDimension, yDimension etc, there is another way to do this using a custom class. You can create a static method in that class and create instance of BarCodeAttributes in this method. You may further call any available setxxx() methods of BarCodeAttributes class to generate the barcode according to your specific requirements.

 

Below is the sample code that is required in the jrxml file, for using the custom class:

 

<image>

  <reportElement x="0" y="600"  width="500" height="250" />               

  <imageExpression class="net.sf.jasperreports.engine.JRRenderable"><![CDATA[new com.aspose.barcode.jr.BarCodeRenderer(MyAttributesFactory.Create("test-123","qr",java.awt.Color.RED, “caption above”, “caption below”))]]></imageExpression>

</image>

 

In the above jrxml code snippet, we used a new class MyAttributesFactory (will be created later). It’s Create() method has 5 arguments.

 

  1. Codetext
  2. Symbology type
  3. Fore color
  4. Caption above the barcode
  5. Caption below the barcode

 

Next, we will create MyAttributesFactory.java file to create our new class. Below is the code for MyAttributesFactory class:

 

import com.aspose.barcode.jr.BarCodeAttributes;

import java.awt.*;

 

/**

* A sample factory class to create BarCodeAttributes instances for reports

*/

public class MyAttributesFactory {

   

    public static BarCodeAttributes Create(String text, String symbology, Color foreColor, String captionAbove, String captionBelow) {

        BarCodeAttributes b = new BarCodeAttributes();

        b.setCodeText(text);

        b.setSymbology(symbology);

        b.setForeColor(foreColor);

        b.setCodeTextVisible(false);

        b.setCaptionAbove(new Caption(captionAbove));

        b.setCaptionBelow(new Caption(captionBelow));

        b.setAutoSize(true);

        return b;

    }

}

 

The Create method in the above class return instance of type BarCodeAttributes. Since, we are creating the instance ourselves, we can call any available method of BarCodeAttributes class to customize our barcode image according to our requirements.