New to Telerik UI for WinForms? Start a free 30-day trial
How to Adjust Font Size for the Text Fields in PDF Forms
Updated over 6 months ago
Environment
| Product Version | Product | Author |
|---|---|---|
| 2023.1.314 | RadPdfViewer for WinForms | Desislava Yordanova |
Description
When a very long text is entered in a text field of a PDF form, the font size remains unchanged and the text is scrolled in the following way:
Default Font Size

Abode offers in the PDF forms text auto-sizing/shrinking functionality so the text can fit in the available box and it is fully visible. This article demonstrates a sample approach how to simulate a similar behavior:
Auto Font Size

Solution
It is possible to change the font size dynamically for the activated editor. For this purpose, it is necessary to measure the text and determine the correct size.
C#
private void RadPdfViewer1_MouseUp(object sender, MouseEventArgs e)
{
foreach (RadFixedPageElement visualPage in this.radPdfViewer1.PdfViewerElement.ViewElement.Children)
{
foreach (RadElement editorElement in visualPage.Children)
{
RadTextBoxControlElement tb = editorElement as RadTextBoxControlElement;
if (tb != null)
{
font = tb.Font;
tb.TextChanged -= tb_TextChanged;
tb.TextChanged += tb_TextChanged;
}
}
}
}
Font font;
float width;
float initialFontSize = -1;
private void tb_TextChanged(object sender, System.EventArgs e)
{
RadTextBoxControlElement tb = sender as RadTextBoxControlElement;
string textToMeasure = tb.Text;
float fontSize = tb.Font.Size;
if (initialFontSize == -1)
initialFontSize = tb.Font.Size;
SizeF measured = TextRenderer.MeasureText(textToMeasure, font);
width = tb.Size.Width;
if (measured.Width > width)
{
do
{
fontSize = Math.Max(0, fontSize - 1);
font = new Font(tb.Font.FontFamily, fontSize, FontStyle.Regular);
measured = TextRenderer.MeasureText(textToMeasure, font);
}
while (!(measured.Width < width));
}
else
do
{
fontSize = Math.Min(fontSize + 1, initialFontSize);
font = new Font(tb.Font.FontFamily, fontSize, FontStyle.Regular);
measured = TextRenderer.MeasureText(textToMeasure, font);
}
while ((measured.Width < width) && fontSize < initialFontSize);
tb.Font = font;
}