How to Remove 'Undefined' from Acrobat JavaScript PDF File Names
Question details
The user needs to prevent the word 'undefined' from appearing in automatically generated PDF file names when using an Acrobat JavaScript save script.

- Product
- Adobe Acrobat
- Device & OS
- not provided
- Scenario
- Running a custom JavaScript action in Acrobat to automatically generate a date-based file name and save the PDF.
- Observed behavior
- The script prepends or includes the text 'undefined' in the resulting PDF file name instead of just outputting the generated date.
Before modifying your Acrobat JavaScript, ensure you have saved a backup copy of your original script and have a sample PDF open to test the updated saveAs function.
Fix the replace() Method Syntax in Your JavaScript
Provide the correct replacement value in the replace() function to prevent it from returning 'undefined' during file path generation.
In Acrobat JavaScript, when using the replace() method to manipulate file paths, failing to provide a second argument (the replacement value) causes JavaScript to default to the string 'undefined'. By explicitly passing an empty string as the second argument, the path is processed correctly without injecting unwanted text.
Open your Acrobat Action Wizard or the JavaScript console where your custom saveAs script is located. Find the line of code defining the file path.
Change your code to explicitly use an empty string as the replacement. Modify the line to: var filePath = this.path.replace(this.documentFileName, "");
Add your generated date-based name to the clean path variable. For example: var newName = filePath + "YYYYMMDD.pdf";
Call the save function using the corrected path variable by adding the command: this.saveAs(newName);

Save and Rename PDFs Easily with WPS Office
If writing and debugging JavaScript in Adobe Acrobat feels overly complex, WPS Office provides an intuitive, user-friendly interface to manage, rename, and save PDF files instantly without writing a single line of code.
- 1. Open your PDF in WPS Office: Launch WPS Office on your device and open the PDF file you wish to rename or save.
- 2. Access the Save As menu: Click on the 'Menu' button located in the top-left corner of the interface and select 'Save As' from the dropdown list.
- 3. Specify the new file name: In the dialog box, choose your desired destination folder and type the new date-based name directly into the 'File name' text field.
- 4. Save the document: Click the 'Save' button to securely save your newly named PDF file without relying on complex scripts.

Frequently Asked Questions
Why does JavaScript return 'undefined' when renaming files?
In JavaScript, if a function like replace() expects two parameters but only receives one, the missing parameter is treated as 'undefined'. When concatenated with a string to build a file path, it physically prints the word 'undefined'.
What does this.documentFileName do in Acrobat JavaScript?
It is a built-in property in Adobe Acrobat JavaScript that returns the exact file name, including the extension, of the currently active document.
Can I automate PDF renaming without using JavaScript?
Yes. Many document management tools and alternative software suites like WPS Office offer built-in batch renaming or straightforward 'Save As' features that do not require writing custom code.
Does the saveAs method work in the free Adobe Reader?
No, the saveAs JavaScript method is heavily restricted in the free Adobe Acrobat Reader for security reasons. Executing custom save scripts generally requires Acrobat Pro or Standard.




