2 Replies Latest reply on Aug 15, 2012 12:23 PM by StealthH4cker

    Remove A Widget (Field Instance) Using JavaScript?


      Hello ,


      I am trying to createt a JavaScript Action (Sequence) which removes unwanted fields from forms, however some fields share the same name. I only want to remove certain widgets/instances. I can remove fields with the "removeField()" function but it does not work with the widget/instance of a field; all instaces of the field are removed.


      For example:

      There are two fields on the form. They have the same name called "address".


      So there are two fields with these IDs:




      I would like to remove the first one ONLY.


      So I tried the function and it fails:



      I also tried:



      Is what I am trying to do not supported or is there another way to do it?


      I am using Acrobat X Professional on Windows 7.


      Thank you.

        • 1. Re: Remove A Widget (Field Instance) Using JavaScript?
          George_Johnson MVP & Adobe Community Professional

          As you've found, it removes all instances.


          Some ideas for other approaches:


          1. Before removing the field, collect information about the ones you want to retain and recreate just the ones you want after removing them. This may not be feasible since you can't get field actions, and can only recreate JavaScript actions, unless you import an FDF that sets the field actions.


          2. The flattenPages method can be used to remove just the non-printing fields from a page, so you could first set the field in question to non-printing (field.display = display.noPrint) and flatten just the page that has the instance you want to remove. If you have other type of annotations on the page, you may have to deal with them as well.

          • 2. Re: Remove A Widget (Field Instance) Using JavaScript?
            StealthH4cker Level 1

            Thank you. I have built a function using approach number 1. I hope this helps someone else.




            function removeFieldInstance(fieldInstanceName) {
                 var keepFields = [];
                 //Get the value before the last period
                 var idPos = fieldInstanceName.lastIndexOf(".");
                 var parentFieldName = fieldInstanceName.substring(0,idPos);
                 //Go through each instance and get the field properties
                 instanceID = 0;
                 while(true) {
                      var currentInstanceName = parentFieldName+"."+instanceID;
                      //Increment the instanceID
                      //Skip this instance if it is the one we are removing
                      if(fieldInstanceName==currentInstanceName) {
                      //Get the field instance
                      var fieldInstance = getField(currentInstanceName);
                      //Check if this instance is null (exists)
                      if(fieldInstance==null) {
                           break; //Stop the loop
                      //Get the Rectangle, Page Number, Field Type properties
                      var fieldName = parentFieldName;
                      var fieldType = fieldInstance.type;
                      var fieldPage = fieldInstance.page;
                      var fieldRect = fieldInstance.rect;
                      //Add the properties to an array
                      var keepField = [];
                      keepField[0] = fieldName;
                      keepField[1] = fieldType;
                      keepField[2] = fieldPage;
                      keepField[3] = fieldRect;
                      //Add the field array to the keepFields array
                 //Remove all the field instances
                 //Create all the fields
                 for(var i=0; i<keepFields.length;i++) {
                      var keepField = keepFields[i];
                      //Create the field