Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Javascript for saving PDF with name from text field

New Here ,
Nov 11, 2019 Nov 11, 2019

Could someone please tell me how to correct the script below or provide an alternate script that will allow me to pull text from a text field labeled "ID" to use in performing a SAVE AS and RENAME with a form button in a PDF?

 

Thanks!!!!

 

I took this script from:

https://acrobatusers.com/forum/javascript/generating-filename-when-saving-acrobat-form/

 

where the "ID" text field = 19746

 

but I get an error that says:

Error! Could not save as: /C/Users/Me/underdfined19746.pdf

 

 

// get the value of the form field
var text1Value = this.getField("ID").value;
// here you need to make sure that no illegal characters are used, and also that text1Value is not empty
// make a file name from the field value
var newFileName = text1Value + ".pdf";
// get the path where the file is currently located
var filePath = this.path.replace(this.documentFileName);
// create the new full path
var newFullFilePath = filePath + newFileName;
try {
this.saveAs(newFullFilePath); // Only this will not work from a button...
} catch (e) {
app.alert("Error! Could not save as: " + newFullFilePath);
}

TOPICS
PDF forms
5.8K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 11, 2019 Nov 11, 2019

Your second parameter of replace is not defined:

http://www.w3schools.com/jsref/jsref_replace.asp

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 11, 2019 Nov 11, 2019
LATEST

Change this line:

var filePath = this.path.replace(this.documentFileName);

To:

var filePath = this.path.replace(this.documentFileName, "");

 

However, saving a file in this way requires a privileged context, which means you can't simply do it from a button field. See: https://acrobatusers.com/tutorials/trust-and-privilege-in-acrobat-scripts

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines