Repeat Calculation Down Rows of Forms

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.

Most Valuable Participant
Correct answer by try67 | Most Valuable Participant

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.

TOPICS
Acrobat SDK and JavaScript

Views

161

Likes

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

Repeat Calculation Down Rows of Forms

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.

Most Valuable Participant
Correct answer by try67 | Most Valuable Participant

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.

TOPICS
Acrobat SDK and JavaScript

Views

162

Likes

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
Dec 05, 2018 0
Adobe Community Professional ,
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.

Likes

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
Reply
Loading...
Dec 05, 2018 1
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?

Likes

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
Reply
Loading...
Dec 05, 2018 0
Adobe Community Professional ,
Dec 06, 2018

Copy link to clipboard

Copied

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

Likes

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
Reply
Loading...
Dec 06, 2018 0
Most Valuable Participant ,
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.

Likes

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
Reply
Loading...
Dec 06, 2018 2
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.

Likes

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
Reply
Loading...
Dec 06, 2018 0