How to Add Four-Digit PDF Page Numbers with JavaScript
Question details
The user needs to generate PDF page numbers with leading zeros to create a four-digit format (e.g., 0001, 0002) using a custom JavaScript code snippet.

- Product
- PDF Editor
- Device & OS
- not provided
- Scenario
- Customizing the display of page numbers within a PDF document using form fields or page label workflows to maintain a consistent digit length.
- Observed behavior
- Standard page numbers lack leading zeros by default, requiring a string-padding script to enforce an exact four-digit display.
Ensure you are using a PDF editor that supports advanced JavaScript execution, such as Adobe Acrobat Pro, as basic PDF readers may not process document-level or form-field calculation scripts.
Use JavaScript String Padding for Page Numbers
Apply a simple JavaScript padding technique to automatically format the page index into a four-digit string with leading zeros.
This script can be implemented as a document-level script, a page action, or inside a form field calculation, depending on where you want the number to be displayed.
Add a text form field to your PDF document in the exact location where you want the four-digit page number to appear.
Right-click the text field, select 'Properties', and navigate to the 'Calculate' or 'Action' tab depending on your software version.
Select the option to run a custom calculation script and enter the string padding logic: var pageNumber = ("0000" + (this.pageNum + 1)).slice(-4);
Complete the script by assigning the generated string to the text field's value. For example, add the line: event.value = pageNumber;

Add Formatted Page Numbers Easily with WPS PDF
Skip the complex coding. WPS PDF provides built-in tools like Bates Numbering to automatically add four-digit page numbers with leading zeros to your document with just a few clicks.
- 1. Open PDF in WPS Office: Launch WPS Office and open your target PDF document.
- 2. Access the Page Tools: Navigate to the 'Insert' or 'Page' tab on the top ribbon and select the 'Bates Numbering' option.
- 3. Configure Number Format: In the Bates Numbering settings dialog, set the 'Number of Digits' to 4. This automatically forces leading zeros for single, double, or triple-digit numbers (e.g., 0001).
- 4. Apply and Save: Adjust the positioning and margins for the numbers, then click 'OK' to apply the formatting across all selected pages.

Frequently Asked Questions
Why does my JavaScript page number script return NaN or an incorrect value?
This usually happens if the page-number variable (such as 'this.pageNum') evaluates as a string before mathematical addition occurs. Ensure you use proper parenthesis to isolate the math operation: (this.pageNum + 1).
Can I get four-digit page numbers without using JavaScript?
Yes. Advanced PDF editors like WPS PDF offer 'Bates Numbering' features. Unlike standard page numbers, Bates Numbering lets you specify the exact number of digits, automatically generating leading zeros without any coding.
How do I make the page number start from 0001 on the third page of the PDF?
In your JavaScript calculation, you can write an IF statement to check if 'this.pageNum' is greater than or equal to 2 (since page 1 is index 0). If true, subtract 2 from the current page count before applying the 0000 string padding.




