Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
1

Simple Javascript to Export to Plain Text

Community Beginner ,
Aug 29, 2020 Aug 29, 2020

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

 

send_to_user_group.jpgexpand image

 

Much appreciation for any help.

 

 

TOPICS
PDF forms
4.4K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
1 ACCEPTED SOLUTION
Community Expert ,
Aug 29, 2020 Aug 29, 2020

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

View solution in original post

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 29, 2020 Aug 29, 2020

Is this file going to be used exclusively in Adobe Acrobat?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 29, 2020 Aug 29, 2020

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 29, 2020 Aug 29, 2020

I'm thinking the use of      exportAsText({     but then again I'm just guessing.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 29, 2020 Aug 29, 2020

I meant the PDF file, not the text file...

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 29, 2020 Aug 29, 2020

Yes.  It will only be used by Adobe Acrobat.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 29, 2020 Aug 29, 2020

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");
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 29, 2020 Aug 29, 2020

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  !

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 31, 2020 Aug 31, 2020

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 31, 2020 Aug 31, 2020

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 31, 2020 Aug 31, 2020

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;

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 31, 2020 Aug 31, 2020

Thank you both.

 

The code works as expected.

 

The export procedure, however, causes two dialog boxes to occur:

 

First, this warning:

 

discuss1.jpgexpand image

 

And secondly, the Data.txt file is opened in Notepad:

 

discuss2.jpgexpand image

 

Is there a way to suppress both of these dialogs ?  

 

nLaunch: 0   ?

 

Thank you.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 01, 2020 Sep 01, 2020
LATEST

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines