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

Indicate if checkbox ticked for multiple fields using getNthFieldName

Explorer ,
Jan 22, 2018 Jan 22, 2018

Copy link to clipboard

Copied

I have created an Action script to place an X in text fields on page 1 if a corresponding check box is ticked which is located on  other pages of the document.

The pdf  file has multiple text fields on page 1 with the name of the text field beginning with C1.

In the rest of the document there are multiple check boxes with the name of the check box beginning with C1.

There are the same number of text fields and check boxes.

If the first check box  is ticked, then the first text box on page 1 should then have an X in the field.

If the second check box is ticked, then the second text box on page 1 should then have an X in the field etc.

I am  unsure how to capture the check box as a variable into the text field, if anyone can provide assistance that will be most appreciated.

for (var i = 0; i < this.numFields; i++) {

var fname = this.getNthFieldName(i);

var f = this.getField(fname);

if (fname==null) continue;

if ( f.type == "checkbox" && fname.substring(0,2) === "C1")  {

console.println("checkbox " + this.getNthFieldName(i));

}

else if ( f.type == "textbox" && fname.substring(0,2) === "C1"){

console.println("textbox " + this.getNthFieldName(i));

var check = this.getField(??);

var cbStatus = (check.isBoxChecked(0));

if (cbStatus == 1) {

event.value = "X";

}

if (cbStatus == 0) {

event.value = "";

}

}

}

TOPICS
Acrobat SDK and JavaScript

Views

1.2K

Translate

Translate

Report

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 ,
Jan 22, 2018 Jan 22, 2018

Copy link to clipboard

Copied

Use the name of the checkbox.

Votes

Translate

Translate

Report

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
Explorer ,
Jan 23, 2018 Jan 23, 2018

Copy link to clipboard

Copied

Thank you for your reply.  I will require further assistance please.

I need to place the check box name into if type == textbox section of the script, can you please advise how to get the name of the checkbox as a variant? This is what I have been unable to work out.   It is highlighted in blue.

Votes

Translate

Translate

Report

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 ,
Jan 23, 2018 Jan 23, 2018

Copy link to clipboard

Copied

The variable fname contains the field name in your code.

Votes

Translate

Translate

Report

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 ,
Jan 23, 2018 Jan 23, 2018

Copy link to clipboard

Copied

At the first console.println you have the name of the checkbox.

Votes

Translate

Translate

Report

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
Explorer ,
Jan 23, 2018 Jan 23, 2018

Copy link to clipboard

Copied

Please see revised script, the console reports back as undefined, and the text box name is not reporting in the console, can you please see if there are any errors in the script? Thank you.

for (var i = 0; i < this.numFields; i++) {

var fname = this.getNthFieldName(i);

var f = this.getField(fname);

if (fname==null) continue;

if ( f.type == "checkbox" && fname.substring(0,2) === "C1")  {

var check = this.getNthFieldName(i));

}

else if ( f.type == "textbox" && fname.substring(0,2) === "C1"){

console.println("textbox " + this.getNthFieldName(i));

var cbStatus = (check.isBoxChecked(0));

if (cbStatus == 1) {

event.value = "X"; }

if (cbStatus == 0) {

event.value = "";

}

}

}

Votes

Translate

Translate

Report

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
LEGEND ,
Jan 24, 2018 Jan 24, 2018

Copy link to clipboard

Copied

A note: the console always reports the last value returned in the code you run from the console. Where there is no result the word “undefined“ appears. This is not any kind of error or warning message.

A question: you said the check box names start C1 and the text field names thwart C1 too. So how do you go from one to the other? What are the actual names of the second check box and second text field? You CANNOT rely on getNthFieldName to return them in the order you want.

Votes

Translate

Translate

Report

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
Explorer ,
Jan 23, 2018 Jan 23, 2018

Copy link to clipboard

Copied

The variant check may need to be declared global, to work in the text if statement

Can you please advise how I can achieve this?

I tried global.check = this.getNthFieldName(i);

The looping did not work after declaring global in this way.

Votes

Translate

Translate

Report

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 ,
Jan 24, 2018 Jan 24, 2018

Copy link to clipboard

Copied

Use something like this:

for (var i = 0; i < this.numFields; i++) {

  var fname = this.getNthFieldName(i);

  var f = this.getField(fname);

  if (fname==null) continue;

  if ( f.type == "checkbox" && fname.substring(0,2) === "C1")  {

    console.println("checkbox " + this.getNthFieldName(i));

    var cbStatus = (f.isBoxChecked(0));

    if (cbStatus == 1) {

      event.value = "X";

    }

    if (cbStatus == 0) {

      event.value = "";

    }

  }

  else if ( f.type == "textbox" && fname.substring(0,2) === "C1"){

    console.println("textbox " + this.getNthFieldName(i));

  }

}

Where do you use this code?

Votes

Translate

Translate

Report

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
Explorer ,
Jan 24, 2018 Jan 24, 2018

Copy link to clipboard

Copied

Thanks for your continuing help Bernd. The code will be placed inside all text fields on page 1 as a custom calculation script.

The text fields are on page 1 only, the checkbox fields appear on pages 2 to 24. There are 23 text fields and 23 checkbox fields in this particular document I am using as an example.

I need the script to loop through each text box from 1 to 23 and place the (set action check box part of the script) to the custom calculation script.

Eg first text box will have script for first checkbox, second text box will have script incremented to second check box etc.

I have made changes to the script and reference to the check box instead of text box, as per your example, still not working, have you any ideas how to increment though each check box in the set action and assign to each text box?

I really appreciate your continued assistance, thank you.

for (var i = 0; i < this.numFields; i++) {

var fname = this.getNthFieldName(i);

var f = this.getField(fname);

if ( f.type == "text" && fname.substring(0,2) === "C1")  {

f.setAction("Calculate","for (var i = 0; i < this.numFields; i++) {  var fname = this.getNthFieldName(i);  var f = this.getField(fname);if ( f.type == 'checkbox' && fname.substring(0,2) === 'C1')  {var cbStatus = (f.isBoxChecked(0));if (cbStatus == 1){event.value = 'X';}if (cbStatus == 0){event.value = '';}}}"); } }

Votes

Translate

Translate

Report

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 ,
Jan 24, 2018 Jan 24, 2018

Copy link to clipboard

Copied

You test only for field names with "C1" at the begin.

What field names do you use?

Votes

Translate

Translate

Report

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
Explorer ,
Jan 28, 2018 Jan 28, 2018

Copy link to clipboard

Copied

Field Names for check boxes are

C10 on page 2

C11 on page 3

C12 on page 4

C13 on page 5

etc up to C122 on page 24

There are a total of 23 check boxes in the document.

Thanks Bernd.

Votes

Translate

Translate

Report

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 ,
Jan 28, 2018 Jan 28, 2018

Copy link to clipboard

Copied

And the name of the text fields?

Votes

Translate

Translate

Report

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
Explorer ,
Jan 29, 2018 Jan 29, 2018

Copy link to clipboard

Copied

Field Names for Text Boxes are:

C1 Reviewed and acceptedA

C1 Reviewed and acceptedB

C1 Reviewed and acceptedC

C1 Reviewed and acceptedD etc

There are a total of 23 text fields on page 1

Thanks Bernd

Votes

Translate

Translate

Report

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 ,
Jan 30, 2018 Jan 30, 2018

Copy link to clipboard

Copied

Use this document function:

function set_value(fname)

{

  var f = this.getField(fname);

  if (fname!=null) {

    var cbStatus = (f.isBoxChecked(0));

    if (cbStatus == 1) {

      event.value = "X";

    } else {

      event.value = "";

    }

  }

}

As calculation of field "C1 Reviewed and acceptedA" use:

set_value("C10");

As calculation of field "C1 Reviewed and acceptedB" use:

set_value("C11");

and so on.

Votes

Translate

Translate

Report

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
Explorer ,
Jan 30, 2018 Jan 30, 2018

Copy link to clipboard

Copied

Thanks Bernd.

I have added the document javascript.

When I run the script to add the custom calculation script to each text field it puts the same check box field name in each text field

eg all text fields are reporting the name as C10 (there is no incrementing)

I need the script to increment through each check box field and place it in each text box in the custom calculation script without having to manually input as there are 23 text fields (this will vary for each document as some documents may have over 100 text fields).

I have revised the script to have the check box field name entered by incrementing.  All text fields are all referring to the same check box field name of C19 so this has not worked.

Can you please look at the second script and advise how I can get the custom calculation script have the name of each check box incremented for each text box.

First script which gets the value of checkbox field C10 in all of the 23 text boxes

for (var i = 0; i < this.numFields; i++) {

var fname = this.getNthFieldName(i);

var f = this.getField(fname);

if ( f.type == "text" && fname.substring(0,2) === "C1")  {

f.setAction("Calculate","set_value('C10');");

}

}

Second modified script which increments through the check box fields in the custom calculation script but returns the value from the one check box named C19 in all of the 23 text boxes

for (var i = 0; i < this.numFields; i++) {

var fname = this.getNthFieldName(i);

var f = this.getField(fname);

if ( f.type == "text" && fname.substring(0,2) === "C1")  {

f.setAction("Calculate","for (var i = 0; i < this.numFields; i++) {  var fname = this.getNthFieldName(i);  var f = this.getField(fname);if ( f.type == 'checkbox' && fname.substring(0,2) === 'C1')  {set_value(fname);}}");

}

}

Your continued assistance is very much appreciated - thank you.

Votes

Translate

Translate

Report

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 ,
Jan 30, 2018 Jan 30, 2018

Copy link to clipboard

Copied

This will not work because all the names begin with "C1".

Votes

Translate

Translate

Report

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
Explorer ,
Feb 06, 2018 Feb 06, 2018

Copy link to clipboard

Copied

Hi Bernd,

I have been working on the script for the past week, and have used a different approach.

I am adding new field names over the existing field names, then removing the old fields.

There are 23 fields in total, with the script naming every field "testing".

I need for the script to name the fields in sequence order starting from 0 to 22.

eg

testing0

testing1

testing2

testing3 etc

Can please advise where I can modify the script to add the number sequence to the new field name?

for (var i = 0; i < this.numFields; i++) {

var fname = this.getNthFieldName(i);

var f = this.getField(fname);

if ( f.type == "text" && fname.substring(0,2) === "C1")  {

var myRect = f.rect;

var m = this.addField("testing", "text", this.pageNum, myRect);

}

}

for (var i = this.numFields - 1; i >= 0; i--) {

var fname = this.getNthFieldName(i);

var f = this.getField(fname);

if ( f.type == "text" && fname.substring(0,2) === "C1")  {

this.removeField(fname);

}

}

Thank you.

Votes

Translate

Translate

Report

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 ,
Feb 06, 2018 Feb 06, 2018

Copy link to clipboard

Copied

I give up.

Votes

Translate

Translate

Report

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
Explorer ,
Feb 07, 2018 Feb 07, 2018

Copy link to clipboard

Copied

Giving up is not in my vocabulary. I have modified the script and it is now adding the fields with numbers from 0 to 22.

Now for the very last part I need help with, it is not adding the new fields in sequential order, it is adding the fields randomly as follows.

testing00

testing01

testing12

testing16

testing17

testing18

testing19

testing20

testing21

testing22

testing02

testing03

testing04

testing05

testing07

testing08 etc to testing 15

Can you please advise how to program the script so that it adds the fields in a sequential order?

Once this is rectified the script will be complete (finally)

Thanks Bernd .

var count = 0;

for ( var i=0; i<this.numFields; i++) {

var fname = this.getNthFieldName(i);

if ( this.getField(fname).type == "text" && fname.substring(0,2) === "C1") count++; }

console.println("There are " + count + " text fields.");

for (var i = 0; i < this.numFields; i++) {

var fname = this.getNthFieldName(i);

var f = this.getField(fname);

if ( f.type == "text" && fname.substring(0,2) === "C1")  {

var n = (i - count);

if (n < 10) {

n = "0" + n;

}

else n = n;

var myRect = f.rect;

var m = this.addField("testing" + n, "text", this.pageNum, myRect);

}

}

for (var i = this.numFields - 1; i >= 0; i--) {

var fname = this.getNthFieldName(i);

var f = this.getField(fname);

if ( f.type == "text" && fname.substring(0,2) === "C1")  {

this.removeField(fname);

}

}

Votes

Translate

Translate

Report

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 ,
Feb 08, 2018 Feb 08, 2018

Copy link to clipboard

Copied

What difference does it make what the order is? 1+2+3 is the same as 3+1+2...

Votes

Translate

Translate

Report

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
Explorer ,
Feb 08, 2018 Feb 08, 2018

Copy link to clipboard

Copied

I will be adding a set action to the new form fields. (this will be added to the last script posted).

The set action will extract the number from the active text field to indicate if a check box is ticked.

It is imperative that the new text boxes are in a sequential order, otherwise it will indicate when the incorrect check box is ticked.

eg. page 1 has all the text boxes, pages 2 to 24 have the check boxes.

testing0 text box will indicate an X in the form field when the corresponding check box C10 is ticked.

testing1 text box will indicate an X in the form field when the corresponding check box C11 is ticked etc.

On page 1

Drawing X1233          Text form field - testing0      (Drawing X1233 is on page 2 with check box C10)

Drawing X1234          Text form field - testing1      (Drawing X1234 is on page 3 with check box C11)  

Drawing X1235          Text form field - testing2      (Drawing X1235 is on page 4 with check box C12)

etc

var a = event.target.name;

var thenum = a.replace( /^\D+/g, '');  extracting the digits from the active text field ,text box is called testing0

var g = this.getField("C1" + thenum); getting the check box value, the check box is called C10

var cbStatus = (g.isBoxChecked(0));

if (cbStatus == 1) {

event.value = "X";

}

if (cbStatus == 0) {

event.value = "";

}

console.println(g);

If you can help me revise the script so that the text boxes are added in sequential order, this will have script in working order for use. ( I will be most grateful)

As currently the end user has to manually enter X on page 1 for each check box ticked on the corresponding pages.  This can be quite time consuming as in some cases there can be 100 pages in a document.  This particular case has 23 pages.

Votes

Translate

Translate

Report

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 ,
Feb 08, 2018 Feb 08, 2018

Copy link to clipboard

Copied

I'm not really following the logic of what you're doing, but why are you iterating over all of the fields in the document if you know what the field names are, and they are consistent?

Just use a loop from 1 to 22 and add up all the "testingXX" fields, if that's what you'd like to do.

Votes

Translate

Translate

Report

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
Explorer ,
Feb 08, 2018 Feb 08, 2018

Copy link to clipboard

Copied

Thanks for your reply.

Can you please modify the script to show how to add the loop from 1 to 22 .

I have been playing around with the looping, but it is not working, it now numbers all the text boxes with the same number.

for (var i = 0; i < this.numFields; i++) {

var fname = this.getNthFieldName(i);

var f = this.getField(fname);

if ( f.type == "text" && fname.substring(0,2) === "C1")  {

for (var j = 1; j < 22; j++) { this looping not working gives all of the new text boxes the same name of testing21

var myRect = f.rect; var m = this.addField("testing" + j, "text", this.pageNum, myRect);

}

}

}

Votes

Translate

Translate

Report

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 ,
Feb 08, 2018 Feb 08, 2018

Copy link to clipboard

Copied

Here you go:

var total = 0;

for (var i=1; i<=22; i++) {

    if (i<10) total+=Number(this.getField("testing0"+i).valueAsString);

    else total+=Number(this.getField("testing"+i).valueAsString);

}

event.value = total;

Votes

Translate

Translate

Report

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