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

How to set Numeric Format and the number of decimals to a Field with Javascript?

Community Beginner ,
Jan 18, 2024 Jan 18, 2024

Copy link to clipboard

Copied

Hello community,

I created a button on Adobe PDF, so when the user click on it, it creates 3 fields per row(Amount of times it is clicked the button).

I set up the font size and the font using:  
newField.textFont = "Helvetica";
newField.textSize = 9;

And newField is: var newField =  this.addField(...)

Now, I want to know the code to give the instructions that newField should be "Numeric" Format and with 3 decimals:   5.000 (Example)

Thanks

TOPICS
How to , JavaScript , PDF , PDF forms , Rich media and 3D

Views

1.1K

Translate

Translate

Report

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
1 ACCEPTED SOLUTION
Community Expert ,
Jan 18, 2024 Jan 18, 2024

Copy link to clipboard

Copied

Change this block:

var customFormat = AFNumber_Format(3, 0, 0, 0, "", false);
var myField = this.getField(fieldName );
myField.setFormat(customFormat);

To:

var customFormat = "AFNumber_Format(3, 0, 0, 0, \"\", false)";
var myField = this.getField(fieldName);
myField.setAction("Format", customFormat);

View solution in original post

Votes

Translate

Translate

Report

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 ,
Jan 18, 2024 Jan 18, 2024

Copy link to clipboard

Copied

There isn't a direct command to do it. You would need to use the setAction method to specify the Format and Keystroke scripts for the field, and either write your own code that does it, or have knowledge of the undocumented functions that Acrobat uses to apply those settings.

Votes

Translate

Translate

Report

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 Beginner ,
Jan 18, 2024 Jan 18, 2024

Copy link to clipboard

Copied

Thanks for the response, do you have any website or link where I can find those undocumented functions (I know is undocumented) but maybe somewhere there is this info please.

Votes

Translate

Translate

Report

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 ,
Jan 18, 2024 Jan 18, 2024

Copy link to clipboard

Copied

You're asking about the AFNumber_Format function.  If you execute the function name in the Acrobat JavaScript console window you'll see the whole thing. 

https://www.pdfscripting.com/public/images/video/AcroJSIntro/AcroJSIntro_ConsoleWindow.cfm

 

You would be better served to just set the format using the "util.printf()" function. 

https://opensource.adobe.com/dc-acrobat-sdk-docs/library/jsapiref/JS_API_AcroJS.html#printf

 

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

Votes

Translate

Translate

Report

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 Beginner ,
Jan 18, 2024 Jan 18, 2024

Copy link to clipboard

Copied

Thanks to you guys, I tried my best, but I did not make it function 😞

My Javascript code is in a button, each time the user clicks on it, it adds a new row of fields, but the fields that are manually setted up as Numeric with 3 decimals since row number two(2) it is not functioning, it is without any format.

This is my JS code in the button:

var totalCount = 1;
var totalCount2 = 0;
for (var fieldNumber = 0; fieldNumber < numFields; fieldNumber ++)
{
if(getNthFieldName(fieldNumber).startsWith("CONTAINER_CONTAINER_NRO")){
totalCount++;
totalCount2++;
}
}

var offset = this.getOffset();
var fieldNames = this.getFieldNames();
for (var i = 0; i < fieldNames.length; i++)
{
var fieldName = fieldNames[i] + "_";
var rectLast = this.getField("CONTAINER_" + fieldName +totalCount2 ).rect;

var newField = this.addField("CONTAINER_" + fieldName + totalCount, "text", 1, [rectLast[0],rectLast[1] - offset ,rectLast[2],rectLast[3]-offset ]);

if(fieldName === "QUANTITY_VOL_"){
var customFormat = AFNumber_Format(3, 0, 0, 0, "", false);
var myField = this.getField(fieldName );
myField.setFormat(customFormat);
}
}

this.getField("REMOVE_CONTAINER").display = display.visible;

if(totalCount >= this.getMaxQuantityFields()){
this.getField("ADD_CONTAINER").readonly = true;
}

 

Inside the body of the 'if' it is where I want place my code that will give a Numeric Format and set up to 3 decimals to whichever value the user input into the field, the name of the field is: "QUANTITY_VOL_")

I do not have idea what I am doing wrong.

PDF_JS.png

Votes

Translate

Translate

Report

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 ,
Jan 18, 2024 Jan 18, 2024

Copy link to clipboard

Copied

Change this block:

var customFormat = AFNumber_Format(3, 0, 0, 0, "", false);
var myField = this.getField(fieldName );
myField.setFormat(customFormat);

To:

var customFormat = "AFNumber_Format(3, 0, 0, 0, \"\", false)";
var myField = this.getField(fieldName);
myField.setAction("Format", customFormat);

Votes

Translate

Translate

Report

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 Beginner ,
Jan 19, 2024 Jan 19, 2024

Copy link to clipboard

Copied

LATEST

Thanks to you all guys, this last help solved my problem.

I found documentation about this, it is in the following link, it is a PDF the information related to set a format and decimals is on page 68:
Acro6JSGuide.pdf(Review) - Adobe cloud storage

Votes

Translate

Translate

Report

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