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

Repeat Calculation Down Rows of Forms

Community Beginner ,
Dec 05, 2018 Dec 05, 2018

Copy link to clipboard

Copied

Hi all,

I have a PDF I'm putting together that is a product list with multiple rows, across multiple pages. It calculates Price x Order Quantity and gives a total in the last box. Example attached. Yes, I'm aware my version of Acrobat is getting very old

Is there a way to use the "create multiple copies" function but have the calculation update to match the new field name. As it is, the "cost" field wants to use the calculation from the first row and doesn't auto-update.

Hope that makes sense, cheers.

TOPICS
Acrobat SDK and JavaScript

Views

678

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 , Dec 06, 2018 Dec 06, 2018

Yes, it is possible. Here's how you do it:

Create your first row of fields with the basic names you want to use, WITHOUT any numbers at the end. For example, "Qty", "Price" and "Cost".

If you duplicate them (don't do it yet!) the new fields names will be "Qty.0", "Qty.1", etc., "Price.0", "Price.1", etc., and "Cost.0", "Cost.1", etc.

So the field names are consistent, which means we can write a script that will work in any row.

So now we go back to the original Cost field and use the following code

...

Votes

Translate

Translate
Community Expert ,
Dec 05, 2018 Dec 05, 2018

Copy link to clipboard

Copied

The answer is most likely yes, but it depends on your particular setup. The general solution is to create a folder level function that generates the field names that it needs based on the name of the current cost field.   For this technique to work, all the fields on the same row have to follow the same naming convention. This function is called from the cost field, so the name of the current cost field is in "event.targetName",  All it should take is some text operations to generate the names of the other fields on the same row.

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
Community Beginner ,
Dec 05, 2018 Dec 05, 2018

Copy link to clipboard

Copied

Thanks for the response Thom. I have to admit I've never touched the Javascript part of Acrobat before so this is all new to me. Are you able to provide an example I could test in my PDF?

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 ,
Dec 06, 2018 Dec 06, 2018

Copy link to clipboard

Copied

LATEST

Since you are new to JavaScript, it is more straight forward to add the calculation directly to the cost field, and then duplicate that field. However, if you ever need to make a change to the script, you'll need to make that change to every cost field, which is both time consuming and error prone. The best solution is to create a document level function to encapsulate this code. Then you only have to change the code in one place.

Here's a short article on the topic:

Basic Document Level (Document Open) Scripts

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
Community Expert ,
Dec 06, 2018 Dec 06, 2018

Copy link to clipboard

Copied

Yes, it is possible. Here's how you do it:

Create your first row of fields with the basic names you want to use, WITHOUT any numbers at the end. For example, "Qty", "Price" and "Cost".

If you duplicate them (don't do it yet!) the new fields names will be "Qty.0", "Qty.1", etc., "Price.0", "Price.1", etc., and "Cost.0", "Cost.1", etc.

So the field names are consistent, which means we can write a script that will work in any row.

So now we go back to the original Cost field and use the following code as its custom calculation script:

var baseName = "Cost";

var qtyFieldName = event.target.name.replace(baseName, "Qty");

var priceFieldName = event.target.name.replace(baseName, "Price");

var qtyField = this.getField(qtyFieldName);

var priceField = this.getField(priceFieldName)

event.value = Number(qtyField.valueAsString) * Number(priceField.valueAsString);

If you do that first and then duplicate the fields you'll see that the code continues to work for each row on its own.

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 Beginner ,
Dec 06, 2018 Dec 06, 2018

Copy link to clipboard

Copied

Thank you so much! I'll try this out tomorrow. I was looking at having to manually set the calculation for each row, over many many rows, on multiple pages - you're an absolute life saver mate, really appreciate 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