6 Replies Latest reply on May 23, 2011 10:18 PM by KAKristensen

    Dynamicly Generating a barcode, static otherwise

    chris_matera

      I have a form in Acrobat Pro 9 that I am trying to generate a QR code based on the data entered in 3 fields.  If the 3 fields are NOT filled out I want a default QR code to be generated with a certain text string.  I have thought about creating a hidden form field with the default text I want, or just hardcoding it into the javascript.  Here is the javascript that generates the dynamic QR code based on the fields, but where/how do I put in the default hardcoded value?

       

      Thanks in advance for the help.

       

      -Chris

       

       

      /* Customize: */

       


      function bMemberOf(strName, aStrNames)
      {
          for (var nMembIdx in aStrNames)
          {
              if (strName == aStrNames[nMembIdx])
                  return true;
          }
          return false;
      }

       

      function strTabDelimited(oParam)
      {
          var bNeedTab = false;
          var strNames = "";
          var strValues = "";
          for (var i = 0; i < oParam.oDoc.numFields; ++i)
          {
              var strFieldName = oParam.oDoc.getNthFieldName(i);
              if ((null == oParam.aFields || bMemberOf(strFieldName, oParam.aFields))
                  && (null == oParam.strXclField || strFieldName != oParam.strXclField)
                  && (oParam.oDoc.getField(strFieldName).type != "button"))
              {
                  if (bNeedTab)
                  {
                      if (oParam.bFieldNames)
                          strNames += "\t";
                      strValues += "\t";
                  }
                  if (oParam.bFieldNames)
                      strNames += strFieldName;
                  strValues += oParam.oDoc.getField(strFieldName).value;
                  bNeedTab = true;
              }
          }
          if (oParam.bFieldNames)
              return strNames + "\n" + strValues;
          else
              return strValues;
      }

       

      try
      {
          if ( app.viewerVersion >= ADBE.PMD_Need_Version )

       

              event.value = strTabDelimited({oDoc: this, aFields: ["ID1","LAST","fIRST","MI"], bFieldNames: false});
          /* bfieldnames = false does not display the field names in the barcode just the data in the fields */
           else event.value = " ";

       


      }
      catch (e)
      {
          event.value = " ";
      }

        • 1. Re: Dynamicly Generating a barcode, static otherwise
          George_Johnson MVP & Adobe Community Professional

          It looks like the function strTabDelimited will return a string that just contains tab characters if all of the fields are blank. So you could add code just after the call to it that checks to see if that's what's returned, and if so, set the value of event.value to the default string you want to use.

          • 2. Re: Dynamicly Generating a barcode, static otherwise
            chris_matera Level 1

            Thanks George, is there a sample out there where I could see what that might look like, I am, admittedly, not a javascript expert.

             

            Thanks so much for all the help!

            • 3. Re: Dynamicly Generating a barcode, static otherwise
              George_Johnson MVP & Adobe Community Professional

              Replace this line of code:

               

                  event.value = strTabDelimited({oDoc: this, aFields: ["ID1","LAST","fIRST","MI"], bFieldNames: false});

               

               

              With something like:

               

              // Get the tab delimited values
              var sTemp = strTabDelimited({oDoc: this, aFields: ["ID1","LAST","fIRST","MI], bFieldNames: false});
              
              // Set up regular expression to test string for just one or more tab characters
              var re = /^\t+$/;
              
              // Set the value of the bar code field
              if (!re.test(sTemp)) {
                  event.value = sTemp;
              } else {
                  event.value = "Your default value goes here";
              }
              

               

               

              Edit: Corrected typo in regular expression

              • 4. Re: Dynamicly Generating a barcode, static otherwise
                chris_matera Level 1

                THanks that seemed to work, the only issue is that even though the fields are in this order in the barcode:

                 

                "ID1","LAST","fIRST","MI"

                 

                It's displaying

                 

                FIRST, Last, Middle, ID

                 

                Is there a way of setting the order?

                • 5. Re: Dynamicly Generating a barcode, static otherwise
                  George_Johnson MVP & Adobe Community Professional

                  You could set the order to whatever you want, but you'd have to use different code. If all you want is the values in a tab-delimited format in the order they are in the array, you could remove the bMemberOf function altogether and replace the strTabDelimted function and the subsequent code with the following:

                   

                  function strTabDelimited(aFields);
                  
                      var sValues = "";
                  
                      // Do minimal testing to see if aFields is an array
                      if (typeof aFields !== "object" || typeof aFields.length === "undefined") return sValues;
                  
                      // Get the number of field names
                      var nFields = aFields.length;
                  
                      // Loop through the fields and build up a string of tab-delimited values
                      for (var i = 0; i < nFields; i++) {
                  
                          // Append the current field value
                          sValues += getField(aFields[i].valueAsString;
                  
                          // Add a tab if needed
                          if (i !== nFields - 1) sValues += "\t";
                      }
                  
                      // Return the string
                      return sValues;
                  }
                  
                  // Get the tab delimited values string for these fields
                  var sTemp = strTabDelimited(["ID1","LAST","fIRST","MI"]);
                  
                  // Set up regular expression to test string for just one or more tab characters
                  var re = /^\t+$/;
                  
                  // Set the value of the bar code field
                  if (sTemp && !re.test(sTemp)) {
                      event.value = sTemp;
                  } else {
                      event.value = "Your default value goes here";
                  }
                  

                   

                  Note that the code in the function does little validation of the fields array . It assumes that the field names you pass in the array are valid field names of fields that can have a value (e.g., not buttons). I also didn't test this code, so double check.

                  • 6. Re: Dynamicly Generating a barcode, static otherwise
                    KAKristensen Level 1

                    Hi Chris

                     

                    Did you manage to get the barcode order to work?

                     

                    I have tried the code George sugested in one of my AcroForms but it only returns a null value. And I can't figure out whats wrong.

                     

                    Kirstine