Skip to content

Commit 51bee02

Browse files
authored
Merge pull request #625 from telerik/new-kb-auto-font-size-form-fields-pdfprocessing-bb4fa243e78d48fa9c2785015ede054e
Added new kb article auto-font-size-form-fields-pdfprocessing
2 parents 00f9d96 + 89ec9ac commit 51bee02

File tree

3 files changed

+144
-0
lines changed

3 files changed

+144
-0
lines changed
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
---
2+
title: Adjust Font Size and Flatten Form Fields in PDFs with PdfProcessing
3+
description: Learn how to calculate and adjust the font size of text fields when flattening form fields of a PDF document with PdfProcessing.
4+
type: how-to
5+
page_title: Adjust Font Size and Flatten Form Fields in PDFs with PdfProcessing
6+
meta_title: Adjust Font Size and Flatten Form Fields in PDFs with PdfProcessing
7+
slug: auto-font-size-form-fields-pdfprocessing
8+
tags: pdf, processing, text, box, field, flatten, font, size, recalculation, acroform, fixed, calculate, auto, fit
9+
res_type: kb
10+
ticketid: 1697695
11+
---
12+
13+
<style>
14+
img[alt$="><"] {
15+
border: 1px solid lightgrey;
16+
}
17+
</style>
18+
19+
## Environment
20+
21+
| Version | Product | Author |
22+
| ---- | ---- | ---- |
23+
| 2025.3.806| RadPdfProcessing |[Desislava Yordanova](https://www.telerik.com/blogs/author/desislava-yordanova)|
24+
25+
## Description
26+
27+
Learn how to adjust the font size of textbox fields to fit the whole text content while processing a PDF form.
28+
29+
![Adjust TextBox Font Size ><](images/textboxfield-calculculate-font-size.gif)
30+
31+
## Solution
32+
33+
To auto-size text in [TextBoxFields]({%slug radpdfprocessing-model-interactive-forms-form-fields-textboxfield%}), set the `TextBoxField.TextProperties.FontSize` property to `0`. This enables the font size to adjust automatically to fit the content when the document is **displayed** in a viewer.
34+
35+
```csharp
36+
TextBoxField textBoxField = new TextBoxField("AutoSizeTextBox");
37+
textBoxField.TextProperties.FontSize = 0; // Auto-size when displayed in a PDF viewer
38+
textBoxField.TextProperties.Font = FontsRepository.HelveticaBold;
39+
textBoxField.Value = "Sample text for auto-sizing.";
40+
```
41+
42+
However, if you want to adjust the font size and flatten the [form fields]({%slug radpdfprocessing-model-interactive-forms-form-fields%}) to produce a non-editable PDF document, it would be necessary to calculate the appropriate font size according to the rectangle occupied by the [widget]({%slug radpdfprocessing-model-annotations-widgets%}).
43+
44+
The following code snippet shows how to calculate the font size manually to fit the content using the following approach:
45+
46+
```csharp
47+
public static double CalculateFontSizeForRectangle(string text, Rect rect, FontBase font)
48+
{
49+
double fontSize = 0;
50+
Size measuredSize = new Size(0, 0);
51+
Size availableSize = rect.Size;
52+
53+
while (measuredSize.Width < availableSize.Width && measuredSize.Height < availableSize.Height)
54+
{
55+
fontSize++;
56+
Block block = new Block();
57+
block.TextProperties.FontSize = fontSize;
58+
block.TextProperties.Font = font;
59+
block.InsertText(text);
60+
measuredSize = block.Measure();
61+
}
62+
63+
return fontSize - 1;
64+
}
65+
```
66+
67+
Use this calculated font size to create the textbox field:
68+
69+
```csharp
70+
static void Main(string[] args)
71+
{
72+
RadFixedDocument document = new RadFixedDocument();
73+
RadFixedPage page = document.Pages.AddPage();
74+
FontBase font = FontsRepository.Helvetica;
75+
string wideText = "This is a wide textbox that demonstrates horizontal fitting of text content.";
76+
Rect wideRect = new Rect(200, 500, 400, 30);
77+
double wideFontSize = CalculateFontSizeForRectangle(wideText, wideRect, font);
78+
79+
TextBoxField wideTextBoxField = CreateTextBoxWithCalculatedFont("WideTextBox", wideText, wideRect, wideFontSize, font);
80+
document.AcroForm.FormFields.Add(wideTextBoxField);
81+
var wideWidget = wideTextBoxField.Widgets.First();
82+
page.Annotations.Add(wideWidget);
83+
wideWidget.RecalculateContent();
84+
string outputPath = "AutoSizeTextBoxForm.pdf";
85+
using (FileStream output = File.Create(outputPath))
86+
{
87+
PdfFormatProvider provider = new PdfFormatProvider();
88+
provider.Export(document, output, TimeSpan.FromSeconds(10));
89+
}
90+
91+
Process.Start(new ProcessStartInfo() { FileName = outputPath, UseShellExecute = true });
92+
93+
}
94+
95+
/// <summary>
96+
/// Creates a TextBoxField with calculated font size for the given rectangle
97+
/// </summary>
98+
private static TextBoxField CreateTextBoxWithCalculatedFont(string name, string text, Rect rect, double fontSize, FontBase font)
99+
{
100+
TextBoxField field = new TextBoxField(name);
101+
102+
field.TextProperties.FontSize = Unit.DipToPoint(fontSize);
103+
field.TextProperties.Font = font;
104+
field.Value = text;
105+
106+
var widget = field.Widgets.AddWidget();
107+
widget.Rect = rect;
108+
widget.Border.Width = 0;
109+
widget.TextProperties.FontSize = Unit.DipToPoint(fontSize); ;
110+
widget.TextProperties.Font = font;
111+
112+
return field;
113+
}
114+
115+
/// <summary>
116+
/// Calculates the optimal font size for text to fit within a specific rectangle
117+
/// </summary>
118+
public static double CalculateFontSizeForRectangle(string text, Rect rect, FontBase font)
119+
{
120+
double fontSize = 0;
121+
Size measuredSize = new Size(0, 0);
122+
Size availableSize = rect.Size;
123+
while (measuredSize.Width<availableSize.Width && measuredSize.Height< availableSize.Height)
124+
{
125+
fontSize++;
126+
Block block = new Block();
127+
block.TextProperties.FontSize = fontSize;
128+
block.TextProperties.Font = font;
129+
block.InsertText(text);
130+
measuredSize = block.Measure();
131+
}
132+
133+
return fontSize-1;
134+
}
135+
```
136+
137+
138+
## See Also
139+
140+
- [PdfProcessing Overview]({%slug radpdfprocessing-overview%})
141+
- [Flatten Form Fields]({%slug radpdfprocessing-flatten-form-fields%})
142+
- [TextBoxFields]({%slug radpdfprocessing-model-interactive-forms-form-fields-textboxfield%})
143+
- [Widget]({%slug radpdfprocessing-model-annotations-widgets%})
46.2 KB
Loading

libraries/radpdfprocessing/model/interactive-forms/form-fields/textboxfield.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,4 @@ TextBoxField exposes the following properties:
9191
* [Create Interactive Forms SDK example](https://github.com/telerik/document-processing-sdk/tree/master/PdfProcessing/CreateInteractiveForms)
9292
* [Modifying Forms SDK example](https://github.com/telerik/document-processing-sdk/tree/master/PdfProcessing/ModifyForms)
9393
* [Customizing Text and Border Colors to Highlight a TextBoxField with RadPdfProcessing]({%slug radpdfprocessing-customize-textboxfield-colors%})
94+
* [Adjusting Widgets' Font Size to Fit the Whole Content in Form Fields Using PdfProcessing]({%slug auto-font-size-form-fields-pdfprocessing%})

0 commit comments

Comments
 (0)