How to Stop PDF JavaScript from Changing a Saved Quote Number
Question details
The user needs to prevent a PDF document script from generating a new quote number every time a completed and saved PDF form is reopened.

- Product
- PDF Editors
- Device & OS
- not provided
- Scenario
- Reopening a completed PDF form that contains a JavaScript function designed to generate unique quote numbers.
- Observed behavior
- The JavaScript runs automatically upon opening the document, overwriting the previously saved quote number with a newly generated random number.
Ensure you have access to the PDF's JavaScript editor and keep a backup copy of your original form before modifying any document scripts.
Use Conditional JavaScript to Check for Empty Fields
Modify your document script so it only generates a random quote number if the field is currently blank, preventing it from overwriting saved data.
By adding an "if" statement to your existing script, you can instruct the PDF reader to evaluate whether the quote field already contains a value. If the document was previously saved with a quote number, the condition prevents the script from running again.
Open your PDF form in Adobe Acrobat or your advanced PDF editor, and navigate to the Document JavaScripts or the specific Page Open trigger event.
Find the line of code that sets the value of the quote number field (e.g., using Math.random).
Wrap your generation code in a conditional check so it only executes when empty: if (this.getField("Quote Number").value == "") { this.getField("Quote Number").value = util.printf("%06d", Math.floor((Math.random() * 1000000) + 1)); }
Save the PDF, fill out the form, and reopen it to verify that the originally generated quote number remains unchanged.

Overwrite the Script Upon Locking the Form
Use a button action to simultaneously lock form fields and overwrite the generating script, preventing it from executing on reopen.
Edit and Flatten PDF Forms with WPS Office
If you want to prevent interactive scripts from altering your finalized forms, WPS PDF offers a streamlined way to fill out, flatten, and manage your PDF documents easily.
- 1. Download WPS Office: Install WPS Office and launch the WPS PDF application.
- 2. Open your form: Import your interactive PDF quote document directly into the workspace.
- 3. Fill out the details: Complete the form fields, letting any native generation scripts run as intended for the initial setup.
- 4. Print to PDF: To permanently lock the quote number, use the 'Print' feature and select 'Print to PDF' to flatten the document, rendering all scripts inactive.

Frequently Asked Questions
Why does my PDF quote number change every time I open it?
This occurs because a document-level JavaScript triggers automatically upon opening the file. Without conditional logic, the script continuously generates a new random number instead of retaining the previously saved value.
Can I completely delete a document script in Acrobat using a button?
JavaScript does not allow you to directly delete a document script for security reasons. However, you can use the addScript method to overwrite the existing script with an empty string or comments, effectively neutralizing it.
Does flattening a PDF prevent scripts from running?
Yes. Flattening a PDF converts all interactive form fields and scripts into static visual elements. Once flattened, no JavaScript will execute, keeping your quote numbers permanently locked.




