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

Save PDF File Name as Field Name in PDF

Explorer ,
May 29, 2015 May 29, 2015

Copy link to clipboard

Copied

Hi all,

I've read through this thread : https://acrobatusers.com/tutorials/how-save-pdf-acrobat-javascript ‌ and I still can't figure this out.

I want to have a custom save button on my fillable form PDF, and have it save the file name with the fields in my form.  The field names are "RequestedBy" , "NameofEvent" , "EventStartmdyy"

so as an example, the pdf is filled out and returned to me with its default pdf name as "Event Request Form Update-1.pdf" , then I get it, and click the button, then it saves it as "DarinVegasTradeshowJune152015.pdf" and saves to a specific folder every time.

I read about getting trusted access and put it into a javascript either in the folder or on my computer? My co-worker and I will be the only person to save these, so it's only on two machines.

Hope you can assist.

Thank you

Views

41.5K

Translate

Translate

Report

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

correct answers 1 Correct answer

Explorer , Jun 01, 2015 Jun 01, 2015

I tried the following;

The correct syntax I got, based on the debugger for "this.path" returned

/HAUCACAM0007VS/Shares/Marketing_bridge/Darin/`MasterTracker/EventRequestForms/1/test.pdf

so I changed the code from this

// determine the directory path for the current document 

var directory = this.path.substring(0, this.path.lastIndexOf('/') +1); 

to this

// determine the directory path for the current document 

var directory = "/HAUCACAM0007VS/Shares/Marketing_bridge/Darin/`MasterTracker/EventRequest

...

Votes

Translate

Translate
Community Expert ,
May 29, 2015 May 29, 2015

Copy link to clipboard

Copied

The trusted function needs to be stored in a folder level script, so it needs to go either into the system level or the user level JavaScripts folder.

Votes

Translate

Translate

Report

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
Explorer ,
May 29, 2015 May 29, 2015

Copy link to clipboard

Copied

Do you have an example of a trusted function? I am unaware of what that is, or looks like. Also, where would the system level default location be? Is the user level the 'folder' that the pdf is in?

I then also don't know how to code to suit my needs, maybe the trusted part made my test code not work? i am not sure.

thanks for your help

Votes

Translate

Translate

Report

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 ,
May 29, 2015 May 29, 2015

Copy link to clipboard

Copied

The tutorial you've linked to contains the implementation for such a trusted function. This tutorial explains where to save such scripts: https://acrobatusers.com/tutorials/folder_level_scripts

Votes

Translate

Translate

Report

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
Explorer ,
May 29, 2015 May 29, 2015

Copy link to clipboard

Copied

Thanks, found the location on my computer, however I am still not 100% clear on WHAT code goes inside that javascript file?

as a test, I put this code in a config.txt file, then saved as config.js

[code]

var mySaveAs = app.trustedFunction(

  function(oDoc,cPath,cFlName)

  {

  // Ensure path has trailing "/"

  cPath = cPath.replace(/([^/])$/, "$1/");

  try{

  oDoc.saveAs(cPath + cFlName);

  }catch(e){

  app.alert("Error During Save");

  }

  }

);

[/code]

I then used the second part of that, and put that into my javascript inside the pdf, but it says "syntaxerror: missing )  after arugment list 5 : at line 6.  I put a ) there and didnt help.

am I getting on the right track? I have not much confidence in this at this point, as i am not seeing how this will work for my application of renaming a file via button

Votes

Translate

Translate

Report

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
Explorer ,
May 29, 2015 May 29, 2015

Copy link to clipboard

Copied

sorry, this is the code inside the javascript in the PDF, can't find the edit button on this forum

// First make sure the function exists

if(typeof(mySaveAs) == "function"){

    mySaveAs(this,this.path);

}else{

    app.alert("Missing Save Fucntion" + Please contact forms administrator");

}

Votes

Translate

Translate

Report

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 ,
May 29, 2015 May 29, 2015

Copy link to clipboard

Copied

After reviewing the tutorial again, I found out that the folder level code does not run correctly in a newer version of Acrobat. Use this instead:

var mySaveAs = app.trustedFunction(

  function(oDoc, cPath, cFlName) {

  // Ensure path has trailing "/"

  cPath = cPath.replace(/([^/])$/, "$1/");

  try {

  app.beginPriv();

  oDoc.saveAs(cPath + cFlName);

  app.endPriv();

  } catch (e) {

  app.alert("Error During Save - " + e);

  }

  }

);

And then, there is a syntax error in the code that needs to run in your document, but in addition to that, it is not using the folder level script correctly. Try to use this instead:

// 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, "newName.pdf");

} else {

  app.alert("Missing Save Function. Please contact forms administrator ");

}

Also, make sure that you are using device independent paths for your filenames. When doing this on a Windows system, you would not use "C:\directory\file.pdf", it would be "/c/directory/file.pdf".

Votes

Translate

Translate

Report

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
Explorer ,
May 29, 2015 May 29, 2015

Copy link to clipboard

Copied

Ok, still not working for me.

When I do your second code in my pdf, it gives me the alert error message..

I put your first code in all places I could think of, both appdata's, the folder itself and even in the pdf javascript area.  still not saving, still not renaming to 'newname.pdf'

what am I doing wrong ??

Votes

Translate

Translate

Report

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 ,
May 29, 2015 May 29, 2015

Copy link to clipboard

Copied

Which operating system and what version of Acrobat are you running?

Votes

Translate

Translate

Report

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
Explorer ,
May 29, 2015 May 29, 2015

Copy link to clipboard

Copied

I'm on Windows 7, Adobe Acrobat X - 10.1.14.11

I'm also at work, but I imagine these won't need network assistance...

Votes

Translate

Translate

Report

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 ,
May 29, 2015 May 29, 2015

Copy link to clipboard

Copied

The application level directory is either "C:\Program Files\Adobe\Acrobat 10.0\Acrobat\Javascripts" or "C:\Program Files (x86)\Adobe\Acrobat 10.0\Acrobat\Javascripts" (depending on whether you are running a 32bit or 64bit operating system). The user level directory is "C:\Users\<user name>\AppData\Roaming\Adobe\Acrobat\10.0\JavaScripts"

Did you install the script in either one of these? If the "JavaScripts" directory does not exist, you will have to create it.

Votes

Translate

Translate

Report

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
Explorer ,
May 29, 2015 May 29, 2015

Copy link to clipboard

Copied

ok so I put it in both locations, and pasted your code in my PDF javascript... nothing worked yet. starting to feel like a fool haha, I appreciate your help and patience!

  1. // determine the directory path for the current document 
  2. var directory = this.path.substring(0, this.path.lastIndexOf('/') +1); 
  3.  
  4.  
  5. if (typeof(mySaveAs) == "function") { 
  6.   mySaveAs(this, directory, "newName.pdf"); 
  7. } else
  8.   app.alert("Missing Save Function. Please contact forms administrator "); 


then i also did


  1. // determine the directory path for the current document
  2. var directory = this.path.substring(0, this.path.lastIndexOf('/') +1);
  3. if (typeof(mySaveAs) == "function") {
  4.   mySaveAs(this, //HA123/Shares/Marketing_bridge/Darin/`MasterTracker/EventRequestForms/, "newName.pdf");
  5. } else {
  6.   app.alert("Missing Save Function. Please contact forms administrator ");
  7. }

then i also tried

  1. // determine the directory path for the current document
  2. var directory = this.path.substring(0, this.path.lastIndexOf('/') +1);
  3. if (typeof(mySaveAs) == "function") {
  4.   mySaveAs(this, directory, "//HA123/Shares/Marketing_bridge/Darin/`MasterTracker/EventRequestForms/newName.pdf");
  5. } else {
  6.   app.alert("Missing Save Function. Please contact forms administrator ");
  7. }

p.s. I am going home for the weekend, I will return to this on Monday.  Have a great weekend and again, appreciate the help and patience with me on this!

Votes

Translate

Translate

Report

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
Explorer ,
Jun 01, 2015 Jun 01, 2015

Copy link to clipboard

Copied

Good morning,

anymore idea's for my problem?

Votes

Translate

Translate

Report

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 ,
Jun 01, 2015 Jun 01, 2015

Copy link to clipboard

Copied

Saying "nothing worked" isn't really helping us help you... What exactly is the result when you run the code? Are there any error messages in the console? Does it save the file at all but using the wrong name or in the wrong location? You need to be be more specific if you want to get any help...

Votes

Translate

Translate

Report

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
Explorer ,
Jun 01, 2015 Jun 01, 2015

Copy link to clipboard

Copied

good morning.

Sorry, I figured I gave enough information based on the entire thread, I will try and be more informative for you.

It does not save at all.  I make a change, hit the button and try to exit, and it still asks me to save.

Debugger says

Acrobat EScript Built-in Functions Version 10.0

Acrobat SOAP 10.0

TypeError: redeclaration of const mySaveAs

1:Field:Mouse Up

TypeError: redeclaration of const mySaveAs

1:Field:Mouse Up

code in the button is

  1. var mySaveAs = app.trustedFunction( 
  2.   function(oDoc, cPath, cFlName) { 
  3.   // Ensure path has trailing "/" 
  4.   cPath = cPath.replace(/([^/])$/, "$1/"); 
  5.   try { 
  6.   app.beginPriv(); 
  7.   oDoc.mySaveAs(cPath + cFlName); 
  8.   app.endPriv(); 
  9.   } catch (e) { 
  10.   app.alert("Error During Save - " + e); 
  11.   } 
  12.   } 
  13. );
  14. // determine the directory path for the current document 
  15. var directory = this.path.substring(0, this.path.lastIndexOf('/') +1); 
  16.  
  17.  
  18. if (typeof(mySaveAs) == "function") { 
  19.   mySaveAs(this, directory, "//HA123/Shares/Marketing_bridge/Darin/`MasterTracker/EventRequestFormsnewName123.pdf"); 
  20. } else { 
  21.   app.alert("Missing Save Function. Please contact forms administrator "); 
  22. }

Votes

Translate

Translate

Report

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 ,
Jun 01, 2015 Jun 01, 2015

Copy link to clipboard

Copied

The mySaveAs function needs to exist in the folder-level script. You should not declare it again in the code you're running from the console or button or whatever you use.

Votes

Translate

Translate

Report

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
Explorer ,
Jun 01, 2015 Jun 01, 2015

Copy link to clipboard

Copied

Ok I figured it out... I'll try and describe my efforts to get this to work, in-case someone else finds this on the web.  Thank you all who have helped me in this thread.

I pasted this code into a text file, saved it as a javascript (.js file), in the following locations Program Files > Adobe > Acrobat > Javascript, I also pasted it into the folder where I will be saving the file.

  1. var mySaveAs = app.trustedFunction( 
  2.   function(oDoc, cPath, cFlName) { 
  3.   // Ensure path has trailing "/" 
  4.   cPath = cPath.replace(/([^/])$/, "$1/"); 
  5.   try
  6.   app.beginPriv(); 
  7.   oDoc.saveAs(cPath + cFlName); 
  8.   app.endPriv(); 
  9.   } catch (e) { 
  10.   app.alert("Error During Save - " + e); 
  11.   } 
  12.   } 
  13. ); 

Then, I created a button, added a javascript to it.  the "this.getField's are looking for field names in my PDF, "RequestedBy" is a field in my PDF that has my sales managers name, it then adds a space beside it (+ " " +), then goes to the next field I want.  so when I fill out the pdf, it then saves it as SalesManager EventName June 1 2015.pdf. 

  1. // determine the directory path for the current document 
  2. var directory = this.path.substring(0, this.path.lastIndexOf('/') +1); 
  3. if (typeof(mySaveAs) == "function") { 
  4.   mySaveAs(this, directory, this.getField("RequestedBy").value + " " + this.getField("NameofEvent").value + " " + this.getField("EventStartmdyy").value + ".pdf"); 
  5. } else { 
  6.   app.alert("Missing Save Function. Please contact forms administrator "); 
  7. }

Hope that helps anyone else looking to do this, it's fairly easy once you know where to save the javascripts (and what to put in it), then edit another javascript code to meet your needs.

Thank you!

Votes

Translate

Translate

Report

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
Explorer ,
Jun 01, 2015 Jun 01, 2015

Copy link to clipboard

Copied

Actually one thing, maybe you can help with it?

I will be getting these forms via email, and I would like to open the form they return, and hit save, then it saves it into my directory.  Right now, I have to save this in the directory I want, then hit save, then it works as I want....as you can see it's an added step that I hope I can eliminate....

I tried messing around with the = this.path ... but didn't work.

Code;

  1. // determine the directory path for the current document 
  2. var directory = this.path.substring(0, this.path.lastIndexOf('/') +1);

directory I want it to be saved in;

\\HA123\Shares\Marketing_bridge\Darin\`MasterTracker\EventRequestForms

Votes

Translate

Translate

Report

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 ,
Jun 01, 2015 Jun 01, 2015

Copy link to clipboard

Copied

You need to find out the internal path of that folder and then specify it as the second parameter for mySaveAs.

The best way to do that is to open a PDF file that's inside that folder, and then run this command in the JS console:

this.path

Then copy the first part of the output, until the file-name, and use it directly in your code.

It will probably be something like this:

/HA123/Shares/Marketing_bridge/Darin/`MasterTracker/EventRequestForms/

Votes

Translate

Translate

Report

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
Explorer ,
Jun 01, 2015 Jun 01, 2015

Copy link to clipboard

Copied

ok I've tried a bunch of things, and haven't got it to work. I tried pasting it in the javascript folder level, tried inside the code...nothing...

Where exactly do you paste that internal? I knew to switch the /'s ... but not sure exactly where. can you post an example of the code?

appreciate it

Votes

Translate

Translate

Report

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 ,
Jun 01, 2015 Jun 01, 2015

Copy link to clipboard

Copied

var directory = "/HA123/Shares/Marketing_bridge/Darin/`MasterTracker/EventRequestForms/";

var fileName = "newFile.pdf";

mySaveAs(this, directory, fileName);

Votes

Translate

Translate

Report

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
Explorer ,
Jun 01, 2015 Jun 01, 2015

Copy link to clipboard

Copied

hmm, not working, gives me an error

"Error during Save - UnsupportedValueError: Value is unsupported. ======> Parameter cPath

my code , I've also tried the directory without the + "/"; ...and I have also tried adding a / after the /1        -      neither worked.

  1. // determine the directory path for the current document 
  2. var directory = "//HAUCACAM0007VS/Shares/Marketing_bridge/Darin/`MasterTracker/EventRequestForms/1" + "/"; 
  3. if (typeof(mySaveAs) == "function") { 
  4.   mySaveAs(this, directory, this.getField("RequestedBy").value + " " + this.getField("NameofEvent").value + " " + this.getField("EventStartmdyy").value + ".pdf"); 
  5. } else { 
  6.   app.alert("Missing Save Function. Please contact forms administrator "); 
  7. }

I have no added/changed anything in the Javascript.

can you see where I went wrong ?

Votes

Translate

Translate

Report

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 ,
Jun 01, 2015 Jun 01, 2015

Copy link to clipboard

Copied

It's likely the file-name contains invalid characters.

Votes

Translate

Translate

Report

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
Explorer ,
Jun 01, 2015 Jun 01, 2015

Copy link to clipboard

Copied

All letters/spaces/numbers.

I took out the spaces and still nothing.  so after I changed the code from

  1. var directory = this.path.substring(0, this.path.lastIndexOf('/') +1);

to

  1. var directory = "//HAUCACAM0007VS/Shares/Marketing_bridge/Darin/`MasterTracker/EventRequestForms/1";

something went wrong.... the folders exist.

Votes

Translate

Translate

Report

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 ,
Jun 01, 2015 Jun 01, 2015

Copy link to clipboard

Copied

Add a slash at the end of the directory variable.

Votes

Translate

Translate

Report

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