<bc:asposebarcode> and <bc:barCodeAttributesExpression> are special tags, used in JasperReport files (jrxml), for rendering barcode images in reports, using Aspose.BarCode for JasperReports library.
These tags can be used in two ways:
Print BarCodes Based on Parameter Values
Below is an example for rendering barcodes based on parameter values:
<componentElement>
<reportElement x="0" y="70" width="400" height="150"/>
<bc:asposebarcode
xmlns:bc="http://jasperreports.sourceforge.net/jasperreports/asposebarcode"
xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/asposebarcode
http://jasperreports.sourceforge.net/xsd/barcode.xsd">
<bc:barCodeAttributesExpression>$P{barCodeAttributes1}</bc:barCodeAttributesExpression>
</bc:asposebarcode>
</componentElement>
$P(barCodeAttributes1) is the text inside <bc:barCodeAttributesExpression> tag, which tells that it expects a parameter whose name is “barCodeAttributes1” and parameter type is “BarCodeAttributes”, which is defined in com.aspose.barcode.jr package. So, we need to define this parameter before using it. This parameter can be defined in jrxml file as below:
<parameter name="barCodeAttributes1" class="com.aspose.barcode.jr.BarCodeAttributes"/>
.jrxml files are the report definition files. We can pass this parameter from our java source file. Sample code for passing a parameter is given below:
Map<String, BarCodeAttributes> parameters = new HashMap<String, BarCodeAttributes>();
parameters.put("barCodeAttributes1", new BarCodeAttributes("123456", "code11"));
In the above java source file, we created an instance of type Map<key, value>. String type is used as key and BarCodeAttributes type is used as value. After declaration, we added one element in the map. The key given is “barCodeAttributes1” and its value is of type “BarCodeAttributes”. Its constructor takes codetext and symbology type as arguments. Both codetext and symbology type are passed as string values.
So, as a result, based on the code snippets above, a barcode image will be displayed on the report whose codetext is “123456” and symbology type is Code11.
Print BarCodes Based on Field Values (Using DataSource)
Below is code snippet from a jrxml file, which renders barcode images from a data source. ID field is used as codetext.
<componentElement>
<reportElement x="0" y="4" width="315" height="150"/>
<bc:asposebarcode
xmlns:bc="http://jasperreports.sourceforge.net/jasperreports/asposebarcode"
xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/asposebarcode
http://jasperreports.sourceforge.net/xsd/barcode.xsd">
<bc:barCodeAttributesExpression>$F{id}</bc:barCodeAttributesExpression>
</bc:asposebarcode>
</componentElement>
$F(id) means that a field whose name is “id” is required here. The type of “id” field should be “BarCodeAttributes”. Following is the definition of “id” field in the jrxml file.
<field name="id" class="com.aspose.barcode.jr.BarCodeAttributes"/>
In our java source file, we need to pass a data source to the jrxml file. We will use a custom class inherited from JRDataSource. And in getFieldValue() method, we need to return instance of type “BarCodeAttributes”, which will be used by <bc> tags in jrxml files for rendering barcodes. For more details about using data source in JasperReports, please click here.