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

How to "SaveAs" with filename generated from form field?

radzmar
Expert
Registered: Nov 3 2008
Posts: 1202
Answered

Hi,

since month I'm looking for a solution for following problem.
I'm designing forms with livecycle designer that uses a "Save-Button" with the script

app.execMenuItem("SaveAs")

in the click event.
Works fine so far after enabling the reader extentions.

But is there a way to generate a new filename by one of the forms fields?
Cause the users now often overwrites the original form, if they don't rename the file.

I found several discussions about in this forum, but often for acrobat not designer.
Cause of complex dynamic forms I only preffer designer for creating the forms.

Any solution known?
I also tried some examples for the acrobat like

var myDoc =
event.target;
myDoc.saveAs();

+

a trusted function in the config.js

but this script never works, I don't know how.

I read the Disigner Scripting Basic Manual and some of the others, too, but did not find a solution.
Maybe I misunderstood some declarations cause my native language is german and it's hard for me reading technical docs in english :-/

Can anyone help?
I preffer a solution for designer.

THANKS IN ADVANCE
radzmar

radzmar
LoveCycle Blog
Documents you need:
LiveCycle Designer ES2 Docs

My Product Information:
LiveCycle Designer, Windows
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1875
Did you see this discussion: http://www.acrobatusers.com/forums/aucbb/viewtopic.php?id=16792

The person was using a name derived from the current date/time, but you would use the same general approach if the file name/path is based on a field value.

If you post some code that's not working, we may be able to suggest changes.

George
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
The AcroForm "saveAs()" fucntion will work when run from a trusted function. In fact, this is the only way you can perform this operation.

If this function is run from a folder level there is no need to use "event.target" as the document object. Folder level scripts are already in the AcroForm document context, so "this" will work, and they do not receive XFA events, so "event.target" does not mean the same thing.

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/]http://www.adobe.com/devnet/acrobat/[/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

radzmar
Expert
Registered: Nov 3 2008
Posts: 1202
George_Johnson wrote:
Did you see this discussion: http://www.acrobatusers.com/forums/aucbb/viewtopic.php?id=16792The person was using a name derived from the current date/time, but you would use the same general approach if the file name/path is based on a field value.

If you post some code that's not working, we may be able to suggest changes.

George
Well,

I've just tried this example from the forum in the config.js without great changings to see how it works:

//DateTime function
function myDateString()
{

return util.printd("yyyymmdd_HHMMss", new Date());

}

// SaveAs Function
var mySaveDoc = app.trustedFunction(function(doc) {

app.beginPriv();
var myPath = app.getPath("user", "documents") + myDateString() + ".pdf";

// saveAs is the only privileged code that needs to be enclosed
// with beginPriv/endPriv
doc.saveAs(myPath);
app.endPriv();

});

and put this JS on the mouse up event of a button in livecycle designer (dynamic xml-form), saved the file and opened it wit acrobat pro.

mySaveDoc(event.target);

result is nothing, not even an error message, so I don't know what happend or not.

???

radzmar
LoveCycle Blog
Documents you need:
LiveCycle Designer ES2 Docs

George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1875
There are a few things to look at. First, did you confirm that no new document was created in your "My Document" folder?

Also, when making changes to folder-level JavaScripts, you need to close and then re-open Acrobat to reload the code you have modified.

George
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Also, did you check the console window for errors?

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1875
> var myPath = app.getPath("user", "documents") + myDateString() + ".pdf";This line should be:

var myPath = app.getPath("user", "documents") + "/" + myDateString() + ".pdf";
George
radzmar
Expert
Registered: Nov 3 2008
Posts: 1202
thomp wrote:
Also, did you check the console window for errors?
Hi,

the console reports

mySaveDoc is not defined
1:XFA:Formular1[0]:Seite1[0]:Header[0]:SaveAs[0]:mouseUp

when I click the SaveAs-Button in the form.
In the target folder is no new file generated.

radzmar
LoveCycle Blog
Documents you need:
LiveCycle Designer ES2 Docs

thomp
Expert
Registered: Feb 15 2006
Posts: 4411
I tried out the code and It's working for me. From a LC form I tried two variations.

mySaveDoc(event.target);

and

event.target.mySaveDoc(event.target);

The second variation forces the call to "mySaveDoc" into the context of the document object, where all globally defined functions are availible. Without this prefix, Acrobat searches for the function in the local context. So you may be suffering from an execution context issue.

However I thinks its more likely that either the "config.js" file is located in the wrong place, or that there is an error in the function definition. It's easy enough to find out. In the console window, run this single line:

mySaveDoc

This shoulld return the function definition. If it returns undefined then the "mySaveDoc" does not exits at all.

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/]http://www.adobe.com/devnet/acrobat/[/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

radzmar
Expert
Registered: Nov 3 2008
Posts: 1202
Hello again,

after some trouble with the designer that had to be solved first, it's now working fine with the "mydatestring".
Thanks for your help so far!!!

now, I'm trying to add a variable generated from a fieldvalue.
I used

var customer = this.getField("customer").value;

in the config.js within the beginPriv/endPriv pair but it noting happens.

The JS console reports

this.getField("customer") has no properties

Maybe my JS-understanding is worst :-/
Hope for your help

radzmar
LoveCycle Blog
Documents you need:
LiveCycle Designer ES2 Docs

George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1875
It's hard to tell without seeing the exact code you're using. If the function in question has the document object passed to it, you should do someting like:

var customer = doc.getField("customer").value;



George
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Isn't this a LiveCycle form, "getField()" is an AcroForm function. It'll work for a static XFA, but not for a dynamic XFA form. If you need a value from a form field, it would be better to grab that value in the form script that calls "mySaveDoc()" and pass it in as an argument.

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/]http://www.adobe.com/devnet/acrobat/[/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

radzmar
Expert
Registered: Nov 3 2008
Posts: 1202
ok thomp,

this differences between Acrobat and Designer scripts confusing me often.
I read some articles in the manual and tried following script at the mouse up event in designer, cause resolveNode seems to be the "designers version" of this.getfield:
======================================================
var customer = resolveNode("RechKunde");
event.target.mySaveDoc(event.target);
======================================================


And the config.js look this way.
======================================================
//Datumsfunktion
function myDateString()
{

return util.printd("dd.mm.yyyy_HH.MM.ss", new Date());

}


// Speicherfunktion
var mySaveDoc = app.trustedFunction(function(doc) {

app.beginPriv();
var myPath = app.getPath("user", "documents") + "/" + "Dokumentname" + " " + myDateString() + customer + ".pdf";

// saveAs is the only privileged code that needs to be enclosed
// with beginPriv/endPriv
doc.saveAs(myPath);
app.endPriv();

});
======================================================


When I check the console it reports on mouse up event
======================================================
xfa is not defined
14:XFA:Formular1[0]:Seite1[0]:Header[0]:SaveAs[0]:mouseUp
======================================================


It doesn't work so far.
I think the way of defining the variable customer I did is wrong, but I don't figure it out.

radzmar
LoveCycle Blog
Documents you need:
LiveCycle Designer ES2 Docs

radzmar
Expert
Registered: Nov 3 2008
Posts: 1202
I got it! Yes. ;-)

I defined customer as global variable (without var!) and the whole path to the textfield "RechFirma" for the mouse up event.

IT WORKS!

======================================================
customer = resolveNode("Form1.Page1.Body.Subform1.RechFirma").rawValue;
event.target.mySaveDoc(event.target);
======================================================

Thanks for your support!

radzmar
LoveCycle Blog
Documents you need:
LiveCycle Designer ES2 Docs

thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Well and good, if it works then you're there!! Good Job!

However, you would be better off in the long run passing "customer" into the "mySaveDoc" function as an argument.

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/]http://www.adobe.com/devnet/acrobat/[/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

radzmar
Expert
Registered: Nov 3 2008
Posts: 1202
Hello again,

while the save function with the folder level JS is working fine for me I'm trying to use it for several documents now.
The problem is, that the script renames every file I used the mySaveAs function for in the same way.

Is there a way to select the function of the folder level JS by filename?

I put following script to the folder level

======================================================
//Date function
function myDateString()
{

return util.printd("dd.mm.yyyy"+" - "+"HH.MM.ss", new Date());

}


//Filename selection
var aDocumentFileName = this.documentFileName;

if aDocumentFileName = "TEST.pdf"

// Save function
var mySaveDoc = app.trustedFunction(function(doc) {

app.beginPriv();

var myPath = app.getPath("user", "documents") + "/" + "Filename" + " - " + myDateString() + " - " + Kunde + " - " + Maschine + " - " + SN +".pdf";

// saveAs is the only privileged code that needs to be enclosed
// with beginPriv/endPriv
doc.saveAs(myPath);
app.endPriv();

});
======================================================

But now the console reports

missing ( before condition
13:Folder-Level:User:MyScripts.js
event.target.mySaveDoc is not a function
4:XFA:Formular1[0]:Seite1[0]:Header[0]:SaveAs[0]:mouseUp

Any hints for me?
Thanks in advance

radzmar
LoveCycle Blog
Documents you need:
LiveCycle Designer ES2 Docs

gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
The console message is referring to line 13 of your folder level script, and telling you there is a missing "(" in that line of code.

Your 13th line of code reads:
 if aDocumentFileName = "TEST.pdf"
A check of the syntax requirements of the "if" statement in JavaScirpt requires that the statement to be evaluated must be within parenthesis. Also the comparison operators are "==" or "===" and not the set operator, "=".

So your line of code should read:
 if (aDocumentFileName == "TEST.pdf")
You may also need to denote the begriming and ending block of code to run when this statement is true. Without the block makers, only the immediate following line of code is run when the statement evaluates to true.

George Kaiser

radzmar
Expert
Registered: Nov 3 2008
Posts: 1202
gkaiseril wrote:
You may also need to denote the begriming and ending block of code to run when this statement is true. Without the block makers, only the immediate following line of code is run when the statement evaluates to true.
Thanks for this quick response,

the console now reports

event.target.mySaveDoc is not a function
4:XFA:Formular1[0]:Seite1[0]:Header[0]:SaveAs[0]:mouseUp

I think you trying to tell me, what i have to add to the script, but my english knowledge (I'm german ;-) ) collapes by your statement above, sorry.

...denote the begriming...

Can you explain it in other word for me please!
Do you mean () or {}?

radzmar
LoveCycle Blog
Documents you need:
LiveCycle Designer ES2 Docs

gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
The "{" denotes a beginning of a block of JS code and "}" denotes the end of the block of code.


You have an "if" statment written to execute one line of code and it appears to execute a comment statment.

So if we look at line 4:
var aDocumentFileName = this.documentFileName
It appears you are trying to access an open PDF during the initialization of the appliction. If Acrobat/Reader is closed and you double click on a PDF, the application will need to perform all initialization actions including processing all folder leve JavaScripts before opening the selected PDF. So there is no opened PDF and thus there is no available document objet.

It appears your "if" statement is in the wrong place and does not block out the code to be executed when the statement is true. I would expect this code to be either with the event calling your save as function or as part of the function.

George Kaiser

radzmar
Expert
Registered: Nov 3 2008
Posts: 1202
OK thanks for the hint gkaiseril!!!

In the folder level JS I wrote
======================================================
//Date function
function myDateString()
{
return util.printd("dd.mm.yyyy"+" - "+"HH.MM.ss", new Date());
}


//Save function
var mySaveDoc = app.trustedFunction(function(doc)
{
var aDocumentFileName = this.documentFileName;
if (aDocumentFileName == "TEST1.pdf")
{
app.beginPriv();

var myPath = app.getPath("user", "documents") + "/" + "TEST1" + " - " + myDateString() + " - " + Kunde + " - " + Maschine + " - " + SN +".pdf";
doc.saveAs(myPath);

app.endPriv();

}
if (aDocumentFileName == "TEST2.pdf")
{
app.beginPriv();

var myPath = app.getPath("user", "documents") + "/" + "TEST2" + " - " + myDateString() + " - " + Kunde + " - " + Maschine + " - " + SN +".pdf";
doc.saveAs(myPath);

app.endPriv();

}
});
======================================================

and to the mouse up event of the button
======================================================
aDocumentFileName = this.documentFileName;
...
event.target.mySaveDoc(event.target);
======================================================

I works as I want it to.
But a new problem occurs now I didn't think of before.
It only works once for each file cause the file has now been renamed by the script ... ;-)
Can I use a wildcard for the filename
if (aDocumentFileName == "TEST*.pdf")
or something else?
So that the script works for all files with for example "TEST" at the beginning of the filename.

radzmar
LoveCycle Blog
Documents you need:
LiveCycle Designer ES2 Docs

gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
You can replace the 'if(...)' statement with the followiing:
// force to non-sensative case stringvar sTestName = aDocumentFileName.toLowerCase();// split into file name and extensionvar aTestName = sTestName.split('.');// get first 4 characters of file name - length of 'test'aTestName[0] = aTestName[0].substr(0, 4);// create test document file namevar sTestDocument = aTestName.join('.');// see if test name is 'test.pdf'if (sTestDocument = 'test.pdf')

George Kaiser

radzmar
Expert
Registered: Nov 3 2008
Posts: 1202
WOW! Works perfectly.

BIG THANKS for your help!

radzmar
LoveCycle Blog
Documents you need:
LiveCycle Designer ES2 Docs

Justjoane
Registered: Nov 24 2008
Posts: 10
Hi,

I'm looking for a javascript to add to an Acrobat 8 professional form to generate the file name from a form field. My understanding of javascript is basically copy and paste, filling in the unique field titles. Could you show the final script to do this in Acrobat 8 if there is one that works?

I'd like to pull the file name from two fields: form title and date.

Do I use a button>actions>mouse up/ Run a JavaScript?Thanks.
Joanie
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
JustJoane, You should post your question to a new thread. While the discussion here is relavent to your question, it seems that you are working with an AcroForm PDF and this forum is for LiveCycle forms.

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

patra
Registered: Oct 19 2006
Posts: 270
Is it posible for someone to post the complete code for the Button and for the Folder level Script?
It is a great article!

Thanks
Justjoane
Registered: Nov 24 2008
Posts: 10
Thanks, Thomp. We recently upgraded from Acrobat 7.0 on my Mac to Acrobat 8.0 on my PC. After your response, I found LiveCycle Designer “hiding” in one of the folders. I worked with it last week a little and feel it would be a fine program for some of our internal forms.

I tried working with the Javascript in this topic but I’m rather lost. My JavaScript proficiency is basically copy and paste and fill in the variables.

Is there a script for LiveCycle Designer for generating a filename from three form fields? We’re looking for a script that uses the data in the form fields for date needed, document name, and request type.

Thanks for any direction you can give me.
radzmar
Expert
Registered: Nov 3 2008
Posts: 1202
OK, before I get even more PM's, here's a little solution for those who want to save a form with a filename generated from the form entries.

1. JS for click event of a button:

//Defining global variable(s) used for new filename
Part1 = resolveNode("Form1.Page1.Date").rawValue;
Part2 = resolveNode("Form1.Page1.Name").rawValue;
Part3 = resolveNode("Form1.Page1.Surname").rawValue;

event.target.mySaveDoc(event.target);


2. Folder level JS:

// Defining a trusted function
var mySaveDoc = app.trustedFunction(function(doc)
{
// Check current doc name
var aDocumentFileName = this.documentFileName;

// If-Statement for ensuring, that the save fuction is only done if PDF has exactly this name
if (aDocumentFileName == "Registration Form.pdf")
{
app.beginPriv();

// Save under Users documents
var myPath = app.getPath("user", "documents") + "/" + "Registration Form" + " - " + Part1 + " - " Part2 + " - " + Part3 + " - " + ".pdf";

doc.saveAs(myPath);
app.endPriv();
}
});

So, these example saves a PDF named like "Registration Form - 01.01.2009 - John - Jonson.pdf" under My Documents.
You can also use antother path as saving directory.

var myPath = "//shared drive/folder/subfolder/" + "/" + "Registration Form" + " - " + Part1 + " - " Part2 + " - " + Part3 + " - " + ".pdf";

radzmar
LoveCycle Blog
Documents you need:
LiveCycle Designer ES2 Docs

rjdj2000
Registered: Mar 12 2009
Posts: 9
Hello,

Just found your forums here from the main Acrobat forums, I am trying to figure this all out and just need some clarification on it. There is a folder level JS as well as the JS code in the form? As to the previous post, where does it call for the folder level JS and what should the folder level JS be named? Or can all of the JS be put into the form? If so, how should it be formatted to work probperly.

Thanks
RJ
radzmar
Expert
Registered: Nov 3 2008
Posts: 1202
1. Because of the Acrobat/Reader security settings you are forced to use a folder level JS for some actions of the form, like saving the form into a predefined folder and changing the filename.
So, you can't put all the JS into the form, it won't work!

2. The name of the folder level JS file doesn't matter.

3. You have to put the JS file into the javascripts folder of your local acrobat/reader installation.
For example: C:\programm files\Adobe\Acrobat 9.0\Acrobat\Javascripts


You can use these samples for testing :-)

PDF
https://share.acrobat.com/adc/adc.do?docid=3bf14eee-910e-44f9-8cd6-245f85bcc780

Folder Level JS (save as filename.js)
https://share.acrobat.com/adc/document.do?docid=ea379645-09d4-439d-ad72-cae0dc2cab9a

radzmar
LoveCycle Blog
Documents you need:
LiveCycle Designer ES2 Docs

rjdj2000
Registered: Mar 12 2009
Posts: 9
Thanks.

I will take a look at it and try to implement this into what I already have. Hopefully I can get it to work in with the Save As/E-mail function that I currently have in my form.

If I run into a snag, I will come back for assistance as my JS is not that good, but I can normally figure things out quite well.

RJ
rjdj2000
Registered: Mar 12 2009
Posts: 9
Well I have run into a snag somewhere as when I put code in (See below) this is what I get in Acrobat 9 in the Javascript Console:

Console Error:

resolveNode("form1.body.page1.Name") has no properties
2:XFA:form1[0]:#subform[0]:Button1[0]:mouseUp

Now in LiveCycle, here is the code that is on the button:

form1.#subform[0].Button1::mouseUp - (JavaScript, client)
//Defining Global variable(s) used for new filename
Part1 = resolveNode("form1.body.page1.Name").rawValue;
Part2 = resolveNode("form1.body.page1.DateSold").rawValue;

//app.execMenuItem("SaveAs") - Old Saving - No File Naming - end user defined
event.target.mySaveDoc(event.target);
event.target.mailDoc(false,"******@**.com","","****@allterrainresq.com","ATR Unit Sold Form Submission","Attachment - ATR Unit Sold Form");

Here is the Javascript that is saved to mySave.js in C:\Program Files\Adobe\Acrobat 9.0\Acrobat\Javascripts

// Defining a trusted function
var mySaveDoc = app.trustedFunction(function(doc)
{
// Check current doc name
var aDocumentFileName = this.documentFileName;

// If-Statement for ensuring, that the save fuction is only done if PDF has exactly this name
if (aDocumentFileName == "ATR Units Sold Report Formtest.pdf")
{
app.beginPriv();

// Save under Users documents
var myPath = app.getPath("user", "documents") + "/" + Part1 + " - " Part2 + " - " + "ATR Unit Sold Form" + ".pdf";

doc.saveAs(myPath);
app.endPriv();
}
});

I am not quite sure where I went wrong on it. I have named the object in livecycle in the binding area Name and DateSold where it should, but for some reason it doesn't pick up the information that I have entered into it. Hopefully someone can pick up what went wrong and help get this working as radzmar's information is quite simple to understand. Even looking at the sample files that were left, I'm still lost at what went wrong.

Thanks
RJ
radzmar
Expert
Registered: Nov 3 2008
Posts: 1202
try the following for defining the variables

var Part1 = resolveNode("form1.body.page1.Name").rawValue;
var Part2 = resolveNode("form1.body.page1.DateSold").rawValue;

I don't know exactly why, but sometimes the global variables do not work.
If so, I use "var" to define a variable.

radzmar
LoveCycle Blog
Documents you need:
LiveCycle Designer ES2 Docs

rjdj2000
Registered: Mar 12 2009
Posts: 9
Ok, Tried that, didn't work. I have this now in the debugger:

Acrobat EScript Built-in Functions Version 9.0
Acrobat Annotations / Collaboration Built-in Functions Version 9.0
Acrobat Annotations / Collaboration Built-in Wizard Functions Version 9.0
Acrobat SOAP 9.0

missing ; before statement
13:Folder-Level:App:mySave.js
unterminated string literal
13:Folder-Level:User:mySave.js
resolveNode("form1.body.page1.Name") has no properties
2:XFA:form1[0]:#subform[0]:Button1[0]:mouseUp

Not sure if I am going to continue with this as for the end users of the form will have to save the .js file into the appropriate directory anyways to make it work. Now if there was a way to do this in the form itself and not have to rely on a external .js file. Then I am thinking of continuing but from what I have seen here so far, that is not possible. I guess sooner or later, I will figure out what went wrong and get it working.

RJ
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
You should pass "Part1" and "Part2" into the mySaveDoc() function call. This will avoid any problems with trying resolve the location of the variables.

In the last post, the reported error makes it look as if you were trying to call "resolveNode()" from a folder level script. resolveNode() is a function of the XMLData Object. The Folder level script operates in the Document context. To access the XFA scripting model from the Doc Object, use the "xfa" property.

xfa.form.resolveNode("form1.body.page1.Name")

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/javascript.php]http://www.adobe.com/devnet/acrobat/javascript.php[/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

rjdj2000
Registered: Mar 12 2009
Posts: 9
Thomp,

Tried your suggestion and it didn't work. I think I'm just going to leave it as a plain save as that I have currently working without the extra stuff. I will just mark the file as read-only so end users don't overwrite the orginal file (hopefully) and let them name the file. The only reason as why is because all the end users would have to install the folder level js file in their systems for this script to work. I could make it a self-extracting zip file and tell it where to put the file, but if they are using reader and not full acrobat, the directories are different and there is no way to tell which is installed.

I appreciate the help though and if I run into another problem, I'll be sure to come back here for help.

RJ
Nelmedia
Registered: Aug 7 2006
Posts: 4
Hi,

I don't know if I'm the only one facing this, for neither this topic or the other one mentioned at the beginning covers it...

Here's my code:
// SaveAs Function
var saveFunction = app.trustedFunction(function(doc, nomFichier, noInsp) {

app.beginPriv();
var myPath = app.getPath("user", "documents") + "/" + noInsp + "/" + nomFichier + "_" + noInsp + ".pdf";
doc.saveAs(myPath);
app.endPriv();

});

Everything works fine, except for one thing: I need to have the folder already existing... What I would need is for Acrobat to create a folder with the noInsp value and inside of it a file with the nomFichier_noInsp.pdf name... Now I need to manually create the folder (ex: 121212) for it to work, or I get the message (in the console):
RaiseError: The file may be read-only, or another user may have it open. Please save the document with a different name or in a different folder.
Doc.saveAs:8:XFA:topmostSubform[0]:Page2[0]:Sauvegarder[0]:click
===> The file may be read-only, or another user may have it open. Please save the document with a different name or in a different folder.Is Acrobat supposed to create the folder if it doesn't already exist? If not, is there a way to do it at runtime (there's no createFolder or the like function)...?
radzmar
Expert
Registered: Nov 3 2008
Posts: 1202
You will always get the Raise Error if the target does not exist.
The target definitive has to be there before you can use the script.
Adobe Reader is not able to create a new folders only files.

radzmar
LoveCycle Blog
Documents you need:
LiveCycle Designer ES2 Docs

Nelmedia
Registered: Aug 7 2006
Posts: 4
Well, too bad... but thanks anyway for the quick reply.
agarciabal
Registered: Apr 6 2009
Posts: 1
hello,

i'm a bennigner developer for Acrobat Forms in LiveCycle 8.1.

I'm trying to implementate this "SaveAs" in my form but i don't know where i need put the following code:

// SaveAs Function
var saveFunction = app.trustedFunction(function(nomFitxer) {

app.beginPriv();
var myPath = app.getPath("user", "documents") + "\" + nomFittxer + ".pdf";
doc.saveAs(myPath);
app.endPriv();

});

i need that this code are integrated in the form file, not in external js file.

and how i call it in a click success?

thank you very much!!!!!
Albert from Catalunya

(i'm sorry but my english is very poor)
radzmar
Expert
Registered: Nov 3 2008
Posts: 1202
@agarciabal

You can't use this function within the form, because of adobe's security settings, a save function can't be executed with a script in the form.

But, you can execute the save as function with the following JavaScript on a buttons click event:

app.execMenuItem("SaveAs");

In this way a dialog appers asking the user where the file should be saved.
The filename has to be corrected by the user manually and can't be changed with a script.

radzmar
LoveCycle Blog
Documents you need:
LiveCycle Designer ES2 Docs

wdoughertyiii
Registered: Jul 27 2009
Posts: 5
Greetings:

This is my first post. I am learning Acrobat 9 Pro and LiveCycle Designer. 8.2.1 from scratch (no prev. experience) because I was recently hired to make a college's forms electronic. Things are really going great and I've learned alot from your posts, but I have one thing to resolve before we go live with them.

Here's the background:
Community College will use electonically completed / digitally signed PDF (yes, PDF, no data) forms for Requisitions, Travel Resquests, Requests for leave, etc., etc. (i.e., there will be a large volume coming through). They may be sent from requestor to supervisor, to cabiniet member, to president, to accounting or other final office through email (i.e., they will be signed & submitted on average 5 times or so throughout the workflow).Here's what has happened in my tests:
I create a form in Designer & Reader enable in Acrobat,
Complete the Reader-enabled form in Adobe Reader 9,
Click to digitally sign,
The "save as" dialogue box appears,
Click to save as,
Then it tells me that there is already a file by this name and asks me if I would like to override it with the new copy.

Rather than having to rename the form every time an employee (or especially the administration which recieves dozens of the same forms each day) signs a document, I am wanting to find a way to insert a variable (whether it be the computer login name & date/time, or simply a randomly generated number) into the end of the filename.Is this possible?

Thanks for your help,

Will Dougherty III, Special Projects, Three Rivers Community College

Will Dougherty III

gbirmingham
Registered: May 18 2010
Posts: 1
Hello,
I understand the Acrobat/Reader security settings, and the fact that you are forced to use a folder level JS in order to have a button that will "SaveAs" a predefined filename. My question is, how do I make this form distributable so that recipients of this form can use the SaveAs function without having to go through all the trouble of saving scripts to thier folders? Is this possible?

Thanks,
Greg Birmingham
radzmar
Expert
Registered: Nov 3 2008
Posts: 1202
No, there is no simple solution for this.

The only thing I can imagine is a customized installer (setup.exe).
You can programm such a thing yourself with Inno Setup or NSIS for example.
Users then can simply install/deinstall forms and folder level scripts through this installer.
But, this is only practical in a closed enviroment, not if you're distribute the forms to people outside your company.

radzmar
LoveCycle Blog
Documents you need:
LiveCycle Designer ES2 Docs

gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
You should also be aware that there are many corporate installations that prevent users form adding code to system folders because of security restrictions. And for these users, the System Admins will need to install the code.

Adobe made this a security restricted action because there were PDFs being created and gathering sensitive information and being silently saved without the end user being aware of the save action.

George Kaiser

kellston
Registered: Jul 5 2010
Posts: 30
Hi All,
I have Followed radzmar's example perfectly I think.

I have a folder level script in C:\Program Files\Adobe\Acrobat 8.0\Acrobat\Javascripts

// Defining a trusted function
var mySaveDoc = app.trustedFunction(function(doc)
{
// Check current doc name
var aDocumentFileName = this.documentFileName;

// If-Statement for ensuring, that the save fuction is only done if PDF has exactly this name
if (aDocumentFileName == "Service_Report.pdf")
{
app.beginPriv();

// Save under Users documents

var myPath = "//c/Forms/Work Orders/" + "/" + " - " + Part1 + " - " Part2 + " - " + Part3 + " -" + Part4 + " - " + ".pdf";

doc.saveAs(myPath);
app.endPriv();
}
});

When opening the PDF the JS console reports:
SyntaxError: missing ; before statement
14:Folder-Level:App:config.js

The Code in the Click event of the save button:

//Defining global variable(s) used for new filename
Part1 = resolveNode("form1.ken1.ken4.Report_No").rawValue;
Part2 = resolveNode("form1.ken1.ken2.Company").rawValue;
Part3 = resolveNode("form1.ken1.ken3.Date").rawValue;
Part4 = resolveNode("form1.ken1.ken4.Serial_No").rawValue;

event.target.mySaveDoc(event.target);

When the save button is clicked
JS console reports:
event.target.mySaveDoc(event.target);
event.target is undefined
1:Console:Exec
TypeError: event.target is undefined
1:Console:Exec
undefined

Thanks for any help

LiveCycle Designer 8.0 Windows XP Pro
radzmar
Expert
Registered: Nov 3 2008
Posts: 1202
Hi,

check out this sample here.
It describes the method with the folder level script and offers files for testing.

Btw: Make sure you close Acrobat completely (check task manager) when you install a new folder level script.
The scripts will only be loaded at Acrobats initial process.

radzmar
LoveCycle Blog
Documents you need:
LiveCycle Designer ES2 Docs

kellston
Registered: Jul 5 2010
Posts: 30
Hi radzmar,
That is really cool your sample doc work great.

But I am afraid its a bit to complex for me to follow.
Could you dumb it down for me?
I want to have the path hardcoded to c/WorkOrders the get the file name from 4 fields in the pdf Report_No,Company,Date (which is data from the form not a time stamp),Serial_No.

I have tried working with your example but its a bit hard for me to follow.

Thanks
kellston
Registered: Jul 5 2010
Posts: 30
Hi radzmar,
Are you able to help me simplify this process?
Thanks
radzmar
Expert
Registered: Nov 3 2008
Posts: 1202
Hi,

sorry for the delay.
I didn't recognize your reply last week.

OK, for a fixed path use this script in the layoutReady:Event of a hidden text field "TargetInformation" on the master page.
  1. // Variables we use in the filename taken from fields somewhere in the form
  2. var Var1 = xfa.resolveNode("form1.#subform.TextField1").rawValue;
  3. // Directory where the form should be saved - i.e. the desktop of user 'jack'
  4. var FixPath = "/C/Users/Jack/Desktop/";
  5.  
  6. // This functions checks the current file name. If it is not the correct file name we don't save the file under a new file name
  7. if (event.target.documentFileName === "MyForm.pdf")
  8. {
  9. // Concatenate the variables to a file name
  10. var NewFileName = "MySavedForm_";
  11. NewFileName += Var1;
  12. NewFileName += ".pdf";
  13. // Replace all characters that are not allowed to be used in a save path – this it to avoid an raise error
  14. NewFileName = NewFileName.replace(/[\s\!\?\<\>\'\"\*\/\\\=\?\^\`\{\}\|\~]+/g, "_");
  15. // Concatenate save path with new file name for this form and show it into this field
  16. this.rawValue = FixPath + NewFileName;
  17. }
  18. else
  19. {
  20. this.rawValue = event.target.path;
  21. }
Then you need a button "Save" with this script in it'S click:Event.
  1. if (event.target.documentFileName === "MyForm.pdf")
  2. {
  3. //A variable for the Folder Level Script
  4. SaveAsTarget = xfa.resolveNode("form1.#pageSet.Page1.TargetInformation").rawValue;
  5. try
  6. {
  7. event.target.LCB_SaveAs(event.target);
  8.  
  9. // Show message to inform the user, where the new file was saved.
  10. xfa.host.messageBox("File has beed saved under:\r\r" + SaveAsTarget, "File Saved", 3, 0);
  11. xfa.form.execInitialize();
  12. }
  13. catch(e)
  14. {
  15. xfa.host.messageBox(e.toString().replace("RaiseError: ","") + "\r\rEnsure the destination folder exists and there isn't already a file with the same file name in that folder!", "Failed to save file", 0, 0); }
  16. }
  17. else
  18. {
  19. // Open the 'Save As' dialog to let the user choose the target and filename.
  20. app.execMenuItem("SaveAs");
  21. }
This sample assumes that you have already installed the folder level script from my blog.

radzmar
LoveCycle Blog
Documents you need:
LiveCycle Designer ES2 Docs

kellston
Registered: Jul 5 2010
Posts: 30
Thanks radzmar i think i have made some progress.

After put in this code as you describe and change a few thing to match my fields.
When I click the save button it get a pop of error window.
The title if the error window is:

"Warning JavaScript Window - Failed to save file"
Then the text of the error:
UnsupportedValueError: Value is unsupported. ===>Parameter cPath
Ensure the destination folder exists and there isn't already a file with the same file name in that folder!"

I am trying to save in c:/Forms/WorkOrders

This is the code: var FixPath = "/C/Forms/WorkOrders/";

The folder i am trying to use is empty.
radzmar
Expert
Registered: Nov 3 2008
Posts: 1202
That's a raise error.
The folder either don't exist, a file with the same name already exists and is opened, you have a unsupported character in your save path or you don't have the rights to save into the folder.

Do a check of the path.

Copy a PDF into this folder and open in in Acrobat.
Open the console (Ctrl + j) and enter
this.path;

Now hold down Ctrl and press Enter.
Acrobat returns the path of the file, which has to look like "/C/FORMS/WORKORDER/FILENAME.pdf".

If it is correct it may be a rights problem.
You first should try another folder in your user directory.


radzmar
LoveCycle Blog
Documents you need:
LiveCycle Designer ES2 Docs