I searched for a long time for this and never found a solution. I broke down today and wrote one.
Called with this as the expression for the textbox:
=Code.FitWidth(Fields!JobComments.Value, 8, 1.523)
The string to fit ...Value
The font size
The width of the text box in inches.
The Code
Public Function FitWidth(tempStr as String, iFontSize As Integer, size as Decimal) as String
Dim bmpImage As New Drawing.Bitmap(1, 1)
Dim iWidth As Integer = 0
Dim iHeight As Integer = 0
Dim temp as String
size = (size * 72) '// I want in units in points
'// Create the Font object for the image text drawing.
Dim MyFont As New Drawing.Font("Arial", iFontSize, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point)
Dim MyGraphics As Drawing.Graphics = Drawing.Graphics.FromImage(bmpImage)
MyGraphics.PageUnit = System.Drawing.GraphicsUnit.Point
iWidth = MyGraphics.MeasureString(tempStr, MyFont).Width
iHeight = MyGraphics.MeasureString(tempStr, MyFont).Height
while (iWidth > size)
temp = Left(tempStr, Len(tempStr)-1)
tempStr = temp
iWidth = MyGraphics.MeasureString(tempStr, MyFont).Width
End While
Return tempStr
End Function
There is probably a better way math-wise to do this but it works.
Craig