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

Generating a filename when saving an acrobat form

idv
Registered: Jun 15 2011
Posts: 2

Hi
 
I'm hoping that some of the experts on the forum could help with this please..
 
On our local shared drive for office based staff - I have created a PDF form, but when a user saves the document can anybody share a javascript that would generate the filename of the document based on the data in a form field within the form which is called 't-reference'
 
And for this document to be saved in a seperate directory from the original PDF otherwise the master document will be updated and overwritten with content and would confuse multiple users who would be accessing and completing the form.
  
Many Thanks

My Product Information:
Acrobat Pro 10.0, Windows
try67
Online
Expert
Registered: Oct 30 2008
Posts: 2398
It's possible, but there are a couple of problems:
- This can only work if the users use a special Save button that you create with a script, not if they use the built-in one.
- For this to work, you will need to install a script on the local machine of each of the users. I'm guessing that since this is an office environment it would be possible, though.
- Also, the folders where the files should be saved have to pre-exist. It's not possible to create them with a script.

Bottom line, I think you should look for a different solution. For example, make the file on the server read-only and force the users to save it elsewhere.

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

idv
Registered: Jun 15 2011
Posts: 2
Thanks for the info

How do you make the original read only? and is the only way of generating a filename based on data from one of form fields only possible with Javascript called from a button in the form? If so do you have any JS code that would achieve this?
try67
Online
Expert
Registered: Oct 30 2008
Posts: 2398
Right-click the file, properties, General tab, tick Read-Only.
A more advanced way would be to set the various network permissions so admins can edit it, but users can not, for example. Your IT dept should know how to do that.

Yes, a script (or a plugin) is the only way to use data from a form field when saving the file.
Like I said, the code to do this is a bit complicated since you can't call the method that saves a file directly from a button due to security reasons. It has to go through a trusted script located in a file on the local machine.
But basically it's something like this:

// get the value of the form field
var text1Value = this.getField("Text1").value;

// here you need to make sure that no illegal characters are used, and also that text1Value is not empty

// make a file name from the field value
var newFileName = text1Value + ".pdf";

// get the path where the file is currently located
var filePath = this.path.replace(this.documentFileName);

// create the new full path
var newFullFilePath = filePath + newFileName;

try {
this.saveAs(newFullFilePath); // Only this will not work from a button...
} catch (e) {
app.alert("Error! Could not save as: " + newFullFilePath);
}

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

IntrebuloN
Registered: Nov 25 2011
Posts: 5
Where can the folder level script go if Acrobat is not installed on the machine where the forms are being submitted from?

We would like to host several safety forms on our terminal server so that when field technicians remote into the terminal server from netbooks, they can access, complete and submit the forms.

My idea is to have Reader installed on the Terminal Server for the purpose of filling out the forms, which we also intend to place on the Terminal Server. We have just purchased Acrobat X Standard for the creation of the forms and have it installed on one of our office workstations.

The completed forms will be submitted to a static email address, but with multiple technicians submitting multiple forms from multiple sites, we are looking for a simple way to make the incoming PDF's easily identifiable whether it be via attachment filename (preferred) or via email subject/body (still acceptable) where the forms are being submitted from, or possibly both.

This obviously negates the possibility of a simple submit button with a mailto: URL since a JavaScript button action would have to be created to populate the variables for the composition of a new email message.

I have an idea of how to do the scripting, what I can't find information on is where to put a folder level javascript (if required) without installing acrobat (and purchasing another license) on the terminal server.

Basically we want the forms to be submitted via email with custom subject/body content (based on field values within the form) to a static email address from our Terminal Server and can this work if the only Adobe product installed on the Terminal Server is Reader?
try67
Online
Expert
Registered: Oct 30 2008
Posts: 2398
If you only want to set the subject/body of the email then you don't need a folder-level script, just a doc-level script, so there's no issue of where to install the script. I suggest using this approach.

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

IntrebuloN
Registered: Nov 25 2011
Posts: 5
Correct, but if I wanted to make a submit button that used SaveAs to save the completed PDF to a shared drive with a dynamically generated filename based on text field values then I would right?

At any rate I basically solved the bulk of my own question. With reader installed on the TS, I can place any folder level scripting in the Reader installations Javascripts folder. Or am I wrong?

The only reason I'm even entertaining the email route is b/c it would be simpler to do if dynamically generating a filename from text fields proves too involved for me.
try67
Online
Expert
Registered: Oct 30 2008
Posts: 2398
This is a very unorthodox situation, so I'm not sure what the answer would be. But at any rate, the saveAs method only works in Reader if the document has the "save rights" applied to it.

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

IntrebuloN
Registered: Nov 25 2011
Posts: 5
We are just trying to eliminate having to install reader + any required scripting on all the netbooks / office workstations in our attempt to make our H&S paperwork, well, paperless.Thanks for your help, I now have a definitive idea of where I'm going with this task.
IntrebuloN
Registered: Nov 25 2011
Posts: 5
I'd like to reopen this topic as I thought I had all the information I needed but there are a couple of things I'm still not sure of.

I have working forms with a submit button that emails the completed PDF to a co-worker with email elements generated from text fields.

I would like the button to save the file to disk with the filename to be generated based on the same fields used to compose the email (jobsite, foreman, date) BEFORE the form is submitted for archiving purposes. As it is now the attachments all bear the same name as the original document which forces the receiver to download the PDF from email, rename it and then move it to the network drive for archiving/backup.

I understand the general process to name the file.

What I don't know how to do:

Test that no illegal chars are being used in the filename (this could be an issue since my date field is in the format mm/dd/yyyy, maybe we can replace the /'s with -'s just for the filename generation?)

Creating the trusted function (I was only able to find related code for LiveCycle that included some "XFA."'s and "#subforms"'s that I believe are exclusive to LiveCycle)

These are simple, single page forms.

I don't think I have to check that the fields aren't empty as I'm already validating that required fields are filled within my submit button JS and the fields I'm looking to use for filename generation are all required fields.
Let's say the text fields I'm using are named SiteName, Foreman and SiteDate.

I'd like the filename to end up in this format "Original filename" + " - " + SiteName + " - " + Foreman + " - " + SiteDate + ".pdf"

The new filepath is merely a subfolder of the filepath the original forms reside in.
IntrebuloN
Registered: Nov 25 2011
Posts: 5
Ok trying to slash through this. Not sure how to call the private function from my button, here is where I'm at so far.
FL JS:

var mySaveAs = app.trustedFunction(function(doc)
{
app.beginPriv();
var mySaveAsTarget = newFilePath;
doc.saveAs({cPath: mySaveAsTarget});
app.endPriv();
});

On my button so far:

//JavaScript #0

var f = this.getField("OrientationDate"); f.value = util.printd("mm/dd/yyyy", new Date());

//JavaScript #1 (Validates the required fields)

var requiredFields = new Array();
for (var i = 0; i < this.numFields; i++)
{
var fName = this.getNthFieldName(i);
var f = this.getField(fName);

if (f.type!="button" && this.getField(fName).required && (this.getField(fName).value == '' || this.getField(fName).value == 'Off'))
{
requiredFields[requiredFields.length] = fName;}}
var error = "Please complete the following fields: \n\n";
for (j=0; j < requiredFields.length; j++)
{
if (requiredFields[j].value == null)
{
error = error + requiredFields[j] + '\n';
var f2 = this.getField(requiredFields[j]);
f2.setFocus();
}
}if (requiredFields.length > 0) {
app.alert(error);
} else {

// This is the form return e-mail. Its hardcoded
// so that the form is always returned to the same address
// Change address on your form

var cToAddr = "email [at] mydomain [dot] com";// Get field values to build email subject and body and also new file name

var SiteName = this.getField("SiteName").value;
var Foreman = this.getField("Foreman").value;
var OrientationDate = this.getField("OrientationDate").value;
var newFileName = "Site Orientation - " + SiteName + " - " + Foreman + " - " + OrientationDate + ".pdf";

// Make sure there are no illegal characters in filename before adding it to the full file path

newFileName = newFileName.replace(/[\s\!\?\<\>\'\"\*\/\\\=\?\^\`\{\}\|\~]+/g, "-");// Construct the full path with filename

var newFilePath = "Site Orientations/" + newFileName;

// Save it? Not sure what to put here to call the FL function


// Set the subject and body text for the e-mail message

var cSubLine = "Completed site orientation form for: " + SiteName;
var cBody = "This form was submitted by " + Foreman + " on " + OrientationDate;

// Send the form data as an PDF document to specified email address
this.mailDoc({bUI: true, cTo: cToAddr, cSubject: cSubLine, cMsg: cBody});
}