29 Replies Latest reply on Feb 15, 2018 4:59 PM by paula99520745

    java calculations

    paula99520745 Level 1

      Hi All

      Anyone able to help, I'd like to use or reference a fixed value so I can use it in a java calculation

      I need to do this all in java, so my script needs to hold the value and I want to use it in some simple maths cals

      This should be so easy but the material examples I am finding on the Net on Adobe pdf java is not helping me.

      I been through Thoms video on Youtube and Iam still none the wizer

        • 1. Re: java calculations
          Bernd Alheit Adobe Community Professional & MVP

          Acrobat uses JavaScript, not Java.

           

          For a fixed value you can use a variable like:

          var price = 1.23;

          • 2. Re: java calculations
            paula99520745 Level 1

            Yes I have a declared variable

            var AF = 5;

            My problem is when I add a calculation, like

            + "Admin $"  [Af] + "\n";

            I cant use it to produce an answer

            What I have is a sub total

            I'd then like to show the AF variable

            Then I wish to add the sub total and AF together as a Grand Total

             

             

            your thoughts?

            • 3. Re: java calculations
              try67 MVP & Adobe Community Professional

              To access the value of a field you need to use this syntax:

              this.getField("Af").value

              Notice the JS is case-sensitive, so if the field is called "AF" you have to enter it like that, not as "Af".

              • 4. Re: java calculations
                paula99520745 Level 1

                Thanks for the help.

                 

                Perhaps I should have said the result is being calculated in a popup text box

                because this doesn't work

                + "Admin $" this.getField("AF").value;

                This is the full script:

                ---

                var msgText = "There are " + this.numPages + " pages in this document\n"; 

                // counters 
                var numA4 = 0; 
                var numA3 = 0; 
                var numA2 = 0;
                var numA1 = 0; 
                var numA0 = 0;
                var p = .20;
                var AF = 5.00;

                // Values
                var A4 = numA4*p;
                var A3 = numA3*p;
                var A2 = numA2*p;
                var A1 = numA1*p;
                var A0 = numA0*p;
                var Sum = 0;

                // helper function to compare a number with some wiggle room 
                function inRange(wid,hei,targetWid,targetHei) { 
                    var wiggle = 288; // amount of error to allow 
                    return (wid>=(targetWid-wiggle) && wid<=(targetWid+wiggle)) &&  (hei>=(targetHei-wiggle) &&    hei<=(targetHei+wiggle)) || (hei>=(targetWid-wiggle) && hei<=(targetWid+wiggle)) &&  (wid>=   (targetHei-wiggle) && wid<=(targetHei+wiggle))  } 
                 
                for (pagenum=0;pagenum<this.numPages;pagenum++) { 
                    var pbox = this.getPageBox("Crop",pagenum); 
                    var width = (pbox[2] - pbox[0]); 
                    var height = (pbox[1] - pbox[3]); 
                   var recognized = false; 
                        if (inRange(width,height,595,842)) { numA4++; recognized = true; }   
                        if (inRange(width,height,842,1191)) { numA3++; recognized = true; } 
                        if (inRange(width,height,1191,1684)) { numA2++; recognized = true; }
                        if (inRange(width,height,1684,2384)) { numA1++; recognized = true; }
                        if (inRange(width,height,3502,4566)) { numA0++; recognized = true; }   
                 
                    if (!recognized) msgText += ("Page " + (pagenum+1) + " has a width of " + (width/72).toFixed(2) + " and a height of " + (height/72).toFixed(2) + "inches\n");  } 
                msgText +=
                  numA4 + " A4 pages $" + (numA4*p).toFixed(2) + "\n"
                + numA3 + " A3 pages $" + (numA3*p*2).toFixed(2) + "\n"
                + numA2 + " A2 pages $" + (numA2*p*4).toFixed(2) + "\n"
                + numA1 + " A1 pages $" + (numA1*p*8).toFixed(2) + "\n"
                + numA0 + " A0 pages $" + (numA0*p*16).toFixed(2) + "\n"
                + (this.numPages++ - (numA4+numA3+numA2+numA1+numA0)) + " other pages" + "\n"
                + "SubT $" + (numA4*p+numA3*p*2+numA2*p*4+numA1*p*8+numA0*p*16).toFixed(2) + "\n"
                + "Admin $" this.getField("AF").value;

                app.alert(msgText);

                 

                -----

                • 5. Re: java calculations
                  try67 MVP & Adobe Community Professional

                  You forgot to add a plus sign before that statement...

                  • 6. Re: java calculations
                    paula99520745 Level 1

                    Ok the script now runs

                    so it looks like...

                    + this.getField +("AF").value;

                    This still wont bring up the answer set against the variable

                    var AF = 5.00

                    It should be reporting on the last line

                    Admin $5.00

                    If you copy-n-paste my full code and run it against any multiple paged pdf you will see how it works.

                    What I am wanted to achieve is;

                    an iteration of the pages, valued

                    sum of the 'other pages' - this is pages that are not within the paper spec's

                    a Sub Total

                    A new line for Admin of $5.00

                    And a new Grand total

                    • 7. Re: java calculations
                      Bernd Alheit Adobe Community Professional & MVP

                      + "Admin $" + this.getField("AF").value;

                      • 8. Re: java calculations
                        try67 MVP & Adobe Community Professional

                        Works fine for me...

                         

                        • 9. Re: java calculations
                          paula99520745 Level 1

                          I don't get the output.

                          I get the green tick to indicate the script ran, but no output

                           

                          Command Name: Execute JavaScript

                          Command Start Time: 2018-02-13 12:40:40

                          Command Status: Succeeded

                          Command Finish Time: 2018-02-13 12:40:40

                           

                          Any ideas whats up?

                          • 10. Re: java calculations
                            try67 MVP & Adobe Community Professional

                            Where are these commands and this output are coming from? I'm not familiar with anything like that in Acrobat.

                            • 11. Re: java calculations
                              paula99520745 Level 1

                              If your referring to

                              Command Name: Execute JavaScript

                              Command Start Time: 2018-02-13 12:40:40

                              Command Status: Succeeded

                              Command Finish Time: 2018-02-13 12:40:40

                               

                              They come from here

                              • 12. Re: java calculations
                                try67 MVP & Adobe Community Professional

                                Ah OK, you're running it from an Action. I suggest you run it from the JS Console window first, and see what the results are.

                                • 13. Re: java calculations
                                  paula99520745 Level 1

                                  Thanks for the help. still did not run from console

                                  • 14. Re: java calculations
                                    paula99520745 Level 1

                                    Correction. now that I remember how to do this.

                                    I did get it to run in Console

                                    and it shows an error.

                                     

                                    TypeError: this.getField("AF") is null

                                    52:Console:Exec

                                    There are 4 pages in this document

                                    4 A4 pages $0.80

                                    0 A3 pages $0.00

                                    0 A2 pages $0.00

                                    0 A1 pages $0.00

                                    0 A0 pages $0.00

                                    0 other pages

                                    subT $0.80

                                    • 15. Re: java calculations
                                      Thom Parker Adobe Community Professional

                                      There's you answer right there, field "AF" does not exist on the document.

                                      • 16. Re: java calculations
                                        paula99520745 Level 1

                                        You lost me Thom

                                        Try67 on 12/02/2018 managed to demonstrate he did produce the value from the Variable held??

                                        Which is what I am trying to do as well, I am at a loss as to why he ran the identical script and did get the value but when I run it I get nothing

                                        I want the code told hold a variable that I can change to be displayed on the popup box

                                        The value is in the script code var AF = 5.00;

                                        I don't know how this relates to your comment that AF is no in the document?

                                         

                                        Perhaps the answer lies in how the AF value is being held, can you not simply display a variable without using it in a calculation

                                        • 17. Re: java calculations
                                          try67 MVP & Adobe Community Professional

                                          In your code you're trying to insert the value of a field called "AF" into the message string. Since there's no such field in the file, the error you see is thrown.

                                          • 18. Re: java calculations
                                            Bernd Alheit Adobe Community Professional & MVP

                                            Create text field with the name "AF" and put the value 5 in it.

                                            • 19. Re: java calculations
                                              paula99520745 Level 1

                                              I am not using a form, this is purely a script

                                              or can I create a 'test' field in the script to hold the value? I thought that's what the variable did?

                                              • 20. Re: java calculations
                                                Bernd Alheit Adobe Community Professional & MVP

                                                You can use a variable AF

                                                 

                                                var AF = 5.0;

                                                 

                                                and access the variable

                                                 

                                                + "Admin $" + AF;

                                                • 21. Re: java calculations
                                                  try67 MVP & Adobe Community Professional

                                                  Then you have a very basic misunderstanding what you did.

                                                  The getField method is used to access a form fields. If you're not trying to do that, then what are you trying to do?

                                                  What is this "AF" value?

                                                  • 22. Re: java calculations
                                                    paula99520745 Level 1

                                                    The bit of code I think we are talking about is

                                                    + "Admin $" + this.getField("AF").value;

                                                    I have in the script

                                                    var AF = 5.00;

                                                    What I want to get is the line reporting

                                                    Admin $5.00

                                                    • 23. Re: java calculations
                                                      try67 MVP & Adobe Community Professional

                                                      Use Bernd's code from his last reply.

                                                      • 24. Re: java calculations
                                                        paula99520745 Level 1

                                                        Replaced

                                                        + "Admin $" this.getField("AF").value;

                                                        with

                                                        + "Admin $" + AF;

                                                        This has improved things as the script now runs and generates the popup again.

                                                        But still didn't produce the line Iam after

                                                        Admin $5

                                                        • 25. Re: java calculations
                                                          try67 MVP & Adobe Community Professional

                                                          Once more, if you don't post your full code we can't help.

                                                          • 26. Re: java calculations
                                                            paula99520745 Level 1

                                                            Sorry I didn't see that request.

                                                            Its actually in the tread above, but here it is as it sits now

                                                            ---

                                                            // This script is designed to Count & Price an existing pdf of mixed page sizes based on .20pp
                                                            // how to use. copy all of this text. Open pdf, hit CtrL=J Paste copy in java window, slect all pasted
                                                            // code hit CtrL=Enter
                                                            // Paul Abbot ODC 2018
                                                            // known issue, not identifying A0 pages at present

                                                            var msgText = "There are " + this.numPages + " pages in this document\n"; 

                                                            // counters 
                                                            var numA4 = 0; 
                                                            var numA3 = 0; 
                                                            var numA2 = 0;
                                                            var numA1 = 0; 
                                                            var numA0 = 0;
                                                            var p = .20;
                                                            var AF = 5.0;

                                                            // Values
                                                            var A4 = numA4*p;
                                                            var A3 = numA3*p;
                                                            var A2 = numA2*p;
                                                            var A1 = numA1*p;
                                                            var A0 = numA0*p;
                                                            var Sum = 0;

                                                            // helper function to compare a number with some wiggle room 
                                                            function inRange(wid,hei,targetWid,targetHei) { 
                                                                var wiggle = 288; // amount of error to allow 
                                                                return (wid>=(targetWid-wiggle) && wid<=(targetWid+wiggle)) &&  (hei>=(targetHei-wiggle) &&    hei<=(targetHei+wiggle)) || (hei>=(targetWid-wiggle) && hei<=(targetWid+wiggle)) &&  (wid>=   (targetHei-wiggle) && wid<=(targetHei+wiggle))  } 
                                                             
                                                            for (pagenum=0;pagenum<this.numPages;pagenum++) { 
                                                                var pbox = this.getPageBox("Crop",pagenum); 
                                                                var width = (pbox[2] - pbox[0]); 
                                                                var height = (pbox[1] - pbox[3]); 
                                                               var recognized = false; 
                                                                    if (inRange(width,height,595,842)) { numA4++; recognized = true; }   
                                                                    if (inRange(width,height,842,1191)) { numA3++; recognized = true; } 
                                                                    if (inRange(width,height,1191,1684)) { numA2++; recognized = true; }
                                                                    if (inRange(width,height,1684,2384)) { numA1++; recognized = true; }
                                                                    if (inRange(width,height,3502,4566)) { numA0++; recognized = true; }   
                                                             
                                                                if (!recognized) msgText += ("Page " + (pagenum+1) + " has a width of " + (width/72).toFixed(2) + " and a height of " + (height/72).toFixed(2) + "inches\n");  } 
                                                            msgText +=
                                                              numA4 + " A4 pages $" + (numA4*p).toFixed(2) + "\n"
                                                            + numA3 + " A3 pages $" + (numA3*p*2).toFixed(2) + "\n"
                                                            + numA2 + " A2 pages $" + (numA2*p*4).toFixed(2) + "\n"
                                                            + numA1 + " A1 pages $" + (numA1*p*8).toFixed(2) + "\n"
                                                            + numA0 + " A0 pages $" + (numA0*p*16).toFixed(2) + "\n"
                                                            + (this.numPages++ - (numA4+numA3+numA2+numA1+numA0)) + " other pages" + "\n"
                                                            + "subT $" + (numA4*p+numA3*p*2+numA2*p*4+numA1*p*8+numA0*p*16).toFixed(2) + "\n";
                                                            + "Admin $"+AF;
                                                            + app.alert(msgText);

                                                            ---

                                                            • 27. Re: java calculations
                                                              try67 MVP & Adobe Community Professional

                                                              I already told you what was wrong with that code, but you insist going back to it without making the modifications we mentioned.

                                                               

                                                              I'm also giving up. You need to either spend time learning basic JS syntax, or hire someone to do it for you. Good luck.

                                                              • 28. Re: java calculations
                                                                Thom Parker Adobe Community Professional

                                                                The line before, + "Admin $"+AF;

                                                                ends with a semi-colon.  This is why it is not being shown, remove the semicolon

                                                                 

                                                                 

                                                                • 29. Re: java calculations
                                                                  paula99520745 Level 1

                                                                  Thank you for having the patients to help, much appreciated.