How to Automatically Generate Incremental Numbers in a PDF
Question details
The user needs to automatically generate unique, incremental numbers within a PDF, such as for invoices or forms, particularly when the document is downloaded or distributed.

- Product
- Device & OS
- not provided
- Scenario
- Creating forms or documents that require sequential numbering (e.g., invoices, tickets) either locally or during a web download process.
- Observed behavior
- Acrobat JavaScript can handle basic local numbering but fails to reliably generate unique sequential numbers upon file download without a centralized server-side database.
Determine if your PDF requires numbering for a single local user or if it will be downloaded by multiple users online, as this dictates whether a client-side or server-side solution is required.
Use Server-Side Scripting for Unique PDF Downloads
The most reliable method for ensuring every downloaded PDF receives a unique, database-backed incremental number.
Client-side scripts run inside the user's PDF reader after the file is downloaded, meaning they cannot communicate with other users' downloads to track a global sequence. To guarantee unique sequential numbers (like invoice numbers) upon download, you must use a server-side language like PHP, Java, or Python.
Create a database table (using MySQL, PostgreSQL, etc.) specifically designed to store and increment your sequence number every time a request is made.
Develop a script (e.g., in PHP) that connects to your database, fetches the current sequence number, and increments it by one.
Use a server-side PDF library (such as FPDF, TCPDF, or PDFBox) to open your blank PDF template and stamp the retrieved incremental number onto the designated area.
Configure your download link to point to this script so that when a user clicks 'Download', the server generates the numbered PDF on the fly and sends it to the user's browser.

Implement Acrobat JavaScript for Local Workflows
Ideal for a controlled, single-user workflow where incremental values can be based on local document data or external files.
Design and Prepare PDF Forms with WPS Office
Before applying advanced scripts or server-side numbering, use WPS PDF to easily design your document, insert interactive text fields, and format your invoice or ticket templates.
- 1. Open the PDF document: Launch WPS Office, click on 'PDF', and open your blank document or template.
- 2. Access form tools: Navigate to the 'Form' tab in the top ribbon menu to access interactive PDF tools.
- 3. Add a text field: Select 'Text Field' and draw the box exactly where the incremental number needs to be placed.
- 4. Configure field properties: Double-click the new text field to open its properties, assign a recognizable Field Name, and save the PDF template for deployment.

Frequently Asked Questions
Why doesn't standard PDF JavaScript work for download tracking?
Standard PDF JavaScript executes locally on the client's machine after the file is downloaded. Because it lacks a connection to a central server or database, it cannot communicate with other users' files to maintain a globally unique sequence.
Can I format the incremental number to include the current year?
Yes. Whether you use server-side scripting or local JavaScript, you can concatenate the current date with your sequence variable to create custom alphanumeric formats, such as '2023-0001'.
What programming languages are best for server-side PDF numbering?
Popular choices include PHP, Java, Python, and Node.js. These languages have excellent PDF manipulation libraries (like TCPDF, PDFBox, or ReportLab) that allow you to modify PDF form fields dynamically before sending the file to the user's browser.




