How to Display the Third-to-Last Page with an Acrobat Button Script
Question details
The user needs a way to make a PDF button dynamically jump to the third-to-last page without using a fixed page view link.

- Product
- Adobe Acrobat
- Device & OS
- not provided
- Scenario
- Creating an interactive PDF button that retains correct relative page navigation even after new pages are inserted or existing ones are deleted.
- Observed behavior
- The standard 'Go to a Page View' action statically links to a specific page number, which breaks when the document length changes.
Ensure you are using a PDF editor that supports JavaScript actions, such as Adobe Acrobat Pro, and verify your document has at least three pages to prevent script errors.
Add a JavaScript Action to the PDF Button
Use a simple JavaScript calculation in the button's properties to dynamically determine the destination page based on the current total page count.
By utilizing Acrobat's built-in JavaScript API, you can calculate the exact destination page on the fly. Since the script checks the total document length each time the button is clicked, it will remain accurate regardless of how many pages you add or remove.
Open your PDF document in Adobe Acrobat, navigate to the 'Tools' menu, and select 'Prepare Form' to enable interactive element editing.
Right-click the specific button you want to configure and select 'Properties' from the context menu.
Go to the 'Actions' tab. In the 'Select Action' dropdown menu, choose 'Run a JavaScript', and then click the 'Add' button.
In the JavaScript Editor window, type the following exact code: this.pageNum = this.numPages - 3; then click 'OK' to save.
Close the form editing mode and click your button to verify it successfully transports your view to the third-to-last page.

Manage and Edit PDFs Effortlessly with WPS Office
While advanced JavaScript button scripting is natively handled in specialized Acrobat tools, WPS Office provides a powerful, lightweight, and free alternative for managing, reading, and editing PDF files alongside Word, Excel, and PowerPoint documents.

Frequently Asked Questions
Why does the script subtract 3 instead of 2 to get the third-to-last page?
In Acrobat's JavaScript API, page numbers are zero-based, meaning page 1 is index 0. The numPages property is a standard 1-based count. To access the final page, you would use numPages - 1. Consequently, subtracting 3 lands you exactly on the third-to-last page.
Will this button script keep working if I merge another PDF into the current document?
Yes. Because the script dynamically references the this.numPages property every time the button is clicked, it recalculates the total page count and will reliably jump to the new third-to-last page after the merge.
Can I apply this same JavaScript to a bookmark instead of a form button?
Absolutely. You can right-click a bookmark, choose Properties, navigate to the Actions tab, and select 'Run a JavaScript'. Inputting the exact same code will make the bookmark dynamically navigate just like the button.
What happens if my PDF document has fewer than three pages?
If the document is shorter than three pages, the calculation will result in a negative number. This typically causes the PDF viewer to ignore the command and stay on the current page, or it may trigger a silent background error. It is best used on longer documents.




