How to Apply Dynamic Strikethrough in PDF Forms via JavaScript
Question details
The user wants to dynamically apply strikethrough formatting to a PDF form text field when a different field is populated, utilizing custom JavaScript.

- Product
- Device & OS
- not provided
- Scenario
- Creating or editing an interactive PDF form where text styling needs to change automatically based on specific user inputs.
- Observed behavior
- The target text field should automatically display a strikethrough style only if a specified secondary field contains data.
Ensure you are using a PDF editor that supports form field creation and custom JavaScript calculations. Verify the exact field names in your PDF document before writing and testing the script.
Use Custom Calculation JavaScript for Rich Text Formatting
Apply a custom JavaScript snippet to the target field's calculation event to modify its rich text properties dynamically based on another field's value.
By leveraging the richValue property in PDF JavaScript, you can dynamically control text formatting like strikethroughs, underlines, and font weights inside text fields.
Open your PDF form in Edit mode, right-click the primary text field you want to format (e.g., Box1), select Properties, navigate to the 'Options' tab, and check the 'Allow Rich Text Formatting' box.
Switch to the 'Calculate' tab in the same Properties window and select the 'Custom calculation script' option.
Click the 'Edit' button and input the following code: var spans = new Array(); spans[0] = new Object(); spans[0].text = event.value; spans[0].strikethrough = this.getField('Box2').value == '' ? false : true; event.richValue = spans;
Click 'OK' to save the script, close the Properties window, and test the form by entering text into the secondary field (e.g., Box2) to verify the strikethrough is applied.

Edit and Manage PDF Forms Easily with WPS Office
WPS Office provides a comprehensive suite of PDF tools, allowing you to easily view, fill, and edit interactive PDF forms without requiring complex scripting or expensive software setups.
- 1. Open the PDF File: Launch WPS Office and open your PDF form document in the PDF Editor module.
- 2. Access Form Tools: Navigate to the 'Fill & Sign' or 'Form' tab on the top ribbon to interact with existing form fields.
- 3. Fill the Form: Click directly into the highlighted text fields to type in your information seamlessly.
- 4. Apply Manual Formatting: If you need to manually apply strikethroughs to static text, switch to the 'Comment' tab, select the Strikethrough tool, and highlight the desired text.
- 5. Save the Document: Click the 'Save' icon or press Ctrl+S to save your filled and formatted PDF document locally.

Frequently Asked Questions
Why is the JavaScript strikethrough not showing up in my PDF form?
The primary text field must have 'Allow Rich Text Formatting' enabled in its options tab. If this setting is unchecked, the richValue script will not be executed or displayed by the PDF reader.
Can I use this JavaScript snippet in standard free PDF readers?
Most standard PDF readers support basic JavaScript execution for form calculations, but complex rich text rendering via JavaScript may require advanced PDF software or a dedicated PDF editor to display correctly.
How do I modify the script to underline text instead of adding a strikethrough?
You can change the boolean property in the JavaScript object array. Simply replace 'spans[0].strikethrough = true' with 'spans[0].underline = true' using the same conditional logic.




