Search code examples
c#vb.netimagealignmentrdlc

RDLC Proportional Image Alignment


In a RDLC file, is there any way to align an image to the center of a Tablix cell height? Somehow, it can only have a fixed location. The only solution I can find would be the padding, but I can't seem to find a way to get the cell dimension at runtime.

The reason I need to do this is because other cell on the row might change the height of the row, and since it's mostly just textboxes, the image looks do not align.


Solution

  • I found some kind of solution. It requieres to know the value of the field that will affect the height of the line. You also need a monospaced font. So basically, I set an expression for the padding of the image. From there i call a report function:

    Function GetLineAmount(ByVal str As String, ByVal lineLength As Integer) As Integer
        Dim amount As Integer = 1
        
        If str.Length > lineLength Then
            Dim positionToFind As Integer = lineLength
            
            If str(positionToFind) <> " " Then
                ' Find the last position of a space character
                Do While positionToFind >= 0 AndAlso str(positionToFind) <> " "
                    positionToFind -= 1
                Loop
                
                ' Find the last position before that which is not a space (group of spaces)
                Do While positionToFind >= 0 AndAlso str(positionToFind) = " "
                    positionToFind -= 1
                Loop
                
                positionToFind += 1
                
            End If
            
            If positionToFind = 0 Then 
                positionToFind = lineLength
            End If
            
            Dim rest As String = str.Substring(positionToFind)
    
            'PDF somehow "deletes" the first space in a line of text, so 2 space will leave one ... leaving this if to adapt to the pdf.
            If rest.Length > 0 AndAlso rest(0) = " " Then
                rest = rest.Substring(1)
            End If
            
            If rest <> String.Empty Then
                amount += GetLineAmount(rest, lineLength)
            End If
        End If
        
        Return amount
    End Function
    

    This will return the number of line a field will take. You must know the number of character a line will take (which is why you need a monospaced font)

    Then from the image padding expression, the code will be :

     =  iif(Code.GetLineAmount(Fields!Description.Value,24) = 1
        , "1pt"
        , (4 + (6 * (Code.GetLineAmount(Fields!Description.Value,24) -2) )) & "pt"
        )
    

    So in that part i send the field Description which is the one taking more line sometime, then the 24, the number of caracters that can fit the lines.

    (This part is to explain the if) From my test, those were the observation for the padding on top and bottom:

    • 1 line would need a 1pt padding
    • 2 line needed 4 pt
    • 3 and more were increment of 6.