I created the following class to allow me to dynamically resize the font size of report elements. The intention of this class is to allow me to add code to the ItemDataBound event for an element, section or report to automatically size the font to the best fit. The code works great for a TextBox element but does not work for an HtmlTextBox. The code gets the correct size based on the content that is provided but the report is not displayed with the updated style. I have confirmed that the code works if it is added to the ItemDataBinding event but at this point the Value of the element has not been populated. This scenario would only be beneficial for static content and in this case it would not be necessary to dynamically resize the font.
I am pretty sure that this is a bug with the HtmlTextBox control and/or the reporting engine. Is there a way to accomplish what I am trying to do before this can be fixed? Also, if this is in fact a bug when might you be able to fix it?
I am pretty sure that this is a bug with the HtmlTextBox control and/or the reporting engine. Is there a way to accomplish what I am trying to do before this can be fixed? Also, if this is in fact a bug when might you be able to fix it?
using System;using System.Drawing;using System.Linq;using Telerik.Reporting.Processing;namespace ReportSample{ public class ReportFont { public static void AutoSizeText(DetailSection dtl, int minFontSize, int maxFontSize) { foreach (Telerik.Reporting.Processing.ProcessingElement item in dtl.ChildElements) { if (!(item is ReportItem)) break; ReportItem rptItem = (ReportItem)item; if (TextSizingSupported(rptItem)) { AutoSizeText(rptItem, minFontSize, maxFontSize); } } } public static void AutoSizeText(ReportItem item, int minFontSize, int maxFontSize) { AutoSizeText(item, minFontSize, maxFontSize, false); } public static void AutoSizeText(ReportItem item, int minFontSize, int maxFontSize, bool forceResize) { if (!forceResize && !item.Name.EndsWith("_rf")) return; item.Style.Font.Size = GetFontSize(item, minFontSize, maxFontSize); } protected static Font GetFont(Telerik.Reporting.Drawing.Font font) { return GetFont(font.Name, font.Size.Value, font.Style); } protected static Font GetFont(Telerik.Reporting.Drawing.Font font, float fontSize) { return GetFont(font.Name, fontSize, font.Style); } protected static Font GetFont(string fontName, float fontSize, FontStyle style) { FontFamily fontFamily = new FontFamily(fontName); return GetFont(fontFamily, fontSize, style); } protected static Font GetFont(FontFamily fontFamily, float fontSize, FontStyle style) { return new Font(fontFamily, fontSize, style); } protected static bool TextSizingSupported(ReportItem item) { if (item is HtmlTextBox || item is TextBox) return true; return false; } protected static Telerik.Reporting.Drawing.Unit GetFontSize(ReportItem item, int minFontSize, int maxFontSize) { Size textSize; Font newFont = GetFont(item.Style.Font, maxFontSize); Size bounds = GetBounds(item); string value = GetValue(item); do { textSize = System.Windows.Forms.TextRenderer.MeasureText(value, newFont, bounds, System.Windows.Forms.TextFormatFlags.WordBreak); if (textSize.Height > bounds.Height && newFont.Size != minFontSize) newFont = GetFont(item.Style.Font, newFont.Size - 1); } while (textSize.Height > bounds.Height && newFont.Size > minFontSize); return Telerik.Reporting.Drawing.Unit.Point(newFont.Size); } protected static Size GetBounds(ReportItem tb) { Size bounds = (Size)tb.Bounds.Size; // Adjust the bounds slightly bounds.Height -= 3; return bounds; } protected static string GetValue(ReportItem item) { if (item is HtmlTextBox) return ((HtmlTextBox)item).Value.ToString(); if (item is TextBox) return ((TextBox)item).Value.ToString(); throw new Exception("Unsupported ReportItem passed."); } }}