Skip to main content
paula99520745
Known Participant
February 9, 2018
Answered

java calculations

  • February 9, 2018
  • 1 reply
  • 3445 views

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

This topic has been closed for replies.
Correct answer Thom Parker

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);

---


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

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

1 reply

Bernd Alheit
Community Expert
Community Expert
February 9, 2018

Acrobat uses JavaScript, not Java.

For a fixed value you can use a variable like:

var price = 1.23;

paula99520745
Known Participant
February 11, 2018

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?

try67
Community Expert
Community Expert
February 11, 2018

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".