How to Delete a PDF Page After a Confirmation Dialog in Acrobat JavaScript
Question details
The user needs to prompt a confirmation dialog before executing a JavaScript command that deletes a specific page in a PDF.

- Product
- Adobe Acrobat / PDF Editor
- Device & OS
- not provided
- Scenario
- Programming an interactive button in a PDF document to remove dynamically generated pages, requiring user confirmation to prevent accidental deletion.
- Observed behavior
- The page deletion script fails to work when the confirmation dialog is added because the script does not correctly store the alert result or checks the wrong return value.
Ensure that your PDF editing software supports interactive JavaScript execution and that the document security settings permit page extraction and deletion.
Capture the Alert Result and Validate Before Deletion
Store the value returned by `app.alert` in a variable and verify it against the numerical value representing the 'Yes' button before running the delete command.
In Acrobat JavaScript, the `app.alert` method returns specific integer values based on which button the user clicks. A standard Yes/No dialog (button configuration 2) returns the number `4` for 'Yes' and `3` for 'No'. To execute a dependent action, you must capture this output and place your deletion method inside a conditional statement.
Declare a variable to store the user's choice. Use `var nRslt = app.alert("Are you sure you would like to delete this page from the planner?", 1, 2, "Confirm deletion");`. The parameter '1' specifies an information icon, and '2' creates the Yes/No buttons.
Add an `if` block to evaluate the captured variable. Write `if (nRslt == 4)` to check if the 'Yes' button was explicitly clicked.
Place the deletion script inside your condition block: `{ this.deletePages(this.pageNum, this.pageNum); }`. This ensures the current page is only deleted when the condition is met.

Manage and Delete PDF Pages Visually with WPS Office
If programming JavaScript buttons is too complex or unnecessary for your workflow, WPS Office provides an intuitive, visual interface to delete, insert, and rearrange PDF pages instantly without any coding.
- 1. Open your PDF document: Launch WPS Office, click on the 'PDF' tab, and open the document you wish to edit.
- 2. Navigate to the Page tools: Click on the 'Page' tab located in the top menu ribbon to switch to the visual page management view.
- 3. Select and delete pages: Click on the thumbnail of the page you want to remove, then click the 'Delete' (trash can) icon or press the Delete key on your keyboard.
- 4. Save the modified PDF: Press Ctrl + S or click the Save icon to apply the changes and overwrite or save as a new PDF.

Frequently Asked Questions
Why does `app.alert` return a number instead of true or false in Acrobat JavaScript?
The `app.alert` method in Acrobat JavaScript supports various button configurations, such as OK/Cancel, Yes/No, and Yes/No/Cancel. Returning a specific integer (e.g., 4 for Yes, 3 for No, 2 for Cancel) allows developers to precisely identify which option was selected rather than relying on a simple boolean.
Can I delete multiple pages simultaneously using `this.deletePages`?
Yes, the `this.deletePages` method accepts two parameters: the starting page index and the ending page index. For instance, `this.deletePages(0, 2)` will remove the first three pages of the document, as Acrobat JavaScript relies on zero-based indexing.
Why does `this.pageNum` correctly delete the page the button is on?
The `this.pageNum` property retrieves the zero-based index of the currently active page. When used as both the start and end parameter in `this.deletePages(this.pageNum, this.pageNum)`, it restricts the deletion to that single active page.




