Copy link to clipboard
Copied
Hello:
I am unlikely to ever learn javascript. But our company has need of a simple scripting template, and I wonder if any kind soul could provide sample code for this basic task ?
We would like to superimpose a series of checkboxes over a PDF such as below. When the user has made his selection(s), the submit button would export the names of the chosen checkboxes to a plain text file.
The resulting text file would be something similar to this:
Detail 1Yes
Detail 2No
Detail 3Yes
Detail 4No
Much appreciation for any help.
Copy link to clipboard
Copied
OK, then you can do it using this code:
var s = "";
for (var i=1; i<=4; i++) {
s+="Detail "+i+(this.getField("Detail " + i).valueAsString=="Off" ? "No" : "Yes")+"\r\n";
}
this.createDataObject("Data.txt", s);
this.exportDataObject({cName: "Data.txt", nLaunch: 1});
this.removeDataObject("Data.txt");
Copy link to clipboard
Copied
Is this file going to be used exclusively in Adobe Acrobat?
Copy link to clipboard
Copied
No. Adobe Acrobat will be the application that the user uses to make selections, but Acrobat plays no role in the process thereafter.
The exported information need only be a simple text file structured like my example. It will then be imported and used in another coding environment where we will "take it from there".
But the javascript is foreign to us.
Copy link to clipboard
Copied
I'm thinking the use of exportAsText({ but then again I'm just guessing.
Copy link to clipboard
Copied
I meant the PDF file, not the text file...
Copy link to clipboard
Copied
Yes. It will only be used by Adobe Acrobat.
Copy link to clipboard
Copied
OK, then you can do it using this code:
var s = "";
for (var i=1; i<=4; i++) {
s+="Detail "+i+(this.getField("Detail " + i).valueAsString=="Off" ? "No" : "Yes")+"\r\n";
}
this.createDataObject("Data.txt", s);
this.exportDataObject({cName: "Data.txt", nLaunch: 1});
this.removeDataObject("Data.txt");
Copy link to clipboard
Copied
Perfect !
It worked 1st try.
I'm sure that is primitive stuff for you, but you just saved us a lot of time.
Thank you try67 !
Copy link to clipboard
Copied
Thanks again for the code.
So now here comes the "what if".
Our company prefaces our "details" with four digit numbers. These numbers are not necessarily sequential.
What if our field names had this structure:
0096 - Detail 1
0123 - Detail 2
0012 - Detail 3
0433 - Detail 4
... rather than this structure:
Detail 1
Detail 2
Detail 3
Detail 4
Could the Data.txt have output similar to this ? :
0096 - Detail 1No
0123 - Detail 2Yes
0012 - Detail 3No
0433 - Detail 4No
I realize this changes the way you are iterating through the fields.
this.getField("Detail " + i)
...would not be sufficient.
Does Javascript support a wildcard, such as.... ?
this.getField("*Detail " + i)
Thanks
Copy link to clipboard
Copied
Hi,
Easiest may be just to create an array of fieldnames and iterate over them.
something like
var fieldNames = [ "0096 - Detail 1No", "0123 - Detail 2Yes", "0012 - Detail 3No", "0433 - Detail 4No"]
for ( var i = 0 ;i < fieldNames.length; i++){
var useField = this.getField(fieldNames[i]);
/// rest of the code here
}
OR
for ( var i = 0; i < this.numFields; i++){
var useField = this.getField(this.getNthFieldName(i));
/// rest of the code
}
The second way is more useful if you needing to access most of the fields in a form, but if you are only using a few then the first way works.
Regards
Malcolm
Copy link to clipboard
Copied
If you use the latter method mentioned you can have it automatically process all fields that contain "Detail 1" in their name by adding this line after the one that starts with "var useField...":
if (/Detail 1/.test(useField.name)==false) continue;
Copy link to clipboard
Copied
Thank you both.
The code works as expected.
The export procedure, however, causes two dialog boxes to occur:
First, this warning:
And secondly, the Data.txt file is opened in Notepad:
Is there a way to suppress both of these dialogs ?
nLaunch: 0 ?
Thank you.
Copy link to clipboard
Copied
What do you want to happen with the file, then? Just saved on the local machine?
If so, then yes, change the nLaunch parameter to 0.

