Unplanned
Last Updated: 05 Sep 2025 08:52 by Alvin
Alvin
Created on: 05 Sep 2025 08:52
Category: PdfProcessing
Type: Bug Report
1
PdfProcessing: The VariableTextProperties.FontSize property doesn't convert the DIP value to points

The TextProperties.FontSize property specifies the font size for text fragments. The property is of type double. The measurement unit used for font size in RadPdfProcessing is Device Independent Pixels (DIPs). You can convert it to points or other units using the Unit class.

However, when using the TextProperties with widgets the font conversion is not correct. Let's consider the case that we build a PDF document with a TextBoxField occupying a specific rectangle. According to the set text value, we should calculate the appropriate font size so the whole content can fit in the widget's rectangle.

        /// <summary>
        /// Creates a TextBoxField with calculated font size for the given rectangle
        /// </summary>
        private static TextBoxField CreateTextBoxWithCalculatedFont(string name, string text, Rect rect, double fontSize, FontBase font)
        {
            TextBoxField field = new TextBoxField(name);
       
            field.TextProperties.FontSize = Unit.DipToPoint(fontSize);
            field.TextProperties.Font = font;
            field.Value = text;

            var widget = field.Widgets.AddWidget();
            widget.Rect = rect;
            widget.Border.Width = 0;
            widget.TextProperties.FontSize = Unit.DipToPoint(fontSize); 
            widget.TextProperties.Font = font;

            return field;
        }

        /// <summary>
        /// Calculates the optimal font size for text to fit within a specific rectangle
        /// </summary>
        public static double CalculateFontSizeForRectangle(string text, Rect rect, FontBase font)
        {
            double fontSize = 0;
            Size measuredSize = new Size(0, 0);
            Size availableSize = rect.Size;
            while (measuredSize.Width<availableSize.Width && measuredSize.Height< availableSize.Height)
            {
                fontSize++;
                Block block = new Block();
                block.TextProperties.FontSize = fontSize;
                block.TextProperties.Font = font;
                block.InsertText(text);
                measuredSize = block.Measure();
            }

            return fontSize-1;
        }

            // Example: Wide textbox with calculated font size
            string wideText = "This is a wide textbox that demonstrates horizontal fitting of text content.";
            Rect wideRect = new Rect(200, 500, 400, 30);
            double wideFontSize = CalculateFontSizeForRectangle(wideText, wideRect, font);

            TextBoxField wideTextBoxField = CreateTextBoxWithCalculatedFont("WideTextBox", wideText, wideRect, wideFontSize, font);
            document.AcroForm.FormFields.Add(wideTextBoxField);
            var wideWidget = wideTextBoxField.Widgets.First();
            page.Annotations.Add(wideWidget);
            wideWidget.RecalculateContent();

0 comments