LOAD IMAGES DYNAMICALLY INTO JASPER REPORTS

If you want to insert images from the file system (e.g. C:\images folder) into jasper reports at runtime, for example changing a logo depending on user selection or simply allowing the user to browse for his or her image to be displayed  in the report then here is a simple process of doing that.

Let start with the report design using ireport. In this instance the picture will be passed to the report as a parameter.

So let’s create a parameter in our report and call it “photo”

image

As shown in the picture above, the data type of our parameter should be “java.lang.Object”

After this, drag the image , from the tool bar, on to your report.Right click on the image and choose “Properties” from the menu

image

Under the image tab of the dialog box, make sure you select “java.awt.image” for the Image Expression Class. Once this is done, you are done with the report design

Lets go to netbeans , do some coding and connect our designed report to a java application.

First we need to declare a variable of type image,

Image photo;

Also we create a method , which on click of a button, loads a picture from the file system and initializes the “photo: variable.

JFileChooser fc = new JFileChooser();

private void getPicture() {

fc.setFileFilter(new FileNameExtensionFilter("Images", "jpg", "gif", "bmp"));

if (fc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {

fc.setCurrentDirectory(fc.getCurrentDirectory());

ImageIcon icon = new ImageIcon(fc.getSelectedFile().getAbsolutePath());

icon = new ImageIcon(icon.getImage().getScaledInstance(350, 350, Image.SCALE_DEFAULT));

photo = icon.getImage();

}

After getting our picture, create an instance of the Map class and pass our “variable” as a parameter.

Map<String, Object> param = new HashMap<String, Object>();

param.put("photo", photo); //the “photo” should be the same name as the parameter name in our report

We then create a method that passes our created parameter to the report and we are done. below is the method that does the job.

private void generateReports(String name, Map param) {

try {

String source = "C:/sabonay/jasperreports/" + name + ".jrxml";

if (new File(source).exists() == false) {

xputils.showMessage("Please  report Source does not exist");

return;

}

JasperReport jasperReport = JasperCompileManager.compileReport(source);

JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, param, new JREmptyDataSource());

JasperViewer.viewReport(jasperPrint, false);

//the false parameter makes sure the application does not close on closing the report

} catch (Exception e) {

System.out.println("reports Error " + e.toString());

}}

Make sure to reference the necessary jasper jar files in your application

Advertisements

9 thoughts on “LOAD IMAGES DYNAMICALLY INTO JASPER REPORTS

  1. how to create a method , which on click of a button, loads a picture from the file system and initializes the “photo: variable.

    • Image photo;

      JFileChooser fc = new JFileChooser();
      private void getPicture() {
      fc.setFileFilter(new FileNameExtensionFilter(“Images”, “jpg”, “gif”, “bmp”));
      if (fc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
      fc.setCurrentDirectory(fc.getCurrentDirectory());
      ImageIcon icon = new ImageIcon(fc.getSelectedFile().getAbsolutePath());
      icon = new ImageIcon(icon.getImage().getScaledInstance(350, 350, Image.SCALE_DEFAULT));
      photo = icon.getImage();

  2. Hi Mister Gilbert
    I worked with your exercise LOAD IMAGES DYNAMICALLY INTO JASPER REPORTS, very good.
    But I have the follow error:
    reportErrorjava.lang.IllegalArgumentException: Width (-1) and height (-1) cannot be <= 0
    I hope you can give me a hand.
    Thanks

  3. Hi Mister Gilbert
    Thank you ver much, definitly you are the men!!.
    The problem are the .jar files.
    I was working with the jar file itext 1.0.3, so I download a recent version of itext jar file (itext 2.0.3).
    Now I can view the report, the image and I can save the document.

    Again Thank you very much

    MAURICIO

    • Hi Mister Gilbert
      I hope you are ok!.
      I’m doing a desktop application that generate a report file (.pdf) , I’m using Netbeans IDE 6.8.
      I can run the application and I can view and save the report (inside the IDE).
      When I want to make the executable(I want to run the application outside The IDE) of the aplication and test it (double click to.jar file) the aplication runs ok, but the report doesn’t appear (I can’t view and save it).
      I hope you can help me like in other ocassions.
      Thank a lot for your important help.
      MAURICIO

  4. Hi Mister Gilbert,
    Thank you very much, your article is nice.
    I have some problem, i want to make “Id card” application.
    In detail jasper report, i want to show image one by one depend on id_member in mysql.

    can u help me?

    illustration :

    id_member name_member
    1 Gilbert
    2 Aliong
    3 Jonathan

    I want to design in my report like this :

    ID : 1 [PHOTO HERE C:\image\1.jpg]
    NAME : GILBERT [ ]

    ID : 2 [PHOTO HERE C:\image\2.jpg]
    NAME : ALIONG [ ]

    ID : 3 [PHOTO HERE C:\image\3.jpg]
    NAME : JONATHAN [ ]

    Photo is taken from image in folder consist of the photos.
    Example photo of id_member : 1 will be taken from C:\image\1.jpg, etc.. => C:\image\[id_member].jpg

    I hope, you could help me.. Thanks.. Setiawan..

  5. Hi Sir

    I created a student registration form. and have wrote to view the riport this code in netbeans but it is coming error. please help urgently……
    Code:
    try {

    String reportSource = “C:/Documents and Settings/Radh & Sameer/My Documents/NetBeansProjects/DHO Management System/report/student page one.jrxml”;
    String reportSource2 = “C:/Documents and Settings/Radh & Sameer/My Documents/NetBeansProjects/DHO Management System/report/student page two.jrxml”;

    Map params2 = new HashMap();
    Map params = new HashMap();

    params.put(“stdindexno”,txt_stdindexno.getText());

    JasperReport jasperReport2 = JasperCompileManager.compileReport(reportSource2);
    JasperReport jasperReport = JasperCompileManager.compileReport(reportSource);

    Class.forName(“com.mysql.jdbc.Driver”).newInstance();
    Connection conn = (Connection) DriverManager.getConnection(“jdbc:mysql:///dhomsystem”, “root”, “”);

    JasperPrint jasperPrint2 = JasperFillManager.fillReport(jasperReport2, params2, conn);
    JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, conn);

    JasperViewer.viewReport(jasperPrint2, false);
    JasperViewer.viewReport(jasperPrint, false);

    conn.close();

    } catch (Exception e) {
    e.printStackTrace();
    }

    Error:
    net.sf.jasperreports.engine.JRException: Byte data not found at location : java.io.ByteArrayInputStream@1a7ffb3
    at net.sf.jasperreports.engine.util.JRLoader.loadBytesFromLocation(JRLoader.java:467)
    at net.sf.jasperreports.engine.JRImageRenderer.getInstance(JRImageRenderer.java:175)
    at net.sf.jasperreports.engine.fill.JRFillImage.evaluateImage(JRFillImage.java:921)
    at net.sf.jasperreports.engine.fill.JRFillImage.evaluate(JRFillImage.java:870)
    at net.sf.jasperreports.engine.fill.JRFillElementContainer.evaluate(JRFillElementContainer.java:275)
    at net.sf.jasperreports.engine.fill.JRFillBand.evaluate(JRFillBand.java:426)
    at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillColumnBand(JRVerticalFiller.java:1380)
    at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillDetail(JRVerticalFiller.java:692)
    at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReportStart(JRVerticalFiller.java:255)
    at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:113)
    at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:891)
    at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:795)
    at net.sf.jasperreports.engine.fill.JRFiller.fillReport(JRFiller.java:63)
    at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:402)
    at dho.management.system.StudentRegistration.btn_viewActionPerformed(StudentRegistration.java:2047)
    at dho.management.system.StudentRegistration.access$7900(StudentRegistration.java:49)
    at dho.management.system.StudentRegistration$80.actionPerformed(StudentRegistration.java:1892)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.java:6267)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
    at java.awt.Component.processEvent(Component.java:6032)
    at java.awt.Container.processEvent(Container.java:2041)
    at java.awt.Component.dispatchEventImpl(Component.java:4630)
    at java.awt.Container.dispatchEventImpl(Container.java:2099)
    at java.awt.Component.dispatchEvent(Component.java:4460)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
    at java.awt.Container.dispatchEventImpl(Container.java:2085)
    at java.awt.Window.dispatchEventImpl(Window.java:2478)
    at java.awt.Component.dispatchEvent(Component.java:4460)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

    • Hello Sameer,

      with the parameter you are suppose to pass a byte array not a text

      “params.put(“stdindexno”,txt_stdindexno.getText());” so Please modify this line

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s