Skip to main content
Participant
December 16, 2024
Answered

Use Save As button to create unique file name from form fields

  • December 16, 2024
  • 1 reply
  • 760 views

Hello,

 

I have a form that I've added a button to that needs to pull from form fields and save the file with those field names to create a unique file name.

I've read the old post on this topic:

Solved: Save PDF File Name as Field Name in PDF - Adobe Community - 7231510

However, it's not working for me. 

Steps I've taken:

1. Created JS file in the user directory for the trusted function C:\Users\Username\AppData\Roaming\Adobe\Acrobat\Privileged\DC\JavaScripts (this is for work and I don't have administrator access to save in the JS folder). 

2. Copied the JS file into the folder where the PDF lives

3. File contains script:

 

var mySaveAs = app.trustedFunction(function(oDoc, cPath, cFlName) {
FILE PATH REMOVED PER USER'S REQUEST

Sandbox/22178_Fillable 1_Out - 5 Sandbox Save to Folder.pdf/
cPath = cPath.replace(/([^/])$/, "$1/");
try {
app.beginPriv();
oDoc.saveAs(cPath + cFlName);
app.endPriv();
} catch (e) {
app.alert("Error During Save - " + e);
}
}
);

4. Added button to my form

5. Set button to run a Javascript with the following script:

 

// determine the directory path for the current document
var directory = this.path.substring(0, this.path.lastIndexOf('/') +1);
if (typeof(mySaveAs) == "function") {
mySaveAs(this, directory, this.getField("FirstName").value + " " + this.getField("LastName").value + " " + this.getField("DateofCalldd-Mon-yyyy").value + ".pdf");
} else {
app.alert("Missing Save Function. Please contact forms administrator ");
}

 

When I click the button, I get the following error message in the debugger

TypeError: this.getField(...) is null
4:Field:Mouse Up

 

Thanks for any help on this!

This topic has been closed for replies.
Correct answer PDF Automation Station

That error means one of the fields referenced in your script does not exist.  Check the field name spellings of the fields carefully against the field names in the script, including case sensitivity.  The 4 is line 4 so it's probably this field:

DateofCalldd-Mon-yyyy

1 reply

PDF Automation Station
Community Expert
Community Expert
December 16, 2024

That error means one of the fields referenced in your script does not exist.  Check the field name spellings of the fields carefully against the field names in the script, including case sensitivity.  The 4 is line 4 so it's probably this field:

DateofCalldd-Mon-yyyy

vlm_5095Author
Participant
December 16, 2024

Thank you! This worked for me now. I had an error in the last field