This blog is about adding an image to a report in iReport dynamically. In this example I'm going to do it through a netbeans plugin (using iReport platform).
First create a netbeans module. Select iReport as the platform. Add an action class to the project. In gui registration , I selected it to get a menuitem under the Format menu.
then add following code to Action class.
public void actionPerformed(ActionEvent e) {
boolean isImageAdded=false;
JasperDesign report=IReportManager.getInstance().getActiveReport();
if( report != null ){
JRParameter param[]=report.getParameters();
for(int i=0; iif(param[i].getName().equals("photo")){
JOptionPane.showMessageDialog(null, "The logo is already added to the report");
isImageAdded= true;
}
}
if(!isImageAdded ){
getImage();
}
}
}
private void getImage(){
Image photo;
JFileChooser fc = new JFileChooser();
if (fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
fc.setCurrentDirectory(fc.getCurrentDirectory());
ImageIcon icon = new ImageIcon(fc.getSelectedFile().getAbsolutePath());
icon = new ImageIcon(icon.getImage());
photo = icon.getImage();
JasperDesign report=IReportManager.getInstance().getActiveReport();
if (report != null){
try {
JRDesignParameter parameter =new JRDesignParameter();
parameter.setName("photo");
parameter.setValueClass(java.lang.Object.class);
report.addParameter(parameter);
IReportManager.getInstance().saveiReportConfiguration();
JRDesignExpression expression=new JRDesignExpression();
expression.setText("$P{photo}");
expression.setValueClass(java.awt.Image.class);
JRDesignImage image = new JRDesignImage(null);
image.setY(0);
image.setWidth(photo.getWidth(fc));
image.setHeight(photo.getHeight(fc));
// image.setScaleImage(JRImage.SCALE_IMAGE_CLIP);
image.setExpression(expression);
JRDesignBand band=(JRDesignBand) report.getTitle();
band.setHeight(photo.getHeight(fc));
band.addElement(image);
IReportManager.getInstance().saveiReportConfiguration();
} catch (JRException ex) {
Exceptions.printStackTrace(ex);
}
Mapparam = new HashMap ();
param.put("photo", photo);
try {
generateReports( report,param);
} catch (JRException ex) {
Exceptions.printStackTrace(ex);
}
}
}
}
private void generateReports(JasperDesign report, Map param) throws JRException {
try {
if (IReportManager.getInstance().getActiveReport() != null){
JasperReport jasperReport = JasperCompileManager.compileReport(report);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, param, new JREmptyDataSource());
JasperViewer.viewReport(jasperPrint, false);
}
}
catch (JRException ex)
{Exceptions.printStackTrace(ex);
}
}
Now run the project.
January 12, 2011 at 5:00 AM
Thank's!!!
Your post did help me a lot of!!!
Congratulations for us!!!