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

Using Javascript in a form field

mitch54
Registered: Jun 1 2010
Posts: 30

I am fairly new at both Acrobat AND Javascript. Can anyone tell me how to use it in a field? Do I need to cal a function in the field? I'm using dates that are formatted in the field-do I need to include the format in the script? I'm MORE than a little confused....

My Product Information:
Acrobat Pro 8.1, Windows
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
You do not need to call a function in a field, it is one type of JS statement that could be used. Functions are used to reduce the amount of code for an entire PDF reusing the code many times by a special reference.

As to dates, dates are character strings and nothing more. If you want to do perform date arithmetic, you either need to split the month, day and year from the string and then manipulate each piece. Or you need to convert the date string to a date object and then use the properties to obtain the specific data you need for additional processing.

If this is an early attempt at writing JS, date arithmetic is fairly complex and you might want to learn more about how JS works on simple calculations before you start trying to manipulate date and time.

For example:

Exactly, how many days are in a week?
Exactly, how many days are in a month?
Exactly, how many days are in a year?

The only item with an answer in a whole number is 7 days in a week.

There is no exact number of days in a month. And for February there is no consistent number of days in that month. Every four years there is an extra day , except for most centuries which 3 of 4 are not leap years.

For time sheets I use fictions because there is a lot of repeated code for computing the difference between the start and end of the many time periods. And then there is the special formatting needed to display the result for each day.

George Kaiser

mitch54
Registered: Jun 1 2010
Posts: 30
You sent me to a site with dates and times on it the other day and I appreciate it. What I'm trying to do is populate a form with dates, with each being the next date. I'm trying to add days to a beginning day. I was using a calculation like this:

var oneDay = 24*60*60*1000;
var firstMonday = msBeginningDate + oneDay;
var newDate = new Date(msfirstMonday);
this.getField("firstMonday").value = msfirstMonday;

this is what I'm getting on the console:

f has no properties
f has no properties
msfirstMonday is not defined
3:AcroForm:firstMonday:Calculate
msfirstMonday is not defined
3:AcroForm:firstMonday:Calculate

Not sure what "f" is that has no properties. Do I need to redefine the variable msBeginningDate every time? What about oneDay? The variable msBeginningDate is defined in the first field. This is the code I'm using for msBeginningDate:

var BeginningDate = new Date();
var msBeginningDate = BeginningDate.getTime();

Can you help me any of this? I appreciate all of your help. I was kind of handed a partially finished job and I'm not exactly sure where they were going, so I tried to at least start most of it over. It is, indeed, a timesheet and I have all of the calculations for the hours already done....this is the last part.
mitch54
Registered: Jun 1 2010
Posts: 30
Also, how do I shut off the debugger? Every time I try to tab in the form the debugger console pops up, even when it's not in edit mode.
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
You have an error in your script, and every time a field used within a calculation is updated all the calculation scripts are processed. You need to fix your error.

Where is the variable 'msBeginningDate' and what is its value?

From what I can tell it does not exist.

If you add some additional code to your script to show the results of each line of code:
// define one dayvar oneDay = 24*60*60*1000;console.println('oneDay: ' + oneDay);console.println('msBeginningDate: ' + msBeginningDate);var firstMonday = msBeginningDate + oneDay;console.println('firstMonday: ' + firstMonday);var newDate = new Date(msfirstMonday);console.println('newDate: ' + newDate);this.getField("firstMonday").value = msfirstMonday;

I get:

Quote:
msBeginningDate is not defined
4:Field:Calculate
msBeginningDate is not defined
4:Field:Calculate
There is also no 'msfirstMonday' variable.

The article I pointed you to is in the Learning Center on this site.

George Kaiser

Dimitri
Expert
Registered: Nov 1 2005
Posts: 1389
Hi mitch54,

Since PDF scripting is so new for you, you may want to check out our membership training site www.pdfscripting.com where there are video series on both Basic Acrobat JavaScript and Basic Core JavaScript. The videos walk you through how to set up for scripting, the different kinds of scripts you can write, how to set up the JS debugger in Acrobat, etc. The site also inlcudes many other resources including sample files with scripts for a wide variety of scripting tasks.

The training videos require a paid membership, but there are lots of free resources there too.

Hope this helps,

Dimitri
WindJack Solutions
www.pdfscripting.com
www.windjack.com