How to Print BarCode images on BIRT Reports

Skip to end of metadata
Go to start of metadata
In this tutorial, we will show you how to print barcode images on BIRT reports. We will use Eclipse BIRT Report Designer plug-in to design the report visually and then later on use BIRT runtime SDK to modify the report dynamically and insert barcode images in the report. Finally, the report will be exported to PDF format

We need the following tools, SDKs etc in order to complete this tutorial:

  1. Eclipse SDK
  2. BIRT Report Designer plug-in (along with required dependencies)
  3. BIRT Runtime (can be downloaded from http://download.eclipse.org/birt/downloads/)
  4. JDK 1.5 or higher
  5. Aspose.BarCode for Java
Create New Project

To design the report, open Eclipse SDK, if it’s not opened already. From the menu, choose File -> New -> Project. Open the Business Intelligence and Reporting Tools node and select Report Project as shown in the figure below:


Give any name for your project and finish the New Project Wizard.

Add New Report to the Project

Next step is to add a new Report to this project. Open the Navigator, right click on the Report Project that we just created. From the context menu, select New -> Report. Name the report as “customers.rptdesign” and click on “Next”. From the report templates, select “My First Report” and click on Finish to exit the report wizard.

Add New Data Source

You will now see layout view of the newly created report. It has 4 columns, but still its en empty report with no data. Open the “Data Explorer” in Eclipse. Right click on “Data Sources” node and select “New Data Source” from the context menu. Choose “Classic Models Inc. Sample Database” from the data source list, click “Next” and then “Finish” to exit the data source wizard.

Add New Data Set

Now, right click on “Data Sets” node in “Data Explorer” and choose “New Data Set” from the context menu. Select the newly created Data Source from the list. In Data Set Type, choose “SQL Select Query” and click “Next”. In the SQL window, type in the query “Select * From CLASSICMODELS.CUSTOMERS”, as shown in the figure below:


Click on “Finish” to exit the wizard. The “Edit Data Set” screen will open after that. Be sure to click on “Preview Results” to verify that the data connection is Ok and you can see some data on the screen.

Preview Report

After that, open the “Data Explorer” window and open the Data Set that we just created. You will see the fields from the “Customers” table. Drag the fields onto the report as shown in the figure below:


Click on the “Preview” tab now. You should see the list of customers on the report.

Add New Column for BarCode

Now, we will add a new column in our report for displaying barcode images. In layout view, right click on table header of the “Phone” column and choose “Insert” -> “Column to the Right” from the context menu. Insert a new label in column header and set its value as “BarCode”.

Add Image

In the detail row, barcode column, add a new image from the “Palette window”. “Edit Image Item” screen will open after that. Select “Dynamic Image” from the list.


Click on the “Select Image Data” button and click on “Add” button from the right. Type “imgBarCode ” in Column Binding Name field, set Display Name as “BarCode” and for the Data Type value, choose “Blob” from the list.


Now click on the “Expression” button and type in the following code in the text area.reportContext.getAppContext().get("imgBarCode");


Click “Ok”. From the “Select Data Binding” window, click on the checkbox for “imgBarCode” column to enable it and click on “Ok”. You will see that row "imgBarCode" will be inserted in the dynamic expression textbox.


Finally, click on “Insert” to add the image to the report.Now preview the report in Eclipse. You will image placeholders in the report preview as shown in the image below:


Save the report.

Insert BarCode Images Dynamically in the BIRT Report

We finished the design portion in the above paragraphs. Now we will use BIRT runtime engine and Aspose.BarCode for Java to generate the barcode images and insert into the newly created report.

Create New .Java File

Create a new Java project in Eclipse and create a new Java file. Add the following code to the java program file.You need to make sure that you refer to the relevant .jar files of Aspose.BarCode for Java and BIRT runtime, to make the program compile successfully.

[Java]
import java.awt.Image;
import java.awt.image.RenderedImage;
import java.io.ByteArrayOutputStream;
import java.util.logging.Level;

import javax.imageio.ImageIO;

import org.eclipse.birt.core.framework.Platform;
import org.eclipse.birt.report.engine.api.EngineConfig;
import org.eclipse.birt.report.engine.api.HTMLRenderOption;
import org.eclipse.birt.report.engine.api.IReportEngine;
import org.eclipse.birt.report.engine.api.IReportEngineFactory;
import org.eclipse.birt.report.engine.api.IReportRunnable;
import org.eclipse.birt.report.engine.api.IRunAndRenderTask;
import org.eclipse.birt.report.engine.api.PDFRenderOption;

import com.aspose.barcode.BarCodeBuilder;
import com.aspose.barcode.Symbology;

IReportEngine engine = null;
try{
	System.out.println("engine configuration......");
	EngineConfig config = new EngineConfig( );
	config.setEngineHome( "C:\\birt-runtime-2_5_0\\ReportEngine" );
	//config.setLogConfig("C:\\birt-runtime-2_5_0\\ReportEngine\\temp", Level.INFO);

	System.out.println("startup platform......");
	Platform.startup( config );
	IReportEngineFactory factory = (IReportEngineFactory) Platform.createFactoryObject( IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY );
	engine = factory.createReportEngine( config );
	//engine.changeLogLevel( Level.WARNING );

	// Run reports, etc.
	System.out.println("Open the report......");
	IReportRunnable design = null;
        //Open the report design
        design = engine.openReportDesign("C:\\customers.rptdesign");
        IRunAndRenderTask task = engine.createRunAndRenderTask(design);

        System.out.println("generate PDF report......");
        PDFRenderOption options = new PDFRenderOption();
        options.setOutputFileName("C:\\test.pdf");
        options.setOutputFormat("pdf");

        System.out.println("place barcode......");
        BarCodeBuilder builder = new BarCodeBuilder();
        builder.setSymbologyType(Symbology.Datamatrix);
        builder.setCodeTextVisible(false);
        builder.setCodeText("test-123");
        Image img = builder.generateBarCodeImage();
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ImageIO.write((RenderedImage)img, "png", out);
        byte[] bytes = out.toByteArray();
        task.getAppContext().put("imgBarCode", bytes);

        System.out.println("close the task......");
        task.setRenderOption(options);
        task.run();
        task.close();

}catch( Exception ex){
	ex.printStackTrace();
}
finally
{
	engine.destroy();
	Platform.shutdown();
	System.out.println("engine destroyed......");
}
 

The report in PDF format will be generated on the specified location after the program is compiled and run successfully. A screenshot of the PDF report is shown in the figure below:

Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.