Last Updated: 08 May 2023 07:17 by ADMIN
Created on: 08 May 2023 06:18
Category: PdfViewer
Type: Feature Request
RadPdfViewer: Add text field font auto-sizing functionality

Add functionality for a PDF form filler that supports text auto-sizing/shrinking like Adobe:

1 comment
Dess | Tech Support Engineer, Principal
Posted on: 08 May 2023 07:17

Hi, Christopher,

Indeed, RadPdfViewer doesn't support such kind of functionality when a long text is inserted:

Even though this feature should be precisely implemented by our developers, an idea popped up in my mind which can try implementing. 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.

    Private Sub RadPdfViewer1_MouseUp(sender As Object, e As MouseEventArgs)
        For Each visualPage As RadFixedPageElement In Me.RadPdfViewer1.PdfViewerElement.ViewElement.Children
            For Each editorElement As RadElement In visualPage.Children
                Dim tb As RadTextBoxControlElement = TryCast(editorElement, RadTextBoxControlElement)
                If tb IsNot Nothing Then
                    font = tb.Font
                    RemoveHandler tb.TextChanged, AddressOf tb_TextChanged
                    AddHandler tb.TextChanged, AddressOf tb_TextChanged
                End If
    End Sub

    Dim font As Font
    Dim width As Double
    Private Sub tb_TextChanged(sender As Object, e As System.EventArgs)
        Dim tb As RadTextBoxControlElement = TryCast(sender, RadTextBoxControlElement)
        Dim textToMeasure As String = tb.Text
        Dim fontSize As Single = tb.Font.Size
        Dim measured As SizeF = TextRenderer.MeasureText(textToMeasure, font)
        width = tb.Size.Width
        If measured.Width > width Then
                fontSize = fontSize - 1
                font = New Font(tb.Font.FontFamily, fontSize, FontStyle.Regular) 
                measured = TextRenderer.MeasureText(textToMeasure, font)
                Console.WriteLine(fontSize & " >> " & measured.Width & "available " & width)
            Loop Until measured.Width < width
        End If
        tb.Font = font 
    End Sub

The achieved result is illustrated below:

Note that this is just a sample approach and it may not cover all possible cases. Feel free to modify and further extend it according to the specific requirements.

Dess | Tech Support Engineer, Principal
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.