Categories
Content Marketing Create Learn

How to Embed Interactive Calculations in Adobe PDF Documents

So I’m the poster child for writing niche content these days!  Here’s another one for those of you who want to put interactive calculators in your PDF documents, but need some more advanced functionality than basic arithmetic functions.  Like, who would read this stuff anyway??

Well, here goes… I ran into this when putting together my recent “Insider’s Guide to Paying for College” ebook, which I put together in InDesign and then exported into an interactive PDF document. When discussing the costs of college, I wanted to do better than having a static grid showing the anticipated future college costs in, say, 18 years at an interest rate of 5%.  That might work great for a reader who’s expecting their first child, but doesn’t really help when you have an 8 year old and want to know how much you need to be saving each month for college.

Insiders Guide InDesign Layout

Adding interactivity to an ebook is a great way to add value to the content.  This way your reader doesn’t have to click out of your ebook to another resource or have to download a separate excel spreadsheet and you’ve got total control over the format.  Adobe has a relatively easy way of embedding very basic mathematical functions in a PDF using the prepare form tool, but it’s not that user friendly for for anything more advanced than adding, subtracting, multiplying and dividing.  But the good news is that you can do it!

In my ebook, I’ve actually got 2 interactive calculators.  The first one calculates the future cost of college based on today’s published average tuitions using a time value of money calculation.  The second one calculates the future value of a monthly savings amount invested over time.  This one used a future value of an annuity calculation.

Both calculators allow the reader to enter a several variables including interest rate, time (in one case years and in another case months), and amounts, depending on which calculator you’re using.  In this example, I’m going to use the first calculator, but you’re welcome to download the ebook and have a look at the other calculation.

So what’s the process you ask?  Well, I’m glad you did!

I designed the ebook in InDesign and made a table for the calculator.  Actually, on this page, I made 2 tables, one for the user defined variables and one for the actual calculations.  You can see that the calculation table is a 4 row by 4 column table and has an additional header row.

Future Value InDesign Layout
This calculator is made of two tables in InDesign

I created all of my static information and formatted it the way that I wanted it to look using InDesign, but once I export the entire ebook to a PDF document, I’ll go into Acrobat Pro DC and use the Prepare Form tool to create fields in all of the empty table cells.

Acrobat DC Tools
Access the “Prepare Form” tool in Acrobat DC

For this table, I created 10 fields: an Interest Rate field (IntRate) and a Years field for the user defined variables.  Then corresponding fields for the annual cost: TwoYear, FourYearIn (for instate tuition), FourYearOut (for out of state) and Private.  Then, I wanted to multiply each of these by 4 to get a total 4 year cost based on the future value calculation, so I created 4 more fields: TwoYearTotal, FourYearInTotal, FourYearOutTotal and PrivateTotal.  These 4 fields will use the simple calculation functionality that pretty much anyone can do if you’ve ever used basic Excel formulas.  The first 4 are more complex and involve creating some scripts, which may sound scary, but really aren’t that difficult once you get the basic jist of how they work.  Hopefully my code will help you logically think through the calculation so you can use the principles to create your own calculations.

Acrobat Fields
This form consists of 10 fields. They are sized to fit perfectly in the table that was originally created in InDesign and exported to Acrobat.

It will also help you to give your fields names that make sense as you build out a complex formula, which I’ll show you below.

For each of the fields I created, I set the Text Field Properties to match what I created in InDesign.  Note that the tabs on the Text Field Properties box will give you a lot of formatting options including appearance, position, format and other stuff.  The main one that we’re going to focus on is the Calculate tab at the end.

Text Field Formatting
Use the text field properties to format the data fields to match the formatting of the PDF.
Text Field Properties
Use the “Custom Calculation Script” field in the Text Field Properties box to add code for more complex calculations like this future value calculation.
Custom Calculation Script
Type (or copy) your code into the JavaScript editor in the Text Field Properties.

I’ll start with the more complicated calculation since you can probably figure out the simple calculation (but I’ll go over that below just in case).

Acrobat Pro DC allows you to create JavaScript code to embed in the field properties, so that’s how you’ll need to create a more advanced function.  By ‘more advanced’ I mean anything that’s got more than the basic 4 operators.  In this case, the future value calculation involves using an exponent, so that’s why I couldn’t use the basic function.

The basic Future Value calculation is Future ValuePresent Value * ((1+ Interest Rate) ^ Years).  In this formula, there are 3 variables (with the Present Value being the published yearly tuition and fees today).

So here’s the code I used:

var interestField = this.getField("IntRate");
var yearsField = this.getField("Years");
var TwoYear = this.getField("TwoYear");
var FourYearIn = this.getField("FourYearIn");
var FourYearOut = this.getField("FourYearOut");
var Private = this.getField("Private");
var TwoYearTotal = this.getField("TwoYearTotal");
var FourYearInTotal = this.getField("FourYearInTotal");
var FourYearOutTotal = this.getField("FourYearOutTotal");
var PrivateTotal = this.getField("PrivateTotal");


function calculateFutureValue(intRate, years, pubRate) {
    return (Math.pow((1 + intRate), years) * pubRate);
}

function calculateTotalCost(rate, periods) {
   return (rate * periods)
}
 
if (interestField.value == '' || interestField.value == null) {
    interestField.value = 0;
}

if (yearsField.value == '' || yearsField.value == null) {
    yearsField.value = 0;
}

if (interestField.value != '' && interestField.value != null && yearsField.value != '' && yearsField.value != null) {
    if (interestField.value == 0 && yearsField.value == 0) {
        TwoYear.value = 0; FourYearIn.value = 0; FourYearOut.value = 0; Private.value = 0; TwoYearTotal.value = 0; FourYearInTotal.value = 0; FourYearOutTotal.value = 0; PrivateTotal.value = 0; 
    } else {
        TwoYear.value = calculateFutureValue(interestField.value, yearsField.value, 3347);
        FourYearIn.value = calculateFutureValue(interestField.value, yearsField.value, 9139);
        FourYearOut.value = calculateFutureValue(interestField.value, yearsField.value, 22958);
        Private.value = calculateFutureValue(interestField.value, yearsField.value, 31231);
        TwoYearTotal.value = calculateTotalCost(TwoYear.value, 4);
        FourYearInTotal.value = calculateTotalCost(FourYearIn.value, 4);
        FourYearOutTotal.value = calculateTotalCost(FourYearOut.value, 4);
        PrivateTotal.value = calculateTotalCost(Private.value, 4);
    }
} else {
    TwoYear.value = 0; FourYearIn.value = 0; FourYearOut.value = 0; Private.value = 0; Private.value = 0; TwoYearTotal.value = 0; FourYearInTotal.value = 0; FourYearOutTotal.value = 0; PrivateTotal.value = 0;
}

There are a couple things about this code that are important to point out.  It was written such that it will return the entire calculation for all of the calculation fields.  As an alternative, it could be written specifically for each calculated field, in which case the script/formula would be shorter, but different.  The way it’s coded above is such that you can assign it to either the 2 variable fields (Years, IntRate) OR you can assign it to each of the 4 calculated fields (TwoYear, FourYearIn, FourYearOut, Private), but not both sets of fields.  The choice is yours :).

To calculate the totals fields, this is a simple arithmetic calculation and you can use the Simplified Field Notation option in the Calculation tab on the Text Field Properties box.  Here you can use simple operators like you might find in Excel or Numbers to do basic formulas.  In the example, you can see that I’m multiplying the TwoYear field by 4.  With the Simplified Field Notation feature you can do basic addition, subtraction, multiplication and division without having to code any scripts.

Simplified Field Notation
Use the Simplified Field Notation for simple arithmetic calculations.

And there you have it.  Special thanks to developer Jon Hart for helping to figure out this JavaScript code!

4 replies on “How to Embed Interactive Calculations in Adobe PDF Documents”

Hi,

Im trying to use the math power on the calculation on acrobat DC pro, But it dosent work.

This how I type in the

Math.pow(2, 5)

but is just doesnt work.

Thanks

I just your article on inactive formulas in pdf files
i have a pdf fillable form i would like to add some formulas to simple multiplying i was wondering if you could help with it and what costs would be

Thanks mike

Leave a Reply

Your email address will not be published. Required fields are marked *