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

Adding and Removing Fields

Explorer ,
May 31, 2021 May 31, 2021

Here is my frustrating situation. I have created a code that adds fields to the pages when a radio button is clicked. Here is what I am using:

 

var Pg1 = this.addField({cName:"Pg1", cFieldType:"text", nPageNum:0, oCoords:[510,77,580,59]}); getField("Pg1").readonly = true; Pg1.value = "Page 1" + " of " + (this.numPages); Pg1.textSize = 10; Pg1.textFont = "TimesNewRoman";

 

var Pg2 = this.addField({cName:"Pg2", cFieldType:"text", nPageNum:1, oCoords:[510,77,580,59]}); getField("Pg2").readonly = true; Pg2.value = "Page 2" + " of " + (this.numPages); Pg2.textSize = 10; Pg2.textFont = "TimesNewRoman";

 

//and it continues on with that code for additional pages.

 

What is happening is that I want these newly added fields (Pg1, Pg2, Pg-etc) to be removed/deleted when a different radio button is selected. I have tried:  this.removeField("Pg1");   this.deleteField("Pg1");  doc.removeField("Pg1");   this.resetField("Pg1");  but none of them work. If I use a reset form for all of the fields in the entire document, then some of the added fields will disappear, but Pg1 and Pg5 won't. What is even more annoying is that field Pg1 just keeps being added and cannot be manually removed. In other words, eveytime I use the script, there is a new Pg1 field created in the list of fields, but it cannot be selected and removed (see attached pic). I try to select the field in the list but it's like the field is not actually in the document. Does that make sense? I can go in and manually delete the Pg5 fields, but that's frustrating and the end users of this form will not have that ability.

 

Anyway, that's my problem. I'm sure I'm just missing something easy, but if someone knows how to get those fields to be removed or deleted, I would be  grateful. Thanks for your patience.

 

Screen Shot 2021-05-31 at 11.39.40 AM.pngexpand image

TOPICS
Create PDFs , JavaScript , PDF forms
1.8K
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 ,
May 31, 2021 May 31, 2021

What version of Adobe Acrobat does you use?

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
Explorer ,
May 31, 2021 May 31, 2021

Acrobat Pro DC latest version. I always keep it up-to-date.

 

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 ,
May 31, 2021 May 31, 2021

this.removeField("Pg1");

is the right command, but it will not work in Reader, only 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
Explorer ,
Jun 01, 2021 Jun 01, 2021

That's what doesn't make sense, because I have another section of the form where I use this.removeField(); in conjunction with this.addField and it works just fine. I'm going to go back over that section that works and see if I can find a difference.

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 ,
Jun 01, 2021 Jun 01, 2021

Hi,

 

Why are you adding and removing fields? I am trying to understand your workflow. Normally in most Acrobat forms the easiest way to have fields and not have fields is to show and hide them, this is much easier ( and works in Adobe Reader too).

 

so if we can understand more of your workflow maybe we can provide suggestions as to how to improve it?

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
Explorer ,
Jun 01, 2021 Jun 01, 2021

I have tried using show/hide before and I'd prefer to use that. The this.addField() code is being used as a workaround to create dynamic page numbering. The radio button that triggers this code is at the very end of the form and is one of the last things that is clicked, so when it is clicked, the code sees how many pages there are and numbers them accordingly. I have been trying for years to find another way to dynamically page number as this form can have anywhere from 4 - 7 pages at the end depending on the choices of the user. I have 2 other similar forms that can go as high as 9 pages. I have tried using event.value = this.pageNum + " of " + (this.numPages); and several variations of that, but I cannot get that snippet to number the pages correctly.  this.numPages works great, but this.pageNum doesn't ever work. Thus the this.addField() as it is the only way I've found to have the page numbers added correctly. I know I'm not the best at this, but I'm learning so quickly.

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
Explorer ,
Jun 01, 2021 Jun 01, 2021
LATEST

Ok, so I was able to figure it out. I have a second section of my form which uses similar addField and removeField code and I wondered why that worked and the code above didn't. I doubt I'll be able to fully explain why, but when I changed the cName so that it didn't match the var name, I was able to get this field to add and then be removed. Don't ask me to explain why. So here is what I had:

 

var Pg1 = this.addField({cName:"Pg1", cFieldType:"text", nPageNum:0, oCoords:[510,77,580,59]}); getField("Pg1").readonly = true; Pg1.value = "Page 1" + " of " + (this.numPages); Pg1.textSize = 10; Pg1.textFont = "TimesNewRoman";

 

and here is what is working:

 

var Pg1 = this.addField({cName:"Page1", cFieldType:"text", nPageNum:0, oCoords:[510,77,580,59]}); getField("Page1").readonly = true; Pg1.value = "Page 1" + " of " + (this.numPages); Pg1.textSize = 10; Pg1.textFont = "TimesNewRoman";

 

and then I use    this.removeField("Page1");     to make the field I added go away. Thanks for everyone trying to help me out.

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