PDF Editing Issues

How to Apply Dynamic Strikethrough in PDF Forms via JavaScript

John WilsonJohn Wilson Nov 24, 2026 868 views

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.

How to Apply Dynamic Strikethrough Formatting in PDF Forms
Product
PDF
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.
Before you start

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.

Solution 1Recommended

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.

1
Enable Rich Text Formatting

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.

2
Access Custom Calculation Script

Switch to the 'Calculate' tab in the same Properties window and select the 'Custom calculation script' option.

3
Insert the JavaScript Code

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;

4
Save and Test

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.

Use Custom Calculation JavaScript for Rich Text Formatting
Rich Text Requirement: If 'Allow Rich Text Formatting' is not enabled in the field properties, the JavaScript code assigning the richValue array will be completely ignored by the PDF viewer.
Powerful PDF Editing Alternative

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. 1. Open the PDF File: Launch WPS Office and open your PDF form document in the PDF Editor module.
  2. 2. Access Form Tools: Navigate to the 'Fill & Sign' or 'Form' tab on the top ribbon to interact with existing form fields.
  3. 3. Fill the Form: Click directly into the highlighted text fields to type in your information seamlessly.
  4. 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. 5. Save the Document: Click the 'Save' icon or press Ctrl+S to save your filled and formatted PDF document locally.
Free and lightweight PDF reader and editorSupports filling interactive PDF forms effortlesslyHighly compatible with standard Adobe PDF document formatsIncludes intuitive rich text editing and annotation tools
QA img-9

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.