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

Copy link to clipboard

Copied

Thank you for your help.

I need help with entering in additional new form fields to the pdf, and naming them with a sequential number to the form field name.

There is no need to sum up the values, just entering new form fields and have them show sequentially on the pdf.  (sorry if there is any mis communication).

By using the loop for (var i=1; i<=22; i++) the names of the text form fields all end up with the same number.

I need the new form fields to be named sequentially. Hope this makes sense.

Can you please revise the looping so that it names all of the new text fields in a sequential order.

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; ) { still creating new text fields named with the same number eg testing22 is named on all text boxes

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

Your code will do that, but you're missing a critical part of the loop.

Change this:

for (var j = 1; j <= 22; )

To:

for (var j = 1; j <= 22; j++)

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

Thank you, we are getting so close.

It will now add new text fields named with numbers from 1 to 22, except it repeats the process 22 times and there are 484 text boxes.

Can you please examine the code so it adds the 22 numbered text boxes once only?

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++ ) {

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

Copy link to clipboard

Copied

This seems a more complex task. You want to add these fields based on the location of some "C1" fields?

You need to first describe what you want to achieve, exactly, and then try and write the code for it, not the other way around.

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

Copy link to clipboard

Copied

Hi, below is a description of what is required in a 24 page document.

On page 1 lists all the drawings (with new text form fields beside each drawing ).

On page 2 to 24 are all the drawings with a check box assigned on each page.

When a check box is ticked on the drawing page  then the corresponding text box on page 1 shows an X as a value in the field.

Eg there is a drawing listed on page 1 as ABC123, the actual drawing is on page 2, the check box is called C10.

The text form field on page 1 is called testingC10.

The set action of the form field will extract the digits from the active field name to find the name of the check box eg C10.

The script is actually working, except it does not place the numbered text form fields in the correct order. Eg testing01 then the next form field is testing12, instead it should be testing01, then testing02 etc. The text form fields need to be sorted into the correct ascending order, otherwise the drawing on page 1 will have an incorrect X marked in the form field. Below is the full script, if you can possibly advise how to add the text form fields in the correct ascending numerical sequence, this will be most helpful as the script will then be fully functional.

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++; } count the number of fields beginning with C1

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

console.println("C1" + (i - count)); this gives the correct number to assign to the new text form field eg 0,1,2,3,4 etc

var myRect = f.rect;

var m = this.addField("testing" + (i - count), "text", this.pageNum, myRect); adds 23 text form fields on page 1

m.setAction("Calculate"," var a = event.target.name; var thenum = a.replace(/^D+/g,var g = this.getField('C1'+thenum); var cbStatus = (g.isBoxChecked(0)); if (cbStatus ==1){event.value = 'X';} if(cbStatus == 0){ event.vaue = '';} "'); this adds the custom calculation script to the form field,

}

}

for (var i = this.numFields - 1; i >= 0; i--) {  this removes the old text form fields

var fname = this.getNthFieldName(i);

var f = this.getField(fname);

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

this.removeField(fname);

}

}

Look forward to your reply 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 12, 2018 Feb 12, 2018

Copy link to clipboard

Copied

LATEST

If you want it to be done in a specific order collect the fields names into an array, sort it and then process it.

If you want further help with this you can contact me privately.

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