• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

java calculations

Explorer ,
Feb 08, 2018 Feb 08, 2018

Copy link to clipboard

Copied

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

TOPICS
Acrobat SDK and JavaScript

Views

2.0K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Feb 15, 2018 Feb 15, 2018

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

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

Votes

Translate

Translate
Community Expert ,
Feb 09, 2018 Feb 09, 2018

Copy link to clipboard

Copied

Acrobat uses JavaScript, not Java.

For a fixed value you can use a variable like:

var price = 1.23;

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 11, 2018 Feb 11, 2018

Copy link to clipboard

Copied

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 11, 2018 Feb 11, 2018

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 11, 2018 Feb 11, 2018

Copy link to clipboard

Copied

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

-----

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 11, 2018 Feb 11, 2018

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 11, 2018 Feb 11, 2018

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 12, 2018 Feb 12, 2018

Copy link to clipboard

Copied

Works fine for me...

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 12, 2018 Feb 12, 2018

Copy link to clipboard

Copied

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 12, 2018 Feb 12, 2018

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 12, 2018 Feb 12, 2018

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 12, 2018 Feb 12, 2018

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 12, 2018 Feb 12, 2018

Copy link to clipboard

Copied

Thanks for the help. still did not run from console

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 12, 2018 Feb 12, 2018

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 12, 2018 Feb 12, 2018

Copy link to clipboard

Copied

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

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 15, 2018 Feb 15, 2018

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 15, 2018 Feb 15, 2018

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 15, 2018 Feb 15, 2018

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 15, 2018 Feb 15, 2018

Copy link to clipboard

Copied

Use Bernd's code from his last reply.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 15, 2018 Feb 15, 2018

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 15, 2018 Feb 15, 2018

Copy link to clipboard

Copied

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 15, 2018 Feb 15, 2018

Copy link to clipboard

Copied

You can use a variable AF

var AF = 5.0;

and access the variable

+ "Admin $" + AF;

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 15, 2018 Feb 15, 2018

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 15, 2018 Feb 15, 2018

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 15, 2018 Feb 15, 2018

Copy link to clipboard

Copied

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

---

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines