Other PDF Issues

How to Add Four-Digit PDF Page Numbers with JavaScript

Steven ParkerSteven Parker Nov 10, 2026 868 views

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.

How to Add Four-Digit PDF Page Numbers with JavaScript
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.
Before you start

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.

Solution 1Recommended

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.

1
Create a Target Field

Add a text form field to your PDF document in the exact location where you want the four-digit page number to appear.

2
Access Field Properties

Right-click the text field, select 'Properties', and navigate to the 'Calculate' or 'Action' tab depending on your software version.

3
Apply the JavaScript Code

Select the option to run a custom calculation script and enter the string padding logic: var pageNumber = ("0000" + (this.pageNum + 1)).slice(-4);

4
Assign the Value

Complete the script by assigning the generated string to the text field's value. For example, add the line: event.value = pageNumber;

Use JavaScript String Padding for Page Numbers
Zero-Based Indexing: The 'this.pageNum' property in standard PDF JavaScript is zero-based (page 1 is index 0). Adding 1 before applying the string padding ensures the visual numbering starts at 0001.
Easy PDF Numbering Alternative

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. 1. Open PDF in WPS Office: Launch WPS Office and open your target PDF document.
  2. 2. Access the Page Tools: Navigate to the 'Insert' or 'Page' tab on the top ribbon and select the 'Bates Numbering' option.
  3. 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. 4. Apply and Save: Adjust the positioning and margins for the numbers, then click 'OK' to apply the formatting across all selected pages.
Easily add leading zeros to page numbers without writing custom JavaScript.Fully compatible with standard PDF formats and Adobe Acrobat.Comprehensive built-in Bates Numbering tools for advanced legal and corporate formatting.Lightweight and user-friendly interface for seamless PDF editing.
microsoft office alternative - wps office

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.