These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Calculation Javascript problems

kentwatchlight
Registered: Feb 13 2009
Posts: 30
Answered

I started a topic last week dealing with running calculation scripts on radio buttons that disapeer when a check box is unchecked:

http://www.acrobatusers.com/forums/aucbb/viewtopic.php?id=18562

These are the scripts that were offered to me by George_Johnson. From all outward appearences they seem to work.

Check box toggle action for radio buttons:

(function () {

// Get a reference to the radio button field
var f1 = getField("RadioButton4");

// Reset the radio button field
f1.value = f1.defaultValue;

// Show/Hide the radio buttons based on the state of this checkbox
f1.display = (event.target.value !== "Off") ? display.visible : display.hidden;

})();

Calculation script:

(function () {

// Get the text field values, as numbers
var v1 = +getField("Text5").value;
var v2 = +getField("Text6").value;

// Get the radio button value
var v3 = getField("RadioButton4").value;

// Set the value to zero if no radio buttons are selected
if (v3 === "Off") v3 = 0;

// Get the check box value
var v4 = getField("Checkbox4").value;

// Perform the calculation
if (v4 !== "Off") {
event.value = v1 + v2 + v3;
} else {
event.value = v1 + v2;
}

})();

This is the problem: I can only get the calculation script to work in preview. When Form Editing is closed and Saved with features extended to adobe reader, the file ONLY calculates the sum of Text5 & Text6, reguardless of the state of Checkbox4 or what button in RadioButton4 is selected, which of course renders the whole endeavor pointless. Furthermore, when I then reopen that file in Acrobat, and save a copy, and reopen Form Editing, from that point on the calculation does not work at all, even in preview. I have been screwing around with this problem for three days and am about at the end of my rope. Any help would be greatly appreciated.

Why would this only work in preview?

George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
Kent,

When I looked at that form, it behaved a bit oddly in Acrobat, and I suspect it got corrupted somehow. What you're describing is not normal and the code you're using is not the source of the problem, unless the field names are wrong. What I would do is start with a fresh PDF and copy & paste the fields and code. A bit of a nuisance, but that's all I can suggest. FWIW, your form is working fine on my system (Acrobat 9 Pro).George
kentwatchlight
Registered: Feb 13 2009
Posts: 30
Well, I went so far as to uninstall and reinstall Acrobat 9 Pro, all to no avail. Is it possible this problem is arising because the file originates in Word, and is saved through Word as a pdf before I start The Forms Wizard?

I have saved and resaved this file, under different names, returning to the original Word documetn and resaving from there . . . I wonder, George, if you could email me your working version, so i can see if it works on my machine?
kentwatchlight
Registered: Feb 13 2009
Posts: 30
With a little further investigation, what I find is happening here is that the PDF is being saved in whatever state it is left in at the end of form editing. In other words, If I save it with the checkbox checked and "1 sign" selected, the form is saved with that calculation, and it cannot be changed in reader, though I can still check and uncheck the box and select the number of signs. It is just that the calculation that is supposed to correspond with such a change is not recalculated. It stays with whatever calculation is made at the time the PDF form is saved. If I save it with no checkbox checked, and no sign selected, it saves that calculation. So it is not that the radio button section of the calculation is not being made. It is being made, but once it is saved in acrobat, that section is no longer working.
kentwatchlight
Registered: Feb 13 2009
Posts: 30
Nevermind it all, I figured it out. I am using Office 07. When I save the file in Office 07 and then convert it from Acrobat, or if I do the conversion straight from Word, even with compatibility mode on, that is when the problems arise.

I instead saved it in compatibility mode, reopened it with Word 03, resaved it, then opened it and converted it from acrobat and made the field changes. I don't know why, but this solved my problem. The calculations are now working correctly in reader.

Thanks for your help on this, George. You pointed me in the right direction as to where to look for the problem, and sure enough I found it. Kudos.