8 Replies Latest reply on Aug 11, 2011 4:16 PM by George_Johnson

    Conditional counting function for a PDF

    ixxiiixxi

      Hi everyone,

       

      I kind of feel like a cheater for posting for help rather than just learning this stuff, but... here we go.

       

      I need to write several custom calculations for a pdf. I have minimal programming background...

       

       

       

      Would anyone be willing to translate the following function into Java Script for me? I have no idea about Java Sript syntax... I can barely remember C++.

      This is just a rough sketch in C++, I am not sure the syntax is correct, but the jist is there. The parts in quotes are form fields in a pdf form.


      int x;                                                //x is an intiger
      float y;                                             //y is a real number

       

      x=1;                                                 //x starting value
      y=0;                                                 //y starting value

       

      while (x<13);                                    //initiates a finite counting loop
      {
         if("column_1_field(x)" == "12345");  //checks truth of a form field
           {
              y = y + "column_2_field(x)";      //progresses the value of y
           }                                                 //closes the if statement
      x++;                                               //counts x by +1
      }                                                      //closes the while loop
      cout y;                                             //displays output

       

      This function should check 12 fields in column 1 for truth. Then, for all that are true, it should sum the numbers in the corresponding row of column 2. The answer is displayed.

       

      MUCH thanks for correct answers, especially if there is a more simple way to code this.

       

      ixX

        • 1. Re: Conditional counting function for a PDF
          George_Johnson MVP & Adobe Community Professional

          You'll find JavaScript shares a lot of syntax with c languages. Below is my attempt at the equivalent JavaScript code, intended as the custom Calculate script for a text field. I didn't test it, but it should  work:

           

          // Custom Calculate script for text field
          (function () {

              // Initialize the variables
              var x = 1;
              var y = 0;

              while (x < 13) {

                  if (getField("column_1_field" + x).valueAsString === "12345") {
                      y += +getField("column_2_field" + x).value;
                  }

                  x++;

              }

              // Set this field value to the sum
              event.value = y;

          })();

           

          The first and last lines simply prevent the creation of document-global variables by creating an anonymous function that calls itself.

           

          The code assumes the field names are in the form "column_1_field1" and "column_2_field3", etc. You'll have to adjust the code to match your field names.

           

          Note that the code that gets the values of the column 1 fields retrieves the values as strings since you're comparing to a string. This is particularly important if the field might contain leading zeroes and all digits.

           

          The code that gets the value of the column 2 fields explicitly converts the value to a number using the unary + operator. This is important so that blank fields do not result in string  concatentation instead of numerical addition, since the "+" operator is used for both. A blank field will return an empty string as its value, not zero. But when you convert an empty string to a number using the unary + operator, it returns 0. There are other ways to convert a string to a number, but this is the simplest.

           

          Finally, you set the value of the field to which a calculation script is attached by setting event.value to the value you want, which in this case is the value of the variable y.

          • 2. Re: Conditional counting function for a PDF
            ixxiiixxi Level 1

            I will test this code this evening... THANKS MUCH!

             

            It really is not that different from C++. I reviewed a few tutorials and made a few attemps before posting here. I kept getting syntax errors

            whenever I treid to plug in field names.

             

            Now, the only difference I see here is that my actual field names have spaces in them. Can you use spaces in field names in Java? Or should I hold to the underscore convention?

            • 3. Re: Conditional counting function for a PDF
              George_Johnson MVP & Adobe Community Professional

              Sure, you can include spaces. I rarely do myself, but that's just a style preference.

              • 4. Re: Conditional counting function for a PDF
                MarkWalsh Level 4

                As George said, you can use spaces; I never use them myself either.

                 

                One problem if you do use spaces, is that you won't be able to use the fields in 'Simplified Field Notation' calculations. (at least not in the versions that I use). There may be other issues as well, and this is why I always name my fields without spaces (I use a similar naming convention as I use for variables when writing code)

                • 5. Re: Conditional counting function for a PDF
                  gkaiseril MVP & Adobe Community Professional

                  It is possible to use white space characters like the space or dot if one precedes the special character with the JavaScript Escape character "\".

                   

                  It is also a good idea to start field names with an alphabetic character since the simplified field notation assumes any unquoted string that starts with a numeric digit is a number and not a field name.

                  • 6. Re: Conditional counting function for a PDF
                    maxwyss Level 4

                    Spaces in field names are allowed. However, like the other contributors to this thread, I very rarely use them.

                     

                    However, you might have a closer look at hierarchical field names (where elements are separated by a dot), and their additional advantages.

                     

                    Hope this can help.

                     

                    Max Wyss.

                    • 7. Re: Conditional counting function for a PDF
                      ixxiiixxi Level 1

                      So I plugged it in, changed the field names, but it is not calculating.

                       

                      Based on the look of the code, I am sure that its right, also there were no syntax errors, and none of the other commentors said anything. So I am sure that this has something to do with the function interfacing the variable fields. That somehow I am not properly filling in these small sections of the function.

                       

                      Again, please pardon my ignorance...  do I need to set this up as a table in order to use a counting variable to interface a series of fields? I currently have 2 sets of 12 individual fields that are only associated in that:
                      1. the properties are all similar (they are numbers, text format is the same, etc.)
                      2. the field names are similar (just a numerical value to make them unique)
                      3. all fields in a given column are intended to receive similar data
                      4. um... they are nicely lined up on their visual presentation... (lol, i really haven't a clue)

                       

                      My actual field names are:

                      Account 1
                      Account 2 //etc. - up through Account 12

                       

                      and

                       

                      Donation Amount 1
                      Donation Amount 2 // - again, up through 12

                       

                      And to be sure I cover the most basic potential pitfall, the function is looking at... "Text Field Properties" "General" tab "Name" field... Is this assumption correct?

                      • 8. Re: Conditional counting function for a PDF
                        George_Johnson MVP & Adobe Community Professional

                        Can you post the code that you're actually using and confirm that you placed it as the custom calculation script for the field?