Save As Function PDF
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

I would like to create a save as script within a PDF document. I know this will require placing a button on the document that calls the script as well as a trusted script that has to be placed in a folder, but I have no idea beyond that.

What I would want is for the script to collect the input from 5 different text fields and bring them together separated by a "." to create the file name.

example: textfield1.textfield2.textfield3.textfield4.textfield5.pdf

I would want the script to save the new file in the same location that the current document would be housed in. example: if the original document is in the my documents folder then I want the file saved by the script to be saved there as well, if I move the original document to the desktop, then I want the file the script saves to be saved to the desktop as well.

I would also like the script to prompt to save and warn me if there is another file with the same name. I do not want it to be a silent save if possible.

So all told, I need the folder level script created based on the options above, as well as the script that I believe has to be attached to a button in the document.

Thanks in advance, let me know if you need more info.

What kind of documents you're using? Simple html pages? If yes, are fields simple inputs?
Chlegou 14 days ago
7 days ago
Tags
javascript

Crowdsource coding tasks.

2 Solutions


In mashtullah's solution, I believe you want a confirmation if the file exists, and then the user can select whether they want to continue with the saving or not. In that case, you can replace the last if statement with this one

if(checkFlag){
    if (app.alert({cMsg: "File Already Exists. Save anyway?", nIcon: 1, cTitle: "File Exists", nType: 2}) == 4){
          this.saveAs(newPath);      
    }
}   
else{
    this.saveAs(newPath);
}

Am not sure if i understood your question well but try out the code below.

put this in your document script...

function doSaveFile() {

 if (app.alert({cMsg: "Do you wish to save this document?",nIcon:2, nType: 3}) == 4){
// build file name
var newFileName = this.getField("textfield1").value + "." + this.getField("textfield2").value + "." 
+ this.getField("textfield3").value+"."+this.getField("textfield4").value+ "." +this.getField("textfield5").value+".pdf";

// Split the current path into an array so that we can remove the current file name
    var curPath = this.path.split("/");

    // Remove current file name
    curPath.pop();

    // Add our new file name to the array
    curPath.push(NewFileName);

    // Glue back together our array to get our new path
    var newPath=curPath.join("/");

    app.beginPriv();  
    var checkFlag = app.openDoc({ cPath: newPath, bHidden: true }); 

    if(checkFlag)
    {
        app.alert({cMsg: "File already exists...",cTitle: " File save error"});
    }
    else
    {
        this.saveAs(newPath);
    }
   app.endPriv();
}

}

put this in your button's mouseUp event...

doSaveFile();

Incase of anything let me know.

Do I have to save the script file as "doSaveFile"?
Santiman21 10 days ago
This doesn't seem to be working. It asks me if I want to save but then doesn't save anything
Santiman21 10 days ago
I put the script into the document level and the other script under the button. But doesn't seem to actually save anywhere.
Santiman21 10 days ago
Sorry, the saveAs function has to be run from a privileged context, i had placed app.endPriv(); before saving... Try the edited code now
mashtullah 9 days ago
Still not doing anything after it asks me to if I want to save. My understanding of scripting is very basic, but wouldn't this have to be a folder level script for it to run from a trusted context? I was under the impression document level scripts couldn't do that.
Santiman21 9 days ago
Try folder level also check the windows user permissions, are you running acrobat with administrator previlages
mashtullah 9 days ago
I'm not able to run with administrator privilege. How do I convert it to a folder level script? I know I have to save it in a file with the extension .js but what do I name the file and how do I make the button call that file?
Santiman21 9 days ago
I have all window user permissions enabled. Still nothing. Don't know how to setup as a folder level script.
Santiman21 8 days ago
View Timeline