How to Remove Buttons and Flatten a PDF Form Using JavaScript
Question details
The user needs to remove buttons and flatten a PDF form using a JavaScript script, but the script fails due to syntax errors and unexpected termination.

- Product
- PDF Editor
- Device & OS
- not provided
- Scenario
- Automating the removal of form buttons and flattening the document via a PDF JavaScript routine.
- Observed behavior
- The script uses an assignment operator instead of a comparison operator, unintentionally changing field types. Additionally, deleting the button that triggers the script causes the execution to stop unexpectedly.
Ensure you have saved a backup copy of your original interactive PDF form, as flattening pages and removing fields via script cannot be easily undone.
Correct the JavaScript Operator and Execution Flow
Fix the comparison operator and ensure the triggering button is not deleted while the loop is actively running to prevent the script from halting.
When writing JavaScript for PDFs, using a single equals sign assigns a value, which changes the field type instead of checking it. Furthermore, if a script deletes the exact object executing it, the process terminates immediately before finishing the remaining tasks.
Open your PDF script editor and locate the if-statement checking the field type. Change if (f.type = "button") to if (f.type == "button") to ensure it compares the value rather than assigning it.
Modify the condition to prevent the active button from being deleted during the loop. Update your code block to: if (f.type == "button" && f.name != event.target.name) { this.removeField(f.name); }.
After the main loop has completely finished iterating through all the form fields, add a single line of code outside the loop to safely delete the button that originally triggered the action.

Easily Manage and Flatten PDF Forms with WPS Office
If managing JavaScript is too complex or your current reader does not support flattening scripts, WPS Office provides an intuitive PDF editor to handle form fields and flatten documents natively without any coding.
- 1. Open the PDF Form: Launch WPS Office and open your PDF document containing the interactive form elements.
- 2. Access Form Tools: Navigate to the 'Form' tab on the top ribbon to access all interactive field editing capabilities.
- 3. Delete Unwanted Buttons: Click on any unwanted buttons or form fields to select them, then press the 'Delete' key on your keyboard to remove them manually.
- 4. Flatten the Document: Use the 'Print' function (Ctrl+P) and select 'Print to PDF' from the printer dropdown to easily flatten the form fields into a static document.

Frequently Asked Questions
Why does my JavaScript stop running when I delete a PDF form button?
If your script deletes the exact button that triggered the action, the script execution terminates immediately. You must use JavaScript conditions to exclude the active button during the loop and delete it only after the loop finishes.
Why does the flattenPages() command not work in my PDF reader?
The flattenPages() JavaScript method requires elevated permissions typically found only in full PDF editors, such as Adobe Acrobat or WPS PDF. Basic viewers like Acrobat Reader do not have the capability to execute flattening commands.
What happens if I use a single equals sign instead of a double equals sign in my PDF script?
A single equals sign (=) acts as an assignment operator rather than checking a condition. For example, using if (f.type = "button") will accidentally change the field type to "button" instead of checking if it already is one. Always use == for comparisons.




