Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Assign the Date format to a field via JavaScript

Community Expert ,
Nov 02, 2019 Nov 02, 2019

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


Acrobate du PDF, InDesigner et Photoshoptographe
TOPICS
Acrobat SDK and JavaScript
12.6K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Nov 02, 2019 Nov 02, 2019

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
Community Expert ,
Nov 02, 2019 Nov 02, 2019

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 02, 2019 Nov 02, 2019

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 02, 2019 Nov 02, 2019

"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


Acrobate du PDF, InDesigner et Photoshoptographe
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 02, 2019 Nov 02, 2019

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

 

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

 

Capture_024.pngexpand image

 

Capture_025.pngexpand image


Acrobate du PDF, InDesigner et Photoshoptographe
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 02, 2019 Nov 02, 2019

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
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 24, 2021 Sep 24, 2021
LATEST

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
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 03, 2019 Nov 03, 2019

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".)

 


Acrobate du PDF, InDesigner et Photoshoptographe
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 03, 2019 Nov 03, 2019

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
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 03, 2019 Nov 03, 2019

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);}

 

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 04, 2019 Nov 04, 2019

Thank you Thom


Acrobate du PDF, InDesigner et Photoshoptographe
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 04, 2019 Nov 04, 2019

You're Welcome 🙂

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 04, 2019 Nov 04, 2019

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.


Acrobate du PDF, InDesigner et Photoshoptographe
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 04, 2019 Nov 04, 2019

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
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 05, 2019 Nov 05, 2019

"You have to look at the source code"

Where can I find it?


Acrobate du PDF, InDesigner et Photoshoptographe
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 05, 2019 Nov 05, 2019

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
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 05, 2019 Nov 05, 2019

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
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 05, 2019 Nov 05, 2019

Many hours of reading to come!

😉

Thank you.


Acrobate du PDF, InDesigner et Photoshoptographe
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines