13 Replies Latest reply on Dec 12, 2013 11:41 AM by Anonymous.123

    Combing PDF forms with identical field names while retaining unique values.

    Anonymous.123

      I have several PDF files of the same form that has been filled out by multiple users. I need to create a combined file of all the responses for reporting purposes. However, the forms (obviously) all have the same field names, and when I combine them the values of the first form autofill the values of the matched fields on the other forms. I need a way to combine the forms while retaining the unique field values. I thought I could write a js to rename the fields, but that isn't possible.

       

      *EDIT: The fields need to retain editability because some contain long, scrolling text. Flattening or read-only isn't an option, not that either fixes the above problem.

       

      Suggestions?

        • 1. Re: Combing PDF forms with identical field names while retaining unique values.
          gkaiseril MVP & Adobe Community Professional

          Export each form as a unique FDF. Create a template of the form. Import one of the FDF files. Spawn a new page before the template have the spawn method rename the fields Hide the template. Import the next FDF file spawn a new page with rename. and repeat.

          • 2. Re: Combing PDF forms with identical field names while retaining unique values.
            Anonymous.123 Level 1

            Do you mean I would open each completed form and export it as FDF? If so, is there another option? I have hundreds of forms and this seems like a lengthy process.

            • 3. Re: Combing PDF forms with identical field names while retaining unique values.
              try67 MVP & Adobe Community Professional

              Just a side-note: Flattening the forms would have solved this problem, if it was an option in your case...

              • 4. Re: Combing PDF forms with identical field names while retaining unique values.
                Anonymous.123 Level 1

                Flattening isn't working for me.

                 

                I am using the Action Wizard to combine the group of files. In my action, I am using the PDF Optimizer to flatten form fields. This has the effect of making my fields uneditable (so where there should be a scroll bar to view additional text there is none), and still does not prevent field values from replicating in subsequent forms (since it does not rename the fields prior to flattening).

                 

                Is there a different process I should use for flattening?

                 

                *I'm using Acrobat X Pro.

                • 5. Re: Combing PDF forms with identical field names while retaining unique values.
                  try67 MVP & Adobe Community Professional

                  Are you flattening the files before combining them? You might need to add a

                  step that saves the flattened files in between...

                  If you want you can flatten the files using this JS code:

                   

                  flattenPages();

                  • 6. Re: Combing PDF forms with identical field names while retaining unique values.
                    Anonymous.123 Level 1

                    @Try67,

                     

                    Having read some of your other posts regarding the inability to rename form fields via javascript, I am attempting to write a script that would get the field properties of each field, use them to create new fields in the same place with a new name, insert the values from the original fields, and delete the old fields. However, I am not able to get that script to function as expected.

                     

                    Here is part of it (it calls upon a document level function that generates a random 5-digit alphanumeric number):

                     

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

                        var theName = makeid(5);

                        var fname = this.getNthFieldName(i);

                        var theRect = this.getField(fname).rect;

                        var thePage = this.getField(fname).page;

                        var theType = this.getField(fname).type;

                        this.addField(theName, theType, thePage, theRect);

                    }

                     

                    Without the for loop it works fine for any outright specified field.

                     

                    If I replace this.numFields with an actual number, like 6, the first two fields of the form (field.0 and field.1) are copied three times each, rather than the first 6 fields being copied once each.

                    • 7. Re: Combing PDF forms with identical field names while retaining unique values.
                      Anonymous.123 Level 1

                      Will flattening that way still allowing editing of the fields?

                      • 8. Re: Combing PDF forms with identical field names while retaining unique values.
                        try67 MVP & Adobe Community Professional

                        The problem, most likely, is that each time you're adding a new field you're changing the value of numFields, and possibly also the order the fields are ordered, thereby causing your loop to malfunction. You need to collect all the data about the fields, remove the current ones, and then create new fields in their places.

                        Also, you will have problems with radio-buttons and other duplicate fields, as well as copying values, etc.

                        This is not a trivial scripting task...

                        • 9. Re: Combing PDF forms with identical field names while retaining unique values.
                          try67 MVP & Adobe Community Professional

                          Anonymous.123 wrote:

                           

                          Will flattening that way still allowing editing of the fields?

                          Which way? Flattening by definition prevents editing as it converts form fields to static contents.

                          • 10. Re: Combing PDF forms with identical field names while retaining unique values.
                            Anonymous.123 Level 1

                            I need to retain editing of fields for scrolling text.

                            • 11. Re: Combing PDF forms with identical field names while retaining unique values.
                              Anonymous.123 Level 1

                              I'm beginning to understand that I may need to run this operation on a field-type specific basis, but most of the script would be the same regardless.

                               

                              However, I'm open to other ideas for achieving my desired end result, which is a combined file of forms that have retaining their unique values and editability (field name not important). It would be a lot simpler if I could just rename the fields, but that isn't possible at this time.

                              • 12. Re: Combing PDF forms with identical field names while retaining unique values.
                                gkaiseril MVP & Adobe Community Professional

                                Anonymous.123 wrote:

                                 

                                Do you mean I would open each completed form and export it as FDF? If so, is there another option? I have hundreds of forms and this seems like a lengthy process.

                                Have you looked at Batch Processing or Actions with Acrobat Professional.

                                • 13. Re: Combing PDF forms with identical field names while retaining unique values.
                                  Anonymous.123 Level 1

                                  UPDATE:

                                   

                                  I solved this problem, at least for my own needs. Following try67's advice in a related post, I had to delete the existing fields and create new ones with new names. Given my desired outcome, this meant collecting all of the field properties of the fields (with some variation by field type), storing it, erasing the existing fields, and using that stored information to create new, identical fields (again with some variation by field type). Since I didn't want to retain actions or javascript, this was a perfect solution. I also no longer needed the buttons to function (since I wasn't retaining their javascript), so I made them read-only. I'm listing my working code below, in case anyone else could benefit from some or all of it.

                                   

                                  My question to the community is this: Why doesn't Adobe allow for fields to be renamed via javascript? Is it a security issue? My life would have been a lot easier the last few days if I could simply rename existing fields.

                                   

                                  -----------------------------------------------------------------------

                                   

                                  The following script is used in a Combine Files action via the Action Wizard. The PDF optimizer is also used to strip out or flatten additional items.

                                   

                                  -----------------------------------------------------------------------

                                   

                                  //This script is used to rename all the fields in a document while also removing any javascript or actions associated with those fields. Makes buttons read only without renaming.

                                   

                                  //Function to create a random alphanumeric ID.

                                  function makeid(n) {

                                      var text = "";

                                      var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

                                      for( var i=1; i <=n; i++ )

                                          text += possible.charAt(Math.floor(Math.random() * possible.length));

                                      return text;

                                  }

                                   

                                  //List variables for collecting arrays of field properties.

                                  var fieldNum = this.numFields;

                                  var fieldNameArray = new Array();

                                  var fieldTypeArray = new Array();

                                  var pageNumArray = new Array();

                                  var fieldRectArray = new Array();

                                  var fieldValueArray = new Array();

                                  var borderStyleArray = new Array();

                                  var borderColorArray = new Array();

                                  var borderThicknessArray = new Array();

                                  var fillColorArray = new Array();

                                  var textColorArray = new Array();

                                  var textFontArray = new Array();

                                  var textSizeArray = new Array();

                                   

                                  var textAlignmentArray = new Array();

                                  var textMultilineArray = new Array();

                                   

                                  var checkmarkStyleArray = new Array();

                                   

                                  var radiowidgetNumArray = new Array();

                                  var radiowidgetRectArray = new Array();

                                  var radiowidgetPageNumArray = new Array();

                                   

                                  //Get the properties of all the current fields, including widgets.

                                  for (var i = 0; i < fieldNum; i++) {

                                      var currentField = this.getNthFieldName(i);

                                      fieldNameArray[i] = makeid(5);

                                      fieldTypeArray[i] = this.getField(currentField).type;

                                   

                                      borderStyleArray[i] = this.getField(currentField).borderStyle;

                                      borderColorArray[i] = this.getField(currentField).strokeColor;

                                      borderThicknessArray[i] = this.getField(currentField).lineWidth;

                                      fillColorArray[i] = this.getField(currentField).fillColor;

                                      textColorArray[i] = this.getField(currentField).textColor;

                                      textFontArray[i] = this.getField(currentField).textFont;

                                      textSizeArray[i] = this.getField(currentField).textSize  

                                   

                                      if (this.getField(currentField).type == "text") {

                                          fieldRectArray[i] = this.getField(currentField).rect;

                                          pageNumArray[i] = this.getField(currentField).page;

                                          fieldValueArray[i] = this.getField(currentField).value;

                                          textAlignmentArray[i] = this.getField(currentField).alignment;

                                          textMultilineArray[i] = this.getField(currentField).multiline;

                                      }

                                   

                                      if (this.getField(currentField).type == "checkbox") {

                                          fieldRectArray[i] = this.getField(currentField).rect;

                                          pageNumArray[i] = this.getField(currentField).page;

                                          fieldValueArray[i] = this.getField(currentField).value;

                                          checkmarkStyleArray[i] = this.getField(currentField).style;

                                      }

                                   

                                      if (this.getField(currentField).type == "radiobutton") {

                                          checkmarkStyleArray[i] = this.getField(currentField).style;

                                          fieldValueArray[i] = this.getField(currentField).value;

                                   

                                          var n = 0;

                                          while (this.getField(currentField + "." + n) != null) {

                                              radiowidgetNumArray[n] = this.getField(currentField + "." + n).name;

                                              n++;

                                          }

                                   

                                          for (x = 0; x < radiowidgetNumArray.length; x++) {

                                              radiowidgetRectArray[x] = this.getField(radiowidgetNumArray[x]).rect;

                                              radiowidgetPageNumArray[x] = this.getField(radiowidgetNumArray[x]).page;

                                          }

                                   

                                      }

                                   

                                  }

                                   

                                  //Delete all the current fields, except for buttons, which become read-only.

                                  for (var i = (fieldNum - 1); i > -1; i--) {

                                      var currentField = this.getNthFieldName(i);

                                      if (this.getField(currentField).type != "button") {

                                          this.removeField(currentField);

                                      } else {

                                          this.getField(currentField).readonly = true;

                                      }

                                  }

                                   

                                  //Using the stored arrays of field properties, generate new, identical fields.

                                  for (var i = 0; i < fieldNum; i++) {

                                   

                                      if (fieldTypeArray[i] == "text") {

                                          var newField = this.addField(fieldNameArray[i], fieldTypeArray[i], pageNumArray[i], fieldRectArray[i]);

                                          newField.value = fieldValueArray[i];

                                          newField.borderStyle = borderStyleArray[i];

                                          newField.strokeColor = borderColorArray[i];

                                          newField.lineWidth = borderThicknessArray[i];

                                          newField.fillColor = fillColorArray[i];

                                          newField.textColor = textColorArray[i];

                                          newField.textFont = textFontArray[i];

                                          newField.textSize = textSizeArray[i];

                                          newField.alignment = textAlignmentArray[i];

                                          newField.multiline = textMultilineArray[i];

                                          newField.doNotSpellCheck = true;

                                      }

                                   

                                      if (fieldTypeArray[i] == "checkbox") {

                                          var newField = this.addField(fieldNameArray[i], fieldTypeArray[i], pageNumArray[i], fieldRectArray[i]); 

                                          newField.value = fieldValueArray[i];

                                          newField.borderStyle = borderStyleArray[i];

                                          newField.strokeColor = borderColorArray[i];

                                          newField.lineWidth = borderThicknessArray[i];

                                          newField.fillColor = fillColorArray[i];

                                          newField.textColor = textColorArray[i];

                                          newField.textFont = textFontArray[i];

                                          newField.textSize = textSizeArray[i];

                                          newField.style = checkmarkStyleArray[i];

                                          newField.readonly = true;

                                      }

                                   

                                      if (fieldTypeArray[i] == "radiobutton") {

                                   

                                          for (y = 0; y < radiowidgetNumArray.length; y++) {

                                   

                                              var newField = this.addField(fieldNameArray[i], fieldTypeArray[i], radiowidgetPageNumArray[y], radiowidgetRectArray[y]);

                                              newField.value = fieldValueArray[i];

                                              newField.borderStyle = borderStyleArray[i];

                                              newField.strokeColor = borderColorArray[i];

                                              newField.lineWidth = borderThicknessArray[i];

                                              newField.fillColor = fillColorArray[i];

                                              newField.textColor = textColorArray[i];

                                              newField.textFont = textFontArray[i];

                                              newField.textSize = textSizeArray[i];

                                              newField.style = checkmarkStyleArray[i];

                                              newField.readonly = true;           

                                          }

                                   

                                      }

                                   

                                  }

                                   

                                  //End script.