How to Create a Button to Print a Specific Page Range in PDF
Question details
The user wants to add an interactive button inside a PDF document that, when clicked, automatically opens the Print dialog with a pre-defined range of pages selected.

- Product
- PDF Editor
- Device & OS
- not provided
- Scenario
- Creating interactive PDF forms or documents where users need a quick, one-click way to print only specific sections or pages.
- Observed behavior
- Currently, users have to manually enter the page range in the print dialog. The goal is to automate this by embedding a button that triggers a JavaScript print command for the desired pages.
Ensure you have a PDF editing tool that supports form fields and JavaScript actions, such as Adobe Acrobat Pro or WPS Office PDF.
Use JavaScript to Create a Custom Print Button
Add a button using the Prepare Form tool and assign a Mouse Up JavaScript action to trigger the print dialog with a specific page range.
JavaScript is the standard way to interact with PDF viewers for custom actions. By specifying the start and end pages in an embedded script, the button will automatically select those pages in the user's print dialog.
Note that PDF JavaScript uses a 0-based index for pages, meaning page 1 is represented as 0, page 2 as 1, and so on.
Open your PDF document and access the 'Prepare Form' or 'Form' tab in your PDF editor to view the interactive field tools.
Select the 'Button' (or Push Button) tool from the toolbar and click on the page where you want the button to appear. Adjust its size and location.
Double-click the newly created button (or right-click and select 'Properties') to open the Button Properties dialog box. You can name the button and set its visual appearance here.
Navigate to the 'Actions' tab. Select 'Mouse Up' as the Trigger, and choose 'Run a JavaScript' as the Action. Click the 'Add' button.
In the JavaScript Editor, enter the following code: var firstPage = 10; var lastPage = 15; this.print({nStart: firstPage - 1, nEnd: lastPage - 1}); Change '10' and '15' to your desired starting and ending page numbers. Click OK and close the properties window.
Exit the form editing mode and click your new button to verify that the Print dialog opens with the correct page range automatically selected.

Create and Manage Interactive PDFs with WPS Office
WPS Office provides a comprehensive PDF editor that allows you to effortlessly add text, buttons, and interactive form fields to your documents. It fully supports PDF JavaScript, making it easy to create custom print buttons.
- 1. Open your PDF: Launch WPS Office and open your PDF document.
- 2. Access Form Tools: Navigate to the 'Form' tab on the top ribbon to access the interactive field tools.
- 3. Insert a Push Button: Click the 'Push Button' icon and draw it on your page. Right-click the button to access its properties.
- 4. Add JavaScript: Go to the 'Action' settings, choose 'Run a JavaScript', and input your print range script.

Frequently Asked Questions
Can I make the button print the current page only?
Yes. You can use JavaScript to print the current page by setting both the start and end pages to the current page number. The code would be: this.print({nStart: this.pageNum, nEnd: this.pageNum});
Will this JavaScript print button work on all PDF viewers?
While it works perfectly on major desktop PDF readers like Adobe Acrobat, WPS Office, and Foxit, some web browsers' built-in PDF viewers or mobile PDF apps may not fully support JavaScript actions.
How do I print multiple non-consecutive pages using a button?
The standard PDF JavaScript 'this.print' function does not natively support an array of non-consecutive pages (e.g., pages 1, 3, and 5) in a single print dialog call. You can only specify a continuous range using the nStart and nEnd parameters.
Can I hide the button when the document is printed?
Yes. In the Button Properties under the 'General' or 'Appearance' tab, look for the 'Form Field' visibility options and select 'Visible but doesn't print'. This ensures the button stays on the screen but won't appear on the physical paper.




