Skip to main content
sebastianb99835257
Inspiring
Dec 1, 2017 4:19 AM UTC
Answered

Help with Save As using JavaScript

Hi All,

This is my first attempt at using Javascript...

I am attempting to create a Save As button on a form that will:

  • Create a file name based on fields;
  • Save to a specific folder (And if it doesn't exist then create folder);
  • Bring up the "Save As" box for confirmation (as opposed to a silent save);
  • Bring up a warning IF Adobe is going to save over an existing pdf (aka has the exact same name);
  • Close Adobe after all of the above is executed.

So far I have been fairly successful. I have been able to do a silent save, saving to a specific location and saving the file name based on fields. I need help building the rest of the functionality into the code if possible.

Thanks in advanced!

-------------------------------------------------------------------------------------------------------------------------

The code I have so far is:

Trusted Level Function

saved in Notepad under the Adobe / Javascript folder

mySaveAs = app.trustPropagatorFunction(function(doc,path) {

app.beginPriv();

doc.saveAs(path);

app.endPriv();

})

myTrustedSpecialTaskFunc = app.trustedFunction(function(doc,path) {

// Privileged and/or non-privileged code above

app.beginPriv();

mySaveAs(doc,path);

app.endPriv();

// Privileged and/or non-privileged code below

});

PDF Button

Code under button that is executed on Mouse up click

// build file name

var myFileName = getField("Work_Pack").value + " - " + getField("Form_Name").value + " - " + getField("Todays_Date").value + ".pdf";

// add folder name

myFileName = "/c/temp/Saved Forms/" + myFileName

myTrustedSpecialTaskFunc(this, myFileName);

this.closeDoc() ;

-------------------------------------------------------------------------------------------------------------------------

This topic is closed to new replies. Start a new post to keep the conversation going.
sebastianb99835257
sebastianb99835257Correct answer
Inspiring
Jan 11, 2018 5:25 AM UTC

FYI for those interested I used the following:

Trusted Level Function

saved in Notepad under the Adobe / Javascript folder

mySaveAs = app.trustPropagatorFunction(function(doc,path) {

app.beginPriv();

doc.saveAs(path);

app.endPriv();

})

 

myTrustedSpecialTaskFunc = app.trustedFunction(function(doc,path) {

// Privileged and/or non-privileged code above

app.beginPriv();

mySaveAs(doc,path);

app.endPriv();

// Privileged and/or non-privileged code below

});

PDF Button

Code under button that is executed on Mouse up click

// build file name

var myFileName = getField("SAP_Work_Order").value + " - " + getField("Form_Name").value + " - " + getField("Todays_Date").value + ".pdf";

// add folder name

myFileName = "/c/Offline Forms/" + myFileName

myTrustedSpecialTaskFunc(this, myFileName);

app.alert("This Form (" + getField("SAP_Work_Order").value + " - " + getField("Form_Name").value + " - " + getField("Todays_Date").value + ") has saved successfully to the following location: /C/Offline Forms/" , 3);

Further Explanations

The "Todays_Date" field has the following:

  • Add in Document level JavaScript > Script Name = Todays_Date > Add > getField("Todays_Date").value = util.printd("yyyymmddHHMM", new Date()); > Ok > Close
  • field Script > this.getField("Today").value = util.printd("mm/dd/yyyy", new Date());

The above code allowed me to fill most of my requirements to a point that I was happy with:

  • Create a file name based on fields; YES
  • Save to a specific folder (And if it doesn't exist then create folder); YES/NO (Have to save to a specific hard coded folder)
  • Bring up the "Save As" box for confirmation (as opposed to a silent save); YES - Have to do silent save but by using a pop up box I can bring up a message saying the file saved successfully and the location it saved.
  • Bring up a warning IF Adobe is going to save over an existing pdf (aka has the exact same name); YES/NO - Using the "Todays_Date" field which gives a time stamp down to the minute ensures that no file gets accidentally saved over silently.
  • Close Adobe after all of the above is executed. I didn't actually do this in the end but I think the following function works "this.closeDoc(true);"

26 replies

Participating Frequently
Jul 25, 2018 11:00 PM UTC

Try67 please let me know if you can help me with the file i have i'm willing to pay. Thanks

Translate
try67
Community Expert
Community Expert
Jul 26, 2018 9:19 AM UTC

Yes. You can contact me via try6767 at gmail.com .

Translate
Participating Frequently
Jul 25, 2018 4:05 PM UTC

I would like to create a JS that will save the PDF document creating a specific folder as well as naming that folder and inserting the PDF in there. The name of the folder and the file will be pulled from information populated within the form. Thanks

Translate
try67
Community Expert
Community Expert
Jul 25, 2018 4:11 PM UTC

Not possible. Acrobat JS can't create folders.

Translate
sebastianb99835257
sebastianb99835257AuthorCorrect answer
Inspiring
Jan 11, 2018 5:25 AM UTC

FYI for those interested I used the following:

Trusted Level Function

saved in Notepad under the Adobe / Javascript folder

mySaveAs = app.trustPropagatorFunction(function(doc,path) {

app.beginPriv();

doc.saveAs(path);

app.endPriv();

})

 

myTrustedSpecialTaskFunc = app.trustedFunction(function(doc,path) {

// Privileged and/or non-privileged code above

app.beginPriv();

mySaveAs(doc,path);

app.endPriv();

// Privileged and/or non-privileged code below

});

PDF Button

Code under button that is executed on Mouse up click

// build file name

var myFileName = getField("SAP_Work_Order").value + " - " + getField("Form_Name").value + " - " + getField("Todays_Date").value + ".pdf";

// add folder name

myFileName = "/c/Offline Forms/" + myFileName

myTrustedSpecialTaskFunc(this, myFileName);

app.alert("This Form (" + getField("SAP_Work_Order").value + " - " + getField("Form_Name").value + " - " + getField("Todays_Date").value + ") has saved successfully to the following location: /C/Offline Forms/" , 3);

Further Explanations

The "Todays_Date" field has the following:

  • Add in Document level JavaScript > Script Name = Todays_Date > Add > getField("Todays_Date").value = util.printd("yyyymmddHHMM", new Date()); > Ok > Close
  • field Script > this.getField("Today").value = util.printd("mm/dd/yyyy", new Date());

The above code allowed me to fill most of my requirements to a point that I was happy with:

  • Create a file name based on fields; YES
  • Save to a specific folder (And if it doesn't exist then create folder); YES/NO (Have to save to a specific hard coded folder)
  • Bring up the "Save As" box for confirmation (as opposed to a silent save); YES - Have to do silent save but by using a pop up box I can bring up a message saying the file saved successfully and the location it saved.
  • Bring up a warning IF Adobe is going to save over an existing pdf (aka has the exact same name); YES/NO - Using the "Todays_Date" field which gives a time stamp down to the minute ensures that no file gets accidentally saved over silently.
  • Close Adobe after all of the above is executed. I didn't actually do this in the end but I think the following function works "this.closeDoc(true);"
Translate
Thom Parker
Community Expert
Community Expert
Jan 11, 2018 4:59 PM UTC

I don't think anyone pointed this out to you, but the "app.trustPropagatorFunction" function doesn't do anything. You don't need it. You have one nested function too many.

Translate
Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Known Participant
Oct 22, 2022 5:11 PM UTC

Then what will be the better script for trusted level function. Thanks

Translate
JR Boulay
Community Expert
Community Expert
Dec 16, 2017 9:59 AM UTC

No, hélas.

I never got this issue.

Translate
Acrobate du PDF, InDesigner et Photoshopographe
Thom Parker
Community Expert
Community Expert
Dec 1, 2017 5:37 PM UTC
Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Bernd Alheit
Community Expert
Community Expert
Dec 1, 2017 6:39 AM UTC

Points 3 and 5 are not possible. Info to point 2: Acrobat JavaScript can't create folders.

Translate
JR Boulay
Community Expert
Community Expert
Dec 1, 2017 9:31 AM UTC

Point 3 :

app.execMenuItem("SaveAs");

Point 5 :

JavaScript can close the document but cannot quit Acrobat.

Translate
Acrobate du PDF, InDesigner et Photoshopographe
sebastianb99835257
Inspiring
Dec 4, 2017 10:45 PM UTC

Hi JR_Boulay,

Can the app.execMenuItem("SaveAs"); function be inserted into the code to bring up the “Save As” box along with the predefined saving location and save name that I have coded above?

Cheers,

Seb.

Translate