Jasper Reports: showing images dynamically depending on field values

五迷三道 提交于 2019-12-01 12:28:34

Layers (Static) Solution

  1. Divide the problem into cases. For example, case 1 show image 1, case 2 show images 2 and 3, case 3 show image 4 etc...
  2. For i to n (for n cases)
    • Create all the elements for case i.
    • Add a new layer, Layer i. Go to Window > Layers if the Layers window is hidden.
    • Select the elements added, right click and Send to layer. Choose layer i.
    • Right click on the layer in the Layers window and update the Print When Expression. For example, $F{Field_ONE} != null
    • Repeat. FYI, you can click on the eye on the layer (in the Layers window) to hide the applicable elements to hide the clutter.
  3. Done, each design you made for each case will only appear when the Layer's print when expression yields true.

Subreport (dynamic) Solution

  1. Create a report, lets call it subreport.
    • Set the width and height of the page to be the height and width of your image section for a given record.
    • Set the all of the page margins to 0
    • Change the column of the report to the number of images you'd like to have in a single row.
    • Change the print order to 'Horizontal'
  2. Add N parameters for the N field that needs to be associated with an image.
  3. Remove all the bands in the page except for the detail band.
  4. Write a dummy query that will return an image name depending on whether a given field is null or not. For example, select 'Ascent.jpg' as image from dual where $P{FIELD_1} is not null. You need to do this for each field and union all the select statements.
  5. In the detail section, add an Image element and set the value to be the path to your picture using the image field from the query above. For example, "C:/WINDOWS/Web/Wallpaper/"+$F{IMAGE}.
  6. Go to your original report and add the subreport to your detail section.
    • Link the fields in the original report to the parameters, created in #2, in the subreport.

I didn't want to post this solution until I tested it. I can post my sample, which has many lines, if there is something unclear. The reason you'll need a subreport is because your main report has 1 column and fills vertically but you need the images to fill horizontally across multiple columns. For each field that is not null, the subreport query will return a record in the statement order. The record will then be filled across the columns.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!