These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Javascript for Trusted function Save As with generated name from fields

amographics
Registered: Nov 23 2010
Posts: 4
Answered

I am trying to perform a "Save As" function that will automatically create a name of the file from fields of the form (LastName and FirstName)
I have read through many tutorials and forums, but just get more confused.
I currently have the following:
 
// SaveAs Function
var mySaveFileName = app.trustedFunction(function(doc) {
 
app.beginPriv();
var myFileName = "LastName" + "FirstName" + ".pdf";
doc.saveAs(myFileName);
app.endPriv();
 
});
  
with the following on my button:
mySaveFileName(myFileName);
  
this is about the fourth option of different code that I have tried and nothing has worked.
Any help with exact code would be much appreciated.
thank you

My Product Information:
Acrobat Pro 9.0, Macintosh
George_Johnson
Online
Expert
Registered: Jul 6 2008
Posts: 1876
See the first example in the following Acrobat JavaScript documentation page:

http://livedocs.adobe.com/acrobat_sdk/9.1/Acrobat9_1_HTMLHelp/wwhelp/wwhimpl/js/html/wwhelp.htm?href=JS_API_AcroJS.88.1.php#1515776&accessible=true
amographics
Registered: Nov 23 2010
Posts: 4
I"m sorry but, which section under that link - that just took me to the main JavaScript API section. thank you

George_Johnson
Online
Expert
Registered: Jul 6 2008
Posts: 1876
Sorry about that, the correct link is: http://livedocs.adobe.com/acrobat_sdk/9.1/Acrobat9_1_HTMLHelp/JS_API_AcroJS.88.167.php

This is to the app.trustPropagatorFunction documentation.
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
When you use the saveAs function you need to provide the variable for the doc object and file path/name.

Your folder level function:

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


Your button code:

// build file name
var myFileName = "LastName" + "FirstName" + ".pdf";
// add folder name
myFileName = "/c/temp/" + myFileName
myTrustedSpecialTaskFunc(this, myFileName);

You must have the folder c:\temp on your system. If you use another folder it must exist and be a safe path folder.

See the section "trustPropagatorFunction" in the Acrobat JS API Reference.

George Kaiser

amographics
Registered: Nov 23 2010
Posts: 4
thank you - that worked to rename the file, but it named it actually "LastNameFirstName.pdf" rather than building the file name out of the "LastName" and "FirstName" fields on the form.
How do I specify that?
thank you for all of your help - this is the furthest I've gotten so far!

try67
Expert
Registered: Oct 30 2008
Posts: 2398
Accepted Answer
Use this:
var myFileName = doc.getField("LastName").value + doc.getField("FirstName").value + ".pdf";

Be aware, though, that if the user entered illegal characters such as \ / < > : ? | or even a comma, the saveAs command will fail.

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

amographics
Registered: Nov 23 2010
Posts: 4
thanks so much for all of your help -
I ended up changing doc.getField to this.getField and it worked great - I received an error of:

ReferenceError: doc is not defined
when I placed in doc.getField


mibu1mt
Registered: Mar 3 2011
Posts: 17
gkaiseril wrote:
When you use the saveAs function you need to provide the variable for the doc object and file path/name.Your folder level function:

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


Your button code:

// build file name
var myFileName = "LastName" + "FirstName" + ".pdf";
// add folder name
myFileName = "/c/temp/" + myFileName
myTrustedSpecialTaskFunc(this, myFileName);

You must have the folder c:\temp on your system. If you use another folder it must exist and be a safe path folder.

See the section "trustPropagatorFunction" in the Acrobat JS API Reference.
I tried out this code and get error in the console:
"TypeError: doc.saveAs is not a function
3:Folder-Level:App:pdfsave.js"

I use a folder level .js file to execute the code. I have no idea why it isn't working.

Please help. Thank You.



try67
Expert
Registered: Oct 30 2008
Posts: 2398
The last part of the code can't be at the folder-level. It's written as a field or document-level code.

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

mibu1mt
Registered: Mar 3 2011
Posts: 17
Thank you very much, that was quick.
Understood the problem, now it works.