How to Prompt Before Overwriting a File with Acrobat JavaScript
Question details
The user needs to save a PDF file using Acrobat JavaScript with a generated filename, while ensuring a warning prompt appears if a file with the same name already exists.

- Product
- Adobe Acrobat / PDF Editor
- Device & OS
- not provided
- Scenario
- Automating PDF saving using JavaScript where filename conflicts might occur.
- Observed behavior
- The user wants a confirmation dialog to prevent accidental overwriting of existing PDF files during an automated save process.
Ensure you have administrative access to your computer, as you will need permission to create and configure folder-level JavaScript files in your PDF application's installation directory.
Configure saveAs Options in a Privileged Context
Use the saveAs method with the bPromptToOverwrite property enabled within a folder-level trusted script.
To prompt users before overwriting a file, you must pass an options object to the saveAs method with the 'bPromptToOverwrite' parameter set to true.
Crucially, security restrictions prevent trusted functions from being defined directly inside a document-level script or form button. The script must be placed in a privileged context, such as a folder-level JavaScript file.
Navigate to the JavaScripts folder of your Acrobat installation and create a new .js file using a plain text editor.
Inside the .js file, define a trusted function using app.trustedFunction. This function will wrap your save operation to grant it the necessary system privileges.
Within your trusted function, insert the save code: doc.saveAs({cPath: filePath, bPromptToOverwrite: true});. This ensures the user is warned if the target filename already exists.
In your PDF form, edit the button's JavaScript action to call the trusted function you defined in the folder-level script, passing the current document object as a parameter.
Try WPS Office for Comprehensive PDF Editing
While advanced Acrobat automation requires complex folder-level scripting, WPS Office provides an intuitive, lightweight, and fully-featured PDF editor alongside its Word, Excel, and PowerPoint alternatives. It simplifies your document workflow without unnecessary complexity.
- 1. Download WPS Office: Visit the official WPS website to download the free installer for your operating system.
- 2. Install the Application: Run the setup file and follow the on-screen instructions to install the comprehensive office suite.
- 3. Open and Edit PDFs: Launch WPS Office, select the PDF module, and open your files to start reading, annotating, or editing immediately.

Frequently Asked Questions
Why doesn't my saveAs script work when placed directly in a form button?
For security reasons, PDF applications restrict scripts from silently writing to the local file system. The saveAs method requires a privileged context, meaning it must be executed from a trusted folder-level script rather than a standard document-level or button script.
What happens if I set bPromptToOverwrite to false?
If bPromptToOverwrite is set to false (or omitted entirely), the JavaScript saveAs method may automatically overwrite any existing file with the same name without displaying a warning dialog, which could lead to accidental data loss.
Where do I place folder-level JavaScript files?
Folder-level scripts are typically placed in the application's JavaScripts directory. The exact path depends on your operating system and the specific PDF software, but on Windows, it is often found in the Program Files directory under the application's installation folder.




