PDF Editing Issues

How to Delete a PDF Page After a Confirmation Dialog in Acrobat JavaScript

John WilsonJohn Wilson Nov 3, 2026 869 views

Question details

The user needs to prompt a confirmation dialog before executing a JavaScript command that deletes a specific page in a PDF.

How to Delete a PDF Page After a Confirmation Dialog in Acrobat JavaScript
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.
Before you start

Ensure that your PDF editing software supports interactive JavaScript execution and that the document security settings permit page extraction and deletion.

Solution 1Recommended

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.

1
Define the alert dialog and capture the response

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.

2
Create a conditional statement

Add an `if` block to evaluate the captured variable. Write `if (nRslt == 4)` to check if the 'Yes' button was explicitly clicked.

3
Execute the delete method

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.

Capture the Alert Result and Validate Before Deletion
Return Value Reference: Keep in mind that app.alert returns 1 for OK, 2 for Cancel, 3 for No, and 4 for Yes. Always check against the exact integer.
WPS Office PDF Alternative

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. 1. Open your PDF document: Launch WPS Office, click on the 'PDF' tab, and open the document you wish to edit.
  2. 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. 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. 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.
Visual page manager with zero coding requiredFull compatibility with all standard PDF formats and structuresLightweight application with robust editing tools for PC and mobile
microsoft office alternative - wps office

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.