Aspose.BarCode for JasperReports

Print BarCodes in Report

Here is an easy tutorial, which will demonstrate how to use Aspose.BarCode for Jasper Reports to generate barcodes for your reports. Before starting, please check our System Requirements and Installation Instructions.

 

Following is the step by step guide to create a simple demo application.

 

Step 1: Create the .jrxml file

 

.jrxml files contains the report definition for Jasper Reports. These are XML formatted text files. You can either create these manually in your favorite text editor or design them using plugins available for popular IDEs like Netbeans and Eclipse.

 

However, in this tutorial, we will start with a simple .jrxml file that will contain some text and barcode images. Following is the complete jrxml file. You may copy the contents below and create a new file BarCodeReport.jrxml.

 

<?xml version="1.0" encoding="UTF-8"?>

 

<jasperReport

                            xmlns="http://jasperreports.sourceforge.net/jasperreports"

                            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

                            xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd"

                            name="BarcodeReport3" pageWidth="595" pageHeight="1842" columnWidth="515" leftMargin="40" rightMargin="40" topMargin="50" bottomMargin="50">

 

 

  <title>

    <band height="1742">

      <line>

        <reportElement x="0" y="0" width="515" height="1"/>

        <graphicElement/>

      </line>

      <textField>

        <reportElement x="0" y="10" width="515" height="20"/>

        <textElement textAlignment="Center">

          <font size="12"/>

        </textElement>

        <textFieldExpression class="java.lang.String"><![CDATA["This sample uses Aspose.BarCode for Jasper Report\n" + ""]]></textFieldExpression>

      </textField>

      <textField>

        <reportElement x="0" y="50" width="515" height="20"/>

        <textElement textAlignment="Center">

          <font size="12"/>

        </textElement>

        <textFieldExpression class="java.lang.String"><![CDATA["Printing BarCodes\n" + ""]]></textFieldExpression>

      </textField>

 

      <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("code text pdf417","Pdf417",java.awt.Color.BLACK))]]></imageExpression>

      </image>

      <image>

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

        <imageExpression class="net.sf.jasperreports.engine.JRRenderable"><![CDATA[new com.aspose.barcode.jr.BarCodeRenderer(com.aspose.barcode.jr.BarCodeAttributesFactory.Create("code text code 128","Code128",java.awt.Color.BLUE))]]></imageExpression>

      </image>

      <image>

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

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

      </image>

    </band>

  </title>

</jasperReport>

 

<imageExpression> tags are used in the above jrxml file for barcode image rendering. Aspose.BarCode for Jasper Reports has com.aspose.barcode.jr package, which contains BarCodeRenderer class for rendering barcode images in the reports. For rendering the barcode of specific symbology and codetext, BarCodeRenderer class uses BarCodeAttributes class. The BarCodeAttributes class specifies the symbology, codetext and color of the barcode to be generated. For example, in the above jrxml file, the following line will render barcode of type Datamatrix, whole color is blue and codetext is “code text Datamatrix”

 

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

 

Step 2: Create the .java file

 

After we have the .jrxml file ready, the next step is to create the .java file. The .java file will compile the .jrxml file. After compilation, .jasper file will be created. .jasper file is a binary file, understandable by the Jasper Reports. JasperCompileManager.compileReportToFile() method is used to compile the .jrxml file.

 

After the file is compiled, we can export the report to pdf format using net.sf.jasperreports.engine.export.JRPdfExporter class. The complete code of .java file is given below:

 

package barcode1;

 

// import the required packages

import java.io.File;

import net.sf.jasperreports.engine.JREmptyDataSource;

import net.sf.jasperreports.engine.JRExporterParameter;

import net.sf.jasperreports.engine.JasperCompileManager;

import net.sf.jasperreports.engine.JasperFillManager;

import net.sf.jasperreports.engine.JasperPrint;

import net.sf.jasperreports.engine.export.JRPdfExporter;

 

public class Main

{

    public static void main(String[] args)

    {

                            // path of the folder where .jrxml file is present

        final String PATH = "C:\\jasperreports-3.5.2\\demo\\samples\\barcode1\\";

        String jrxmlFileName = PATH + "BarcodeReport.jrxml";

              String fillFileName =PATH + "BarcodeReport.jasper";

 

              try

        {

        // compile the .jrxml file to create .jasper file

                                          JasperCompileManager.compileReportToFile(jrxmlFileName,fillFileName);

              System.out.println(jrxmlFileName + " - File compiled successfully.");

 

              File reportFile = new File(fillFileName);

 

        JasperPrint jasperPrint =

                            JasperFillManager.fillReport(

                                          fillFileName,

                                          null,

                                          new JREmptyDataSource()

                                          );

 

              // export the report in pdf format

        JRPdfExporter exporter = new JRPdfExporter();

        File destFile = new File(reportFile.getParent() , jasperPrint.getName() + ".pdf");

        exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);

              exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString());

              exporter.exportReport();

 

        System.out.println("Report exported to " + destFile.getAbsolutePath() );

              System.out.println("Finished.");

              }

        catch(Exception e)

        {

                            e.printStackTrace();

              }

    }

}

 

Step 3: Compile and Run the .java File to Create the Report

 

Please make sure that you follow the instructions listed here and added the required .jar files. Then, compile and run the .java file using your favorite IDE or java command compiler.

 

Following is the screenshot of the report generated by Jasper Reports, which uses Aspose.BarCode for Jasper Reports for rendering barcode images: