http://www.aspose.com/community/forums/187084/postattachment.aspx

 

Welcome to Aspose.BarCode for JasperReports documentation.

 

Aspose.BarCode for JasperReports provides a unique and robust solution to make your reports unique and more professional. It allows developers to display high quality barcode labels on JasperReports. These barcode labels can be rendered in four formats i.e. Bmp, Jpg, Gif and Png. Aspose.BarCode for JasperReports is a very light weight component that supports more than 25 types of most popular barcode symbologies.

 

To use Aspose.BarCode for JasperReports from your application, copy Aspose.BarCode.JasperReport.jar from the \lib folder of aspose.barcode.jasperreports.zip to the JasperReports\lib directory or to a library folder of your application.

 

Here is an easy tutorial, which will demonstrate how to use Aspose.BarCode for JasperReports to generate barcodes for your reports.

 

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 JasperReports. 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 JasperReports 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 JasperReports. 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 the .java file to create the report

 

Include the required libraries/jar files

 

To successfully compile the above .java file, you need to include the following required .jar files (libraries).

 

·         <JasperReports_Install_Folder>/lib – required .jar files

·         <JasperReports_Install_Folder>/dist/jasperreports-3.5.2.jar (file name may differ according to the version installed on your machine)

·         <Aspose.BarCode for Jasper Report Install Folder>/Aspose.BarCode.JasperReports.jar

 

You also need to copy the following 2 files in the output folder (build/classes) for successful compilation of the above java file:

 

·         <Aspose.BarCode for Jasper Report Install Folder>/asposebarcode_beans.xml

·         <Aspose.BarCode for Jasper Report Install Folder>/jasperreports_extension.properties

 

Note: If you are having any trouble with using Aspose.BarCode for JasperReports, you can always post on the forums here and our dedicated support team will help you.

 

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

 

http://www.aspose.com/documentation/jasperreports-exporters/aspose.barcode-for-jasperreports/QuickStart-Tutorial.001.png

 

If you are using an IDE e.g. Netbeans or Eclipse, you need to modify the project settings to include the jar files listed above. And if you are using javac command for compilation, you need to set the –sourcepath to include the required libraries.

In order to use the license, please save the license file to some folder in your disk e.g. c:\Lic\Aspose.BarCode.JasperReport.lic. Call License.setLicense(filename) method and pass file name as an argument. After, this statement is called, your licensed will be set and no “Aspose.Demo” label will appear on top of the barcode image.

You need to call the setLicense() method only once per process/application.

The following code snippet sets the license for Aspose.BarCode for JasperReports.

// set license

 

License lic = new License();
lic.setLicense("C:\\ Lic\\Aspose.BarCode.JasperReports.lic");