Skip to main content
Known Participant
April 12, 2021
Question

Actual script for Sum of Fields

  • April 12, 2021
  • 3 replies
  • 6859 views

I feel like this should be easy to find, but for some reason it isn't.

 

If I have fields named Strata.1, Strata.2, Strata.3, Strata.4 etc... and I want to return the sum of all the Strata fields in a field named Strata_Total.

I know I can use the pull down menu and just choose Sum and select the checkbox for Strata.

 

But what is the actual script that is happening behind the curtain?

 

This topic has been closed for replies.

3 replies

JR Boulay
Community Expert
June 16, 2023

Yes, I realised that too late.

😉

Acrobate du PDF, InDesigner et Photoshopographe
JR Boulay
Community Expert
June 16, 2023

You don't need JavaScript for such a simple calculation.
In the attached PDF you can see the calculation in the text field and the export values in the checkboxes.

 

Acrobate du PDF, InDesigner et Photoshopographe
Inspiring
June 16, 2023

@JR Boulaythat is old post.

Thom Parker
Community Expert
April 12, 2021

You can read about basic field scripting here:

https://www.pdfscripting.com/public/PDF-Form-Scripting.cfm

 

And specically about calculations here:

https://acrobatusers.com/tutorials/how-to-do-not-so-simple-form-calculations/

 

But since you've done such a great job of naming your fields, here's an advanced calculation script:

 

var sum = 0;
this.getField("Strata").getArray().forEach(a=> sum+=Number(a.value));
event.value = sum;
Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
try67
Community Expert
April 13, 2021

I would advise against using this type of notation. It's not compatible with earlier versions of Acrobat and Reader and will not work there. Instead, you should stick to plain "vanilla" JS syntax, like this:

 

var sum = 0;
var fields = this.getField("Strata").getArray();
for (var i in fields) sum+=Number(fields[i].value);
event.value = sum;
New Participant
June 15, 2023

Hi There!

 

I am completely new to Java and working through building a PDF menu that will be sent through email and also accessed online for ordering purposes. I have a ton of fields (imagine foods on a restaraunt menu) and a variety of pricing for each item. I have created editable fields for the quantities of each item, which would be filled out by the person placing the order, and I have the pricing visible separately, not listed in a field.

 

Is there a way to create script to calculate each field by it's own price and then total at the bottom next to my "submit order button" that I've already connected?

 

This is the text I've entered into my Product_Total calculation script:

 

event.value = Number(this.getField("Product.1").valueAsString) * 12;

event.value = Number(this.getField("Product.2").valueAsString) * 10.5; event.value = Number(this.getField("Product.3").valueAsString) * 12; event.value = Number(this.getField("Product.4").valueAsString) * 9.5; event.value = Number(this.getField("Product.5").valueAsString) * 12; event.value = Number(this.getField("Product.6").valueAsString) * 9.5;

 

However, this doesn't create a summed total in the Product_Total box, just the last number in the script (9.5)

 

What should I be doing differently? Again, I know almost nothing at this stage, just learning by reading online and trial and error with the calculations, so any pointers would be helpful! Thank you in advance!