How to Fill Multiple PDF Fields with Random Numbers Using JavaScript
Question details
The user needs a script to populate 100 form fields in a PDF with random integers between 1 and 20 via a button click.

- Product
- PDF Editor
- Device & OS
- not provided
- Scenario
- Creating automated PDF forms requiring random data generation for multiple text fields.
- Observed behavior
- The user requires a custom JavaScript action to dynamically assign random numerical values to specific fields when an action button is triggered.
Ensure your PDF fields have sequential or consistent names (e.g., Field1, Field2) before adding the script, as this makes looping through the fields much easier.
Use JavaScript to Generate Random Numbers in PDF Fields
Add a custom JavaScript action to a button that loops through all target fields and assigns them a random integer.
To quickly fill numerous fields, writing a document-level or button-level JavaScript is the most efficient method. By using a loop structure, you can target hundreds of fields simultaneously without writing individual lines of code for each one.
Check the properties of the 100 fields you want to fill. Ensure they follow a consistent naming convention, such as 'Text1', 'Text2', up to 'Text100'.
Add a new button to your PDF form. Right-click the button, select 'Properties', and navigate to the 'Actions' tab.
Choose 'Run a JavaScript' as the action trigger and click 'Add'. Enter a loop script that generates a random number and assigns it, for example: for (var i = 1; i <= 100; i++) { var n = Math.floor(Math.random() * 20) + 1; this.getField('Text' + i).value = n; }
Close the JavaScript editor and properties dialog. Click the button in your PDF viewer to instantly populate all 100 fields with random numbers from 1 to 20.

Manage Interactive PDF Forms Easily with WPS Office
WPS Office provides a comprehensive PDF editor that allows you to create, modify, and fill interactive PDF forms effortlessly. Whether you are adding text fields, creating buttons, or managing form data, WPS Office offers a seamless experience with extensive compatibility.
- 1. Open Your PDF Form: Launch WPS Office and open your PDF document containing the form fields.
- 2. Access Form Tools: Navigate to the 'Form' tab on the top ribbon to access interactive form controls like buttons and text fields.
- 3. Edit Field Properties: Double-click any form field or button to adjust its properties, names, and assigned interactive actions directly within WPS Office.

Frequently Asked Questions
Can I generate random numbers with decimal points instead of integers?
Yes, to include decimals, you can remove the Math.floor() function from your JavaScript code. For example, using just Math.random() * 20 will generate a random decimal number between 0 and 20.
Why is the JavaScript button not working in my PDF?
Ensure that the PDF viewer you are using supports JavaScript processing. Some lightweight browsers or basic PDF readers disable JavaScript for security reasons. Use full-featured software like WPS Office or Adobe Acrobat to run active form scripts.
How do I clear the fields after filling them with random numbers?
You can add a second button with the action set to 'Reset a form'. Select the specific fields you wish to clear, and clicking this reset button will instantly wipe the generated random numbers.




