How to Create a Sequential Purchase Order Number in PDF with JavaScript
Question details
The user needs to generate sequential purchase order numbers within a PDF form using a script based on the date, department abbreviation, and a sequence value.

- Product
- PDF Forms
- Device & OS
- not provided
- Scenario
- Creating an automated purchase order workflow where the PDF form generates unique, incremental PO numbers dynamically.
- Observed behavior
- Requires a JavaScript solution to combine specific form fields (date, department) with a dynamically incrementing sequence number upon a specific trigger.
Before adding JavaScript to your PDF, decide exactly what action should trigger the number generation (e.g., clicking a button, saving the file, or printing) and ensure you have a reliable external database to track sequence values if multiple users will access the form.
Use Document-Level or Button JavaScript for PO Generation
Implement a custom JavaScript action to combine date and department values while fetching a unique sequence number from a persistent storage source.
Acrobat and standard PDF forms use JavaScript, not Java, for form scripting. To create a reliable sequential number, you cannot rely on a locally stored or random value within the PDF itself, as it will not guarantee unique numbering across different users.
Instead, your script should compile the department and date fields, and pull the sequential element from a controlled external process or database.
Decide when the number should be generated. You can assign the script to a specific trigger, such as the 'Mouse Up' action of a 'Generate PO' button, or a document-level action like 'Will Save' or 'Will Print'.
Use the PDF JavaScript API (e.g., `this.getField("Date").value`) to retrieve the current date and department abbreviation entered by the user.
Since local values cannot guarantee uniqueness across multiple users, configure your script to query an external web service or database that issues the next available sequence number.
Write the final JavaScript to concatenate the date, department abbreviation, and the retrieved sequence number, and output this combined string into the Purchase Order Number text field.
Edit and Manage PDF Forms Easily with WPS Office
WPS PDF provides a comprehensive and user-friendly set of tools to create, edit, and manage interactive PDF forms, making it easy to handle professional document workflows.
- 1. Launch WPS PDF: Open WPS Office and click on the 'PDF' tab to access the dedicated PDF editor.
- 2. Open or Create a PDF Form: Open your existing PDF document or create a new blank PDF to start designing your purchase order form.
- 3. Access Form Tools: Navigate to the 'Form' tab on the top ribbon to add text fields, check boxes, and interactive buttons.
- 4. Save and Distribute: Once your form layout is complete, save the PDF document and distribute it to your team for streamlined data entry.

Frequently Asked Questions
Why can't I use Java to script my PDF form?
PDF viewers and editors use JavaScript—specifically the Acrobat JavaScript API—for form scripting and document-level actions. Java is a completely different programming language and is not supported for native PDF form macros.
How can I ensure the PO number sequence doesn't duplicate if multiple users open the form?
A random or locally stored sequence value cannot guarantee uniqueness when multiple people use the form. You must connect the PDF form's JavaScript to a reliable persistent storage method, such as a centralized database or an external web API, to safely issue the next unique sequence number.
What actions can trigger the purchase order number generation?
You can configure your JavaScript to execute on various triggers depending on your workflow. Common triggers include opening the document, saving the file, printing it, or having the user click a specific interactive 'Generate PO' button.




