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

Help with Save As using JavaScript

Explorer ,
Nov 30, 2017 Nov 30, 2017

Copy link to clipboard

Copied

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

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

TOPICS
Acrobat SDK and JavaScript , Windows

Views

16.4K

Likes

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 , Jan 10, 2018 Jan 10, 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 cl

...

Likes

Translate

Translate
Community Expert ,
Nov 30, 2017 Nov 30, 2017

Copy link to clipboard

Copied

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

Likes

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 ,
Dec 01, 2017 Dec 01, 2017

Copy link to clipboard

Copied

Point 3 :

app.execMenuItem("SaveAs");

Point 5 :

JavaScript can close the document but cannot quit Acrobat.

Likes

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 ,
Dec 04, 2017 Dec 04, 2017

Copy link to clipboard

Copied

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.

Likes

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 ,
Dec 04, 2017 Dec 04, 2017

Copy link to clipboard

Copied

No. It's essentially the same as clicking the Save As command under the File menu of the application.

Likes

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 ,
Dec 04, 2017 Dec 04, 2017

Copy link to clipboard

Copied

So is there any way to bring up the Save As box AND pre fill the save name and save location?

Or is it a silent save as I have done in the above code OR what you describe with the app.execMenuItem("SaveAs"); which is essentially clicking the save as button in the menu? which wont pre fill the save name and location?

Likes

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 ,
Dec 04, 2017 Dec 04, 2017

Copy link to clipboard

Copied

So is there any way to bring up the Save As box AND pre fill the save name and save location?

No.

Or is it a silent save as I have done in the above code OR what you describe with the app.execMenuItem("SaveAs"); which is essentially clicking the save as button in the menu? which wont pre fill the save name and location?

Yes, those are your only options.

Likes

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 ,
Dec 04, 2017 Dec 04, 2017

Copy link to clipboard

Copied

Hmm well that's a bit annoying.. but ultimately good to confirm.. I'm going to use a work around to point 4 by making each file name unique by including a time stamp (date and time fields). YYYYMMDDHHMM or something like that.

Likes

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 ,
Dec 05, 2017 Dec 05, 2017

Copy link to clipboard

Copied

For saving I use a script that displays an alert box containing the new unique name before displaying the Save As dialog box.

So, the end user can copy-paste the new unique name from the alert box to the Save As dialog box.

Here is a sample, you just have to adjust the "strFixe" var (line 2), the "Name" field name (line 3), and the "Date" field name (line 4):

/******************************************************/

var strFixe = "Note-de-frais";

var strNom = this.getField("NOM").value;

var strNum = this.getField("DATE").value

/* language detection */

if (app.language == "FRA") {

    var strTitre = strFixe + " - Enregistrement";

    var strQuestion = "COPIEZ le nom propos\u00E9 ci-dessous et COLLEZ le dans la fen\u00EAtre suivante pour l'utiliser comme nom de fichier :";

} else {

    var strTitre = strFixe + " - Save As";

    var strQuestion = "COPY below proposed text and PASTE it in the following box to use it as file name:";

}

/* new file name concatenation */

var strDisplay = strNum + "_" + strNom + "_" + strFixe;

/* alert box */

var saveRep = app.response({cQuestion: strQuestion, cTitle: strTitre, cDefault: strDisplay,});

if (saveRep != null) {

    /* Acrobat Pro detection */

    if (app.viewerVariation == "Full") {

        /* edit the document Title */

        this.info.Title = strNum + " - " + strNom + " - " + strFixe;

    }

    /* hide the target button and save */

    event.target.display = display.hidden; // optional

    app.execMenuItem("SaveAs");

}

/******************************************************/

Likes

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 ,
Dec 13, 2017 Dec 13, 2017

Copy link to clipboard

Copied

Hi JR,

Quick question I tried this on DC Pro and it worked! yay

I tried it on a different PC with Adobe Reader DC and it doesn't work as intended. I can copy the text but as soon as I click Ok it wipes what I have saved from my clipboard..

Any thoughts?

Cheers,
Seb.

Likes

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 ,
Dec 01, 2017 Dec 01, 2017

Copy link to clipboard

Copied

Here is an article on the topic: https://acrobatusers.com/tutorials/how-save-pdf-acrobat-javascript

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Likes

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 ,
Dec 16, 2017 Dec 16, 2017

Copy link to clipboard

Copied

No, hélas.

I never got this issue.

Likes

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 ,
Jan 10, 2018 Jan 10, 2018

Copy link to clipboard

Copied

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

Likes

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 ,
Jan 11, 2018 Jan 11, 2018

Copy link to clipboard

Copied

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 PDFScripting
Use the Acrobat JavaScript Reference early and often

Likes

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 ,
Oct 22, 2022 Oct 22, 2022

Copy link to clipboard

Copied

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

Likes

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 ,
Oct 22, 2022 Oct 22, 2022

Copy link to clipboard

Copied

Look at myTrustedSpecialTaskFunc

 

Likes

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 ,
Oct 22, 2022 Oct 22, 2022

Copy link to clipboard

Copied

LATEST

Please explain what you want to do on a new post.

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Likes

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
New Here ,
Jul 25, 2018 Jul 25, 2018

Copy link to clipboard

Copied

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

Likes

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 ,
Jul 25, 2018 Jul 25, 2018

Copy link to clipboard

Copied

Not possible. Acrobat JS can't create folders.

Likes

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 ,
Jul 25, 2018 Jul 25, 2018

Copy link to clipboard

Copied

Acrobat JavaScript can't create folders.

Likes

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
New Here ,
Jul 25, 2018 Jul 25, 2018

Copy link to clipboard

Copied

How about just saving the file name with the forms contents. Once the file name is created then they can just copy and past and create a folder and then insert the file within that. Thanks

Likes

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 ,
Jul 25, 2018 Jul 25, 2018

Copy link to clipboard

Copied

Yes, that's possible, but it would require installing a script on the local computer of each user. See earlier replies in this thread for more details.

Likes

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
New Here ,
Jul 25, 2018 Jul 25, 2018

Copy link to clipboard

Copied

How do i go about creating a JS Script for users don't understand that part next thing is it's a Shared PDF on Microsoft Cloud but they can save to there local computer. ALso what's Trusted Level Function. THanks

Likes

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 ,
Jul 25, 2018 Jul 25, 2018

Copy link to clipboard

Copied

Likes

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
New Here ,
Jul 25, 2018 Jul 25, 2018

Copy link to clipboard

Copied

Thanks so much try67 i'll look into all of this shortly.

Likes

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