Expand my Community achievements bar.

'Save As' Button on Form

Avatar

Former Community Member

At the moment we are having to ask the user who is filling in a form to do a 'save as' and then specify a file name (that follows a particular naming convention).  It would be so much better if we could place a button on the form so that when selected it would save the form under a name which is made up of a couple of field values from the form itself.  Is this not possible?

18 Replies

Avatar

Level 6

It's possible, but you'd have to create a folder-level JavaScript file and install it on each user's machine. Will that be feasible?

Avatar

Level 10

As George already metioned you will need a folder level script, to do this.

Here's a sample you can start with.

http://thelivecycle.blogspot.com/2009/11/save-form-to-specific-directories-and.html

With Adobe Reader X you also have to define own policies for the Protected Mode.

http://thelivecycle.blogspot.com/2010/11/custom-policies-for-protected-mode.html

Avatar

Former Community Member

So would I be correct to assume that I would put something like this as a script on the form button...

SaveAsTarget = xfa.resolveNode("form1.#pageSet.Page1.FieldWithNewPath").rawValue;
event.target.LCB_SaveAs(event.target);

And, then place the folder level script (as follows) somewhere.  But where?

var LCB_SaveAs = app.trustedFunction(function(doc)
{
app.beginPriv();
var LCB_SaveAsTarget = SaveAsTarget;
doc.saveAs(LCB_SaveAsTarget);
app.endPriv();
});

Avatar

Level 6

The code would be something like:

SaveAsTarget = xfa.resolveNode("form1.#pageSet.Page1.FieldWithNewPath").rawValue;
LCB_SaveAs(event.target, SaveAsTarget);

var LCB_SaveAs = app.trustedFunction(function(doc, filename) {
    app.beginPriv();

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

So you call the function and pass a reference to the document and the filename string. That first part is assuming the code is somewhere in the document, such as a button click event. For the saveAs method, you might want to specify the optional bPromptToOverwrite parameter to it prompts the user to overwrite the file if it already exists.

To see where to place the folder-level JavaScript file, see: http://www.pdfscripting.com/public/111.cfm?sd=40

Avatar

Level 6

BTW, doing this sets up a security hole since anyone who knows that this function exists on a system could use it to save files to the system. You might want to at least change the name from the one shown here.


Avatar

Level 10

The folder level script goes to the folder "Javascripts" under c:\Progam Files\Adobe\Acrobat x.x\Acrobat for example.

Make sure you restart the app completely, otherwise the Folder Level Script is not initialized.

Avatar

Former Community Member

Have really no experience with Javascripting.  Do i just place the script in the folder via a text editor and save as?

Avatar

Level 6

Yes, and make sure the extension is ".js"


Avatar

Former Community Member

Thanks.  Also, what are the arguments in the two scripts that need changing by me?  I'm assuming I can't just put the first script on the button within the form and the other in the folder as is.  And how do i tell it to use a field value from the form as part of the file name?

Avatar

Level 6

Try the new code that I showed. The first script could indeed be placed in a button, but it assumes that it contains a valid file path. The code gets the value of the field and passes it to the saveAs function. I would recommend doing additional validation to ensure that it represents a valid path. It should look like: "/C/Directory 1/Directory 2/test.pdf"

The folder-level script should be usable as is.

Avatar

Former Community Member

Thank you.  I have the first script associated with a 'SaveAs' button on the form.  I have replaced the "form1.#pageSet.Page1.FieldWithNewPath" part of the script with "form1.#pageSet.Page1./C/test.pdf".  Also, I have placed the second script in my c:\program files\adobe\reader 9.0/reader/javascripts (as LCB_SaveAs.js).

When I open the form in Reader and click on the button nothing appears to happen.  In checking my 'C' folder I do not see any test.pdf file.

Avatar

Level 6

The change you made is wrong. You had the right idea with your original script:

var SaveAsTarget = xfa.resolveNode("form1.#pageSet.Page1.FieldWithNewPath").rawValue;

This gets the value of the text field, which is presumably a valid path. This then gets passed on to the saveAs function. For the time being you might just want to hardcode a valid path:

SaveAsTarget = "/C/Documents and Settings/jax3333/My Documents/test.pdf"

Once you get that working, move on to trying to retrieve the path from a field.

Also, in your example it looks like you're attempting to save to the root directory. Don't try to do that because Acrobat will not let you. Instead, use something in or under your "My Document" folder. Something like I showed above. More information on safe paths is in the documentation for the doc.saveAs method.

For it to work with Reader, the document has to be Reader-enabled. Have you done that?

Avatar

Former Community Member

Great, making progress here.  Yes, I do always read enable the form prior to testing it with Reader.  Anyways, I changed the script in the form to be as follows:

SaveAsTarget

= "/C/Documents and Settings/prjj/My Documents/test.pdf";

LCB_SaveAs(event.target

, SaveAsTarget);

And, it works.  It creates the test.pdf file in the directory as specified.

Now I just have to change the test.pdf to a file name that is made up of some standard text concatenated with a couple of fields on the sheet (eg. Timesheet-20110101-Jones.pdf) where the date and name Jones come from fields on the form itself.

Avatar

Former Community Member

Do you have any guidance/suggestions as to how I can modify the line:

SaveAsTarget = "/C/Documents and Settings/jax3333/My Documents/test.pdf"

such that the file is named "timesheet-date-name.pdf" instead of "test.pdf" (where date and name are field values taken from the form)?

Avatar

Level 10

Hi,

back to my sample.

On the masterpage you can see several text fields, that show different information such as the current file name and the possible save paths.

Those fields are visible for controlling reasons.

If you got it work you can set them to invisible or hidden, but they still have to be present in the form!

Assuming you already have installed the folder level script, the form automatically populates all of those fields with specific data.

It should look like that:

02.png

So, in Designer you can find the field "TargetCurrentPath" for example, that is populated with a new save path in the current directory of this form.

01.png

This is done by a script in the calculate:Event of this field via FormCalc.

; Get values for the file name and path from the specific fields


var CheckFileName = xfa.resolveNode("form1.#pageSet.Page1.FileParameters.ThisFileName").rawValue

var Source = xfa.resolveNode("form1.#pageSet.Page1.FileParameters.CurrentPath").rawValue


; A variable we use in the filename

; Replace all spaces with underlines to avoid raise errors during saving.

var CustomerInfo = Replace(xfa.resolveNode("form1.#subform.CustomerName").rawValue, " ", "_")


; Create a time stamp for the new file (optional)

var TimeID = Num2Date(date(), "DD-MM-YYYY")


; Variable with the new save target

var NewTarget = ""


; This functions checks the current file name. If it is not the correct file name no new file will be saved.

if (CheckFileName == "LCB_SaveAs.pdf") then

NewTarget = Concat(Source, "LCB_SaveAs", "_", CustomerInfo, "_", TimeID, ".pdf")

$.rawValue = NewTarget

else

$.rawValue = xfa.resolveNode("form1.#pageSet.Page1.FileParameters.FullSource").rawValue

endif

Beside the field there is a save button, that also uses a script, this time in JavaScript, to execute the save function in the folder level script.

//Get the value of the field with the current file name

var CheckFileName = xfa.resolveNode("form1.#pageSet.Page1.FileParameters.ThisFileName").rawValue


/*

Create a variable with the current file name.

If the filename matches with the file name in the if expression

save the file under the predefinded destination and with the new file name.

*/

if (CheckFileName == "LCB_SaveAs.pdf")

{

//A variable for the Folder Level Script

SaveAsTarget = xfa.resolveNode("form1.#pageSet.Page1.FileParameters.TargetCurrentPath").rawValue;

// Execute the save function in the Folder Level Script

event.target.LCB_SaveAs(event.target);

// Show message to inform the user, where the new file was saved.

xfa.host.messageBox("File has beed saved under:\r\r" + SaveAsTarget, "File Saved", 3, 0);

xfa.form.execInitialize();

}

else

{

// Open the 'Save As' dialog to let the user choose the target and filename.

app.execMenuItem("SaveAs");

}

If you like to change the naming of your file, you have to change

     if (CheckFileName == "LCB_SaveAs.pdf")

into

     if (CheckFileName == "Timesheet.pdf")

in both of the scripts (TargetCurrentPath and save button).

And also this line

     NewTarget = Concat(Source, "LCB_SaveAs", "_", CustomerInfo, "_", TimeID, ".pdf")

into

     NewTarget = Concat(Source, "Timesheet", "_", CustomerInfo, "_", TimeID, ".pdf")

in the calculate:Event of the field "TargetCurrentPath".

Avatar

Former Community Member

Thank you.  I am somewhat lost though.  Not sure I follow all of what you are suggesting.  Can I not keep it simpler?  At the moment I have the following script within the form itself (associated with a 'saveas' button):

SaveAsTarget = "/C/Documents and Settings/prjj/desktop/sap time 3/temp.pdf";

LCB_SaveAs(event.target

, SaveAsTarget);

Also, I have a script in my javascript folder (titled LCB_SaveAs.js)

var LCB_SaveAs = app.trustedFunction(function(doc, filename) {
    app.beginPriv();

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

It works right now.  All I need to do is to change the SaveAsTarget to include a file name that is made up of the name and date fields from the form.  Is it not possible to do something like the following?

SaveAsTarget = "/C/Documents and Settings/prjj/desktop/sap time 3/" + name.rawvalue + date.rawvalue +".pdf"

Avatar

Level 2

Hello,

I really try to do my best but.... Thanks to all for now, i learned a lot from discussions.

Here are my js and pdf files if someone want to check it and make it works. I'll be grateful. It is non commercial thing I try to make it for non profit org for my friend.

I want to make it that when I click it on "Save as" btn [which is on 2nd page bottom] to rename it on Cbr field value [see picture]  and date value.

Thanks in advance.

Capture field Cbr.PNG

Files:

https://mega.nz/#!OQQSAAZS!N0coQsfr6XVr7WdyPGvKwo0b1OuFicy5Qc7kjLjRF7E