Horizontally Centred Dynamic Images in SSRS

Centring Dynamic Logos in SSRS

This was an interesting problem I ran into the other day, basically I have a report that displays data on a range of businesses. The report then needed a dynamic header to display the logos for each of the businesses that feature in the report. The number of businesses could vary anywhere from 1 to 11 depending on the parameters selected.

In my case I was using images stored along side the reports but you could achieve exactly the same thing using images from a database.

Getting the images on the report

The image to display was derived from one of the other columns in my dataset so I added a derived field called ImageURI;



As tables are only capable of grouping in a vertical direction and I wanted to display the logos across the top of the page I opted for a Matrix. After adding a Matrix to the report and setting the appropriate dataset the next step was to add a column grouping for our ImageURI field;



I then dragged an image control into the header cell of the column (click finish to get out of the wizard) and then in the properties pane set the source to be external and the value to be the ImageURI field;



Now when we run the report we get all the appropriate logos displayed across the top of the page, the next step is to evenly space them to fill the entire page width.



Centring the Images

Now the options for modifying the layout of a Matrix are reasonably limited, primarily because we have no idea how many columns there are going to until run time and cells will automatically re-size to fit whatever data is thrown at them.

Based on this I used the “Padding” attribute of the image to force each cell to grow enough to fill the width of the page. I targeted the left padding of each image which will result in adding a bunch of white space to the left of each cell and pushing the image to the right hand side.
This is the original equation I used;

=(460 / (CountDistinct(Fields!ImageURI.Value, "Products")) - 80).ToString & "pt"
Note: 460 is the total width of my page and 80 is the width of each image



Now all I needed to do was get rid of the padding on the first image, to do this I modified my expression to only apply the padding if this was not the first grouping in the matrix, I achieved this using the RunningValue function as follows;

=iif(RunningValue(1, Sum, "matrix2 ") > 1,((380 / (CountDistinct(Fields!ImageURI.Value, "Products") - 1)) - 80).ToString & "pt", "0pt")

The result is nicely centred images as required. Obviously this approach only works if you have fixed width images (80px in my case), if not you will need to take the width of each image into account in the padding formula, a project for another day.