Skip to main content
sebastianb99835257
Inspiring
December 1, 2017
Answered

Help with Save As using JavaScript

  • December 1, 2017
  • 6 replies
  • 31484 views

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 has been closed for replies.
Correct answer sebastianb99835257

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);"

6 replies

Participating Frequently
July 25, 2018

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

try67
Community Expert
Community Expert
July 26, 2018

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

Participating Frequently
July 25, 2018

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

try67
Community Expert
Community Expert
July 25, 2018

Not possible. Acrobat JS can't create folders.

sebastianb99835257
sebastianb99835257AuthorCorrect answer
Inspiring
January 11, 2018

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);"
Thom Parker
Community Expert
Community Expert
January 11, 2018

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.

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Known Participant
October 22, 2022

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

JR Boulay
Community Expert
Community Expert
December 16, 2017

No, hélas.

I never got this issue.

Acrobate du PDF, InDesigner et Photoshopographe
Thom Parker
Community Expert
Community Expert
December 1, 2017
Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Bernd Alheit
Community Expert
Community Expert
December 1, 2017

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

JR Boulay
Community Expert
Community Expert
December 1, 2017

Point 3 :

app.execMenuItem("SaveAs");

Point 5 :

JavaScript can close the document but cannot quit Acrobat.

Acrobate du PDF, InDesigner et Photoshopographe
sebastianb99835257
Inspiring
December 4, 2017

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.