How to Print Barcode Images on BIRT Reports
This tutorial shows how to print barcode images on BIRT reports. It uses Eclipse’s BIRT Report Designer plug-in to design the report visually and then uses the BIRT runtime SDK to modify the report dynamically and insert barcode images. Finally, the report is exported to PDF.
The following tools are needed in order to complete this tutorial:
- Eclipse SDK
- BIRT Report Designer plug-in (along with required dependencies)
- BIRT Runtime (can be downloaded from http://download.eclipse.org/birt/downloads/)
- JDK 1.5 or higher
- Aspose.BarCode for Java
Designing the Report
The first step is to design the report that the barcodes will be displayed in. When the report is prepared, write the code that adds the barcodes dynamically.
Create New Project
To design the report:
- Open Eclipse, if it’s not opened already.
- From the File menu, choose New and then Project.
- Open the Business Intelligence and Reporting Tools node and select Report Project as shown below:
- Give the project a name and finish the New Project Wizard.
Add New Report to the Project
To add a report to the project:
- Open the Navigator, right-click on the Report Project created above.
- From the menu, select New and then Report.
- Call the report customers.rptdesign.
- Click Next.
- From the report templates, select “My First Report” and click on Finish to exit the report wizard.
Add New Data Source
The new report is displayed in the layout view. It has four columns but no data.
- In Eclipse, open the Data Explorer.
- Right-click the Data Sources node and select New Data Source from the 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
To add a data set:
- Click the Data Sets node in the Data Explorer and select New Data Set.
- Select the new Data Source from the list.
- In Data Set Type, choose SQL Select Query.
- Click Next.
- In the SQL window, type in the query Select * From CLASSICMODELS.CUSTOMERS as shown in the figure below:
- Click Finish to exit the wizard. The Edit Data Set screen opens.
- Click Preview Results to verify that the data connection is OK and you can see some data on the screen.
Preview Report
- Open the Data Explorer window.
- Open the new data set. You should see the fields from the Customers table.
- Drag the fields onto the report as shown below.
- Click the Preview tab. You should see a list of customers on the report.
Add New Column for Barcode
Add a new column to the report, where the barcode images will be displayed:
- In layout view, right-click the table header of the Phone column and choose Insert followed by Column to the Right.
- Insert a new label in the column header and set its value to Barcode.
Add Image
- In the detail row, barcode column, add a new image from the Palette window. The Edit Image Item screen opens.
- Select Dynamic Image from the list.
- Click Select Image Data.
- click Add on the right.
- Type imgBarCode in the Column Binding Name field.
- Set Display Name to Barcode.
- For the Data Type, select Blob from the list.
- Click Expression and enter the following code in the text area: reportContext.getAppContext().get(“imgBarCode”);
- Click OK.
- In the Select Data Binding window, select imgBarCode to enable it.
- Click OK. row[“imgBarCode”] is inserted in the dynamic expression textbox.
- Click Insert to add the image to the report. Now preview the report in Eclipse. Image placeholders are shown in the report preview.
- Save the report.
That completes the report design.
Insert BarCode Images Dynamically in the BIRT Report
Use the BIRT runtime engine and Aspose.BarCode for Java to generate the barcode images and insert into the newly created report.
Create a New .Java File
- Create a new Java project in Eclipse.
- 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......");
}
Output
The PDF report is generated on the specified location after the program is compiled and run successfully.
A screenshot of the PDF report is shown below. Output report, with barcode