Skip to main content
JR Boulay
Community Expert
Community Expert
Nov 2, 2019 7:05 PM UTC
Answered

Assign the Date format to a field via JavaScript

Hi

 

Is it possible to assign the Acrobat's buit-in Date format to a field via JavaScript?
I tried several things but without success:

this.getField("myField").setAction("Format", Date);

this.getField("myField").setAction("Format", "date(fr){DD MMMM YYYY}");

Etc.

 

Thank you

This topic is closed to new replies. Start a new post to keep the conversation going.
try67
try67Correct answer
Community Expert
Nov 2, 2019 10:27 PM UTC

It's possible, if you use one of the options in the list. If you use a format that doesn't appear in the list it will be defined as "Custom". You can do it using this code:

 

var f = this.getField("myField");
var cFormat = "mm/dd/yyyy";
f.setAction("Format", "AFDate_FormatEx(\""+cFormat+"\")");
f.setAction("Keystroke", "AFDate_KeystrokeEx(\""+cFormat+"\")");

17 replies

JR Boulay
Community Expert
JR BoulayCommunity ExpertAuthor
Community Expert
Nov 5, 2019 6:06 PM UTC

Many hours of reading to come!

😉

Thank you.

Translate
Acrobate du PDF, InDesigner et Photoshopographe
JR Boulay
Community Expert
JR BoulayCommunity ExpertAuthor
Community Expert
Nov 5, 2019 12:32 PM UTC

"You have to look at the source code"

Where can I find it?

Translate
Acrobate du PDF, InDesigner et Photoshopographe
ls_rbls
Community Expert
Community Expert
Nov 5, 2019 3:34 PM UTC

Hi, 

 

Check here in this guide:

http://www.acrotex.net/blog/wp-content/uploads/2011/07/pdfblog_22.pdf 

 

Pages 9 through 13 --->>>>    15.2. Format Category: Number

 

this pdf guide has examples with source code and fields that you can practice on

Translate
Technical Generalist
ls_rbls
Community Expert
Community Expert
Nov 5, 2019 3:38 PM UTC

And in the Acrobat Forms API Reference

 

There is a link in this page to download:

http://www.lystech.com/webhelp/Content/format-pdf-text-box/advanced.htm 

Translate
Technical Generalist
JR Boulay
Community Expert
JR BoulayCommunity ExpertAuthor
Community Expert
Nov 4, 2019 11:17 PM UTC

I tried this, but it just does nothing…

I cannot figure how to assign the Zip code format.

 

f.setAction("Format", "AFZipEntryRegExp");
f.setAction("Keystroke", "AFZipCommitRegExp");

 

Please, please, please.

Translate
Acrobate du PDF, InDesigner et Photoshopographe
try67
Community Expert
Community Expert
Nov 4, 2019 11:24 PM UTC

You have to look at the source code of each function/property to find out what it does.

I looked it up in my own code (from this paid-for tool I've created: http://try67.blogspot.com/2012/06/acrobat-apply-format-to-multiple-fields.html) and the methods you need to use are called AFSpecial_Format and AFSpecial_Keystroke.

Translate
try67
Community Expert
Community Expert
Nov 3, 2019 10:23 PM UTC

There are all undocumented methods, built-in to the application. I don't recall at the moment the names of the methods for Zip Code and the rest, but it should be possible.

Translate
Thom Parker
Community Expert
Community Expert
Nov 3, 2019 10:47 PM UTC

All of the custom formatting functions are defined in folder level scripts that ship with Acrobat. In later versions these script are encoded into an intermediate binary format, but if you have an older version of acrobat sitting around, just look in the app JavaScript folder. There's lots of interesting undocumented code. 

 

You can also find these functions by running this script in the console.

 

for(nm in this){if(/^AF/.test(nm))console.println(nm);}

 

 

 

Translate
Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
JR Boulay
Community Expert
JR BoulayCommunity ExpertAuthor
Community Expert
Nov 4, 2019 6:14 PM UTC

Thank you Thom

Translate
Acrobate du PDF, InDesigner et Photoshopographe
JR Boulay
Community Expert
JR BoulayCommunity ExpertAuthor
Community Expert
Nov 3, 2019 8:50 PM UTC

try67 you are a JavaScript JEDI !

 

It works fine.

I have questions more:

- what about for a "Zip code" format field? (I need it too)

- what about for another built-in format? (just because I'm curious)

- where can I find documentation about "AFDate", "FormatEx" and "KeystrokeEx"?

(There is nothing related in the "Developing Acrobat® Applications Using JavaScript™" nor in the "JavaScript™ for Acrobat® API Reference".)

 

Translate
Acrobate du PDF, InDesigner et Photoshopographe
try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
Nov 2, 2019 10:27 PM UTC

It's possible, if you use one of the options in the list. If you use a format that doesn't appear in the list it will be defined as "Custom". You can do it using this code:

 

var f = this.getField("myField");
var cFormat = "mm/dd/yyyy";
f.setAction("Format", "AFDate_FormatEx(\""+cFormat+"\")");
f.setAction("Keystroke", "AFDate_KeystrokeEx(\""+cFormat+"\")");

Translate
vgvallee
Participant
Sep 24, 2021 8:16 PM UTC

Very helpful post. Here are some more informations in case someone else has the requirements as mine.

 

Using AFDate_FormatEx and AFDate_KeystrokeEx worked perfectly in cases where a specific format is required. However, I had the case where many formats were to be accepted and the error message provided by the "Keystroke" event had to be in the language of the file used (and not the program language). I wanted to roll my own functions to setAction but was running into the problem where the calendar drop down would not be shown on the field or would not always work. Turns out that the code to be supplied to setAction with "Format" has to be a function starting with "AFDate_FormatEx". So this code does not show the drop down:

f.setAction("Format", "some_function(\""+cFormat+"\")");

whereas this one does:

f.setAction("Format", "AFDate_FormatEx_some_function(\""+cFormat+"\")");

Double quotes should be used around the argument as the use of the single quotes like the call below seemed to be a problem:

f.setAction("Format", "AFDate_FormatEx_some_function('"+cFormat+"')");

The calendar drop down is shown the first time but it will not work drop anymore after one use. Same if the function call does not have a string parameter:

f.setAction("Format", "AFDate_FormatEx_some_function()");

The "Keystroke" event can have any name and no or any number of parameters.

Translate
JR Boulay
Community Expert
JR BoulayCommunity ExpertAuthor
Community Expert
Nov 2, 2019 8:50 PM UTC

"What do you mean by "Acrobat's buit-in Date format"?"

 

I mean not a custom JavaScript format. See attached screenshots.

 

 

Translate
Acrobate du PDF, InDesigner et Photoshopographe
JR Boulay
Community Expert
JR BoulayCommunity ExpertAuthor
Community Expert
Nov 2, 2019 8:38 PM UTC

"Is the Acrobat built-in format considered the same as the current system date?"

 

I would prefer to be able to choose the date format more directly, but otherwise it could be appropriate

Translate
Acrobate du PDF, InDesigner et Photoshopographe
try67
Community Expert
Community Expert
Nov 2, 2019 8:23 PM UTC

What do you mean by "Acrobat's buit-in Date format"?

Translate
ls_rbls
Community Expert
Community Expert
Nov 2, 2019 7:10 PM UTC

Is the Acrobat built-in format considered the same as the current system date?

Translate
Technical Generalist