How to Change Font by Character Count in Acrobat with JavaScript
Question details
The user needs to dynamically change the font typeface in a PDF form field using JavaScript when the entered text exceeds 30 characters.

- Product
- Adobe Acrobat
- Device & OS
- not provided
- Scenario
- Setting up an interactive PDF form where the text appearance must adapt to spatial constraints, switching to a narrower font like Arial Narrow if the text length exceeds a set character count.
- Observed behavior
- Attempting to change the form's font by setting standalone variables (like textSize or textFont) or using this.rawValue in a custom calculation script fails to update the text appearance.
Ensure you have a PDF editor that supports custom JavaScript for form fields, and identify the exact name of the text field you wish to target in your document.
Apply a Field-Level Formatting Script
Set the field font properties directly through the field object instead of using standalone variables to properly update the text appearance.
When scripting in Acrobat, modifying font properties requires targeting the specific form field object. Assigning values to disconnected variables like `textSize` or using syntax like `this.rawValue` (which is often associated with LiveCycle/XFA forms) will not function in standard AcroForms.
In Adobe Acrobat, enter Prepare Form mode. Right-click your target text field and select Properties from the context menu.
Go to the Format tab. In the Select format category drop-down list, select Custom, and then click the Edit button next to the Custom Keystroke Script option.
Input the script to target the field object dynamically: var field = this.getField("TextField"); if (field.value.length < 30) { field.textFont = "Arial"; } else { field.textFont = "Arial Narrow"; } field.textSize = 11; Replace "TextField" with the exact name of your form field.
Click OK to close the script editor, then close the Properties window. Ensure this script runs on a relevant event trigger such as Keystroke, Validation, or Formatting so it detects character changes instantly.

Create and Customize Interactive PDF Forms with WPS Office
WPS Office offers a comprehensive desktop PDF editor that allows you to easily design, fill, and format interactive PDF forms. With its visual property editor, modifying text fields, fonts, and box sizes is highly intuitive—often removing the need for complex JavaScript.
- 1. Open PDF in WPS Office: Launch WPS Office and open your PDF document using the PDF editor.
- 2. Access Form Tools: Navigate to the Form tab on the ribbon menu to access the interactive form creation toolkit.
- 3. Add a Text Field: Click on Text Field, then drag your mouse on the PDF page to draw the input box.
- 4. Format Font visually: Double-click the newly created text field to open its Properties window. Switch to the Appearance tab to easily select your desired Font, Size, and Text Color without writing code.

Frequently Asked Questions
Why doesn't standard JavaScript work in PDF forms?
Acrobat uses a specific JavaScript API known as AcroJS. You must reference PDF-specific document objects (like `this.getField()`) rather than standard HTML/DOM elements to manipulate form field properties.
Can I apply this font script to multiple fields at once?
Yes. You can create a document-level JavaScript function and call it from each field's formatting event, passing `event.target.name` dynamically to avoid rewriting the code for every single field.
How do I test my JavaScript in Acrobat for errors?
You can use the built-in JavaScript Debugger console. Press Ctrl+J (Windows) or Cmd+J (Mac) to open the console, where you can manually execute scripts and check for syntax or runtime errors.




