PDF Editing Issues

How to Generate a Four-Digit Random Number in a PDF Form

Olivia MillerOlivia Miller Oct 28, 2026 869 views

Question details

The user wants to configure an existing PDF work order form to automatically generate a new four-digit identifying number every time a blank form is opened.

How to Generate a Four-Digit Random Number in a PDF Form
Product
PDF
Device & OS
not provided
Scenario
Setting up a dynamic PDF form template that assigns a unique four-digit identifier to each new work order upon opening.
Observed behavior
The user needs the specific JavaScript code and instructions on exactly where to enter it within the PDF properties to achieve the automated number generation.
Before you start

Ensure you are using a professional PDF editor that supports interactive form fields and JavaScript execution, and that you have created a text field named 'Unique ID' in your document.

Solution 1Recommended

Add a Page-Open JavaScript Action

Implement a short JavaScript code snippet that triggers whenever the PDF page opens to populate a designated text field with a random four-digit number.

By binding a JavaScript action to the 'Page Open' event, the PDF will automatically execute the script each time the document is viewed.

Please note that utilizing the Math.random() function generates a random sequence, meaning duplicate numbers are possible. There is approximately a 0.1% chance of duplication on the second generation, and this probability increases with repeated use.

1
Create the Target Text Field

Open your PDF in form editing mode and insert a new Text Field. Name this field exactly 'Unique ID' (without the quotes) in the field properties.

2
Access Page Properties

Right-click on the page thumbnail in the navigation pane and select 'Page Properties'.

3
Set Up the Action Trigger

Navigate to the 'Actions' tab. Select 'Page Open' from the Select Trigger dropdown menu, and choose 'Run a JavaScript' from the Select Action dropdown menu.

4
Insert the JavaScript Code

Click the 'Add' button and paste the following code into the JavaScript editor: this.getField("Unique ID").value = Math.random().toString().substr(2,4); Click OK to save.

5
Test the Form

Save your PDF document, close it, and reopen it. Check the 'Unique ID' field to ensure a new four-digit number has been generated.

Add a Page-Open JavaScript Action
Preserving the Generated Number: If you want the number to remain static after it is initially generated, save the filled form as a flat PDF or a new file. If a user saves the interactive form and reopens it, the Page-Open script will run again and replace the previous number.
Manage PDFs with WPS Office

Create and Edit Interactive PDF Forms with WPS Office

WPS Office provides robust PDF editing capabilities, allowing you to easily create, modify, and fill interactive PDF forms while maintaining full compatibility with standard PDF scripts and formats.

  1. 1. Open Your PDF Document: Launch WPS Office and open your existing PDF template using the WPS PDF tool.
  2. 2. Access Form Tools: Navigate to the 'Form' tab on the top ribbon to access all interactive form creation features.
  3. 3. Insert Form Fields: Select the 'Text Field' option and drag to place it on your document. Double-click it to rename and customize properties.
  4. 4. Save and Share: Once your form fields are set up, save the document to keep your interactive elements intact for seamless data collection.
Easily add, edit, and format text fields, checkboxes, and dropdowns in your PDF forms.Highly compatible with Adobe Acrobat PDF formats and interactive elements.Lightweight, fast, and features an intuitive interface for seamless document management.Supports extensive PDF conversions and page management tools.
microsoft office alternative - wps office

Frequently Asked Questions

Can I use a different name for my text field?

Yes. You can name your text field anything you prefer. However, you must ensure that you update the JavaScript code to match your chosen name exactly. For example, if your field is named 'OrderNum', change the script to: this.getField("OrderNum").value = Math.random().toString().substr(2,4);

How do I ensure the random number doesn't change when a client opens the saved file?

Because a Page-Open action triggers every time the document is viewed, it will overwrite the number upon reopening. To prevent this, you can generate the number before sending the form, then flatten the PDF (which removes interactive properties but keeps the text) or remove the JavaScript action prior to distribution.

How do I completely avoid duplicate numbers being generated?

Using a simple JavaScript Math.random() function inside an offline PDF cannot guarantee absolute uniqueness. If strict uniqueness is required for work orders, you will need to utilize a server-side script or database-connected application to generate sequential numbers.

Where can I put the code if I want to use Document-Level JavaScript instead?

In your PDF editor's JavaScript tools, look for an option called 'Document JavaScripts'. You can define your script there as a function. However, to automatically populate a field upon opening the file, attaching it to a Page-Open action or a Document 'Will Print' / 'Did Save' action is typically more effective depending on your workflow.