How to Make the First Line of a PDF Text Field Bold Using JavaScript
Question details
The user needs to format only the first line of text within an interactive PDF text box as bold, leaving all subsequent lines in a regular font weight.

- Product
- Device & OS
- not provided
- Scenario
- Creating advanced interactive PDF forms where specific visual text formatting is required for the first input line automatically.
- Observed behavior
- By default, text appearance settings apply to the entire text box, but the desired state is to isolate the bold formatting to just the first line using a rich-text JavaScript snippet.
Ensure your PDF editing software supports JavaScript and rich text formatting for form fields. You must enable the 'Allow Rich Text Formatting' option in the text field's properties before applying the script.
Apply a Custom Validation Script for the First Line
Use a custom JavaScript code snippet to split the text input into lines and apply an 800 font weight (bold) exclusively to the first line.
Interactive PDFs support rich-text formatting through JavaScript. By using a validation script, the PDF calculates the styling the moment the user finishes typing and leaves the text field.
Open your interactive PDF form in edit mode and double-click the target text field to open the Text Field Properties dialog box.
Navigate to the 'Options' tab and check the box labeled 'Allow Rich Text Formatting'.
Go to the 'Validate' tab, select 'Run custom validation script', and click 'Edit'.
Paste the following script into the editor: var lines = event.value.split("\r"); var spans = []; for (var i = 0; i < lines.length; i++) { spans.push({text: lines[i] + "\r", fontWeight: i == 0 ? 800 : 500}); } event.richValue = spans; Then click 'OK'.
Save the PDF and test the field. Type multiple lines of text and click outside the box to trigger the script and see the first line turn bold.

Use Appearance Attributes to Make All Text Bold
If you decide you want the entire text block to be bold rather than just the first line, you can achieve this without using any JavaScript.
Create and Edit Interactive PDFs with WPS Office
WPS PDF provides a comprehensive and user-friendly suite of tools for creating interactive PDF forms, modifying text fields, and adjusting form properties quickly without complex setups.
- 1. Open Your PDF: Launch WPS Office and open your PDF document.
- 2. Access Form Tools: Navigate to the 'Form' tab on the top ribbon to access the interactive form tools.
- 3. Insert or Edit a Text Field: Click 'Text Field' to draw a new text box, or double-click an existing one to open its settings.
- 4. Customize and Save: Adjust the appearance, text formatting, and interactive options as needed, then save the document.

Frequently Asked Questions
Why isn't the text formatting changing immediately while I type?
The rich-text JavaScript validation script relies on a validation event. This event is triggered only when you finish editing the content and click outside (or tab out) of the text field, meaning real-time styling while typing is not supported.
Does the 'Allow Rich Text Formatting' option need to be enabled for the script to work?
Yes. If rich text formatting is not checked in the text field's properties, the 'event.richValue' assignment in your JavaScript will be ignored, and the entire text will remain in plain formatting.
Can I make the first line a different color instead of bold?
Yes. You can modify the script's array to include a 'textColor' property (e.g., textColor: color.red) for the first index, rather than or in addition to changing the 'fontWeight' property.
Will this JavaScript formatting work on mobile PDF viewers?
Not consistently. Many mobile PDF viewing apps do not fully support complex JavaScript calculations or rich-text form field properties. It is highly recommended to fill out advanced interactive forms on a desktop PDF reader.




