character limit via java

Hi, I need to change the character limit of many text fields via script (maybe a document level script?).
And more in general how can I insert action/custom validation/custom keystroke/custom calculation in many fields via only one script (again, maybe a document level script?), what's the syntax?
Thank you


Kripke Robert


5 Answers

Voted Best Answer

What I do in a case like that is to have each line be it's own string. You will need to escape (as \") all quotes in your script.

Let's say I have this script:

for (var i=0; i<5; i++) {
    console.println("The current value of i is " + i);
}

To assign that to an action, I would create a new string variable consisting of concatenations of the individual lines plus a newline character:

var script = "for (var i=0; i<5; i++) {\n" +
    "console.println(\"The current value of i is \" + i);\n" +
    "}\n";

You can now use that variable in creating your action.

Karl Heinz Kremer
PDF Acrobatics Without a Net
PDF Software Development, Training and More...
http://www.khkonsulting.com


By Karl Heinz Kremer   

If this is a one time event or rare event, I would create and run the code in the JavaScript console.

If you need to perfrom this task on many different files. I would use the Actoin Wizard or create a custom tool bar button or menu item to run the code. I most likely would write an applcation folder file that contained the function to perform the task. I would then wrie additonal code seperate from the function that would add the menu item or add the toolbar button.

Have you looked at the tutorials on this site?

To adjust the character length you will need to:

Access the field object this.getField() And then set character limit property

References for Acrobat JavaScript and JavaScript;

Acrobat DC JavaScript

MDN JavaScript Reference


George Kaiser   

You need to review the Acrobat JavaScript documentation, it contains all the information - and sample code - you will need.

In general, if you want to process all fields (or many fields) in a document, you can use something like this:

for (var i=0; i<this.numFields; i++) {
    f = this.getField(this.getNthFieldName(i));
    if (f.type == "text") {
        // do something with this text field
    }
}

To change the character limit, look up the Field object in the SDK, and find the correct property (it's charLimit): http://help.adobe.com/en_US/acrobat/acrobat_dc_sdk/2015/HTMLHelp/index.html#t=Acro12_MasterBook%2FJS_API_AcroJS%2FField_properties.htm%23TOC_charLimitbc-10&rhtocid=_6_1_8_31_1_9

So, so change the charLimit for your fields to e.g. 10, just replace the // do something line with this:

f.charLimit = 10;

To change scripts, look for the Field.setAction() method: http://help.adobe.com/en_US/acrobat/acrobat_dc_sdk/2015/HTMLHelp/index.html#t=Acro12_MasterBook%2FJS_API_AcroJS%2FField_methods.htm%23TOC_setAction2bc-17&rhtocid=_6_1_8_31_2_16

Karl Heinz Kremer
PDF Acrobatics Without a Net
PDF Software Development, Training and More...
http://www.khkonsulting.com


Karl Heinz Kremer   

The script to change the character limit is:

this.getField("theFieldName").charLimit=5;

If your fields are name similarily, you can loop through the fields like this:

for (var i=1;i<11;i++)
{
this.getField("Text"+i).charLimit=5;
}

The above scripts assumes 10 fields named "Text1" through "Text10". If there is no consistency in field naming, you can select multiple fields in field editing mode and manually set the character limit in the options tab.

________________________________________________________________________________________________________________ www.PDFAutomationStation.com

IMAGE ALT TEXT IMAGE ALT TEXT IMAGE ALT TEXT

_________________________________________________________________________________________________________________


David Dagley   

Thank you Karl and David, .charLimit is what I missed.


As for .setAction , I understand the syntax:

var f = this.getField("text1");
f.setAction ("OnBlur", " ..... ")


But I have a complex script to add to "text1" field but I'm not sure where to put quotes! The script is:

var newDate = event.value;
if (newDate != "") {
var oMyDate = [];
oMyDate[0] = util.scand("mmddyyyy", newDate);
oMyDate[1] = util.scand("mmddyy", newDate);
oMyDate[2] = util.scand("mddyyyy", newDate);
oMyDate[3] = util.scand("mmdyyyy", newDate);
oMyDate[4] = util.scand("mdyyyy", newDate);
oMyDate[5] = util.scand("mdyy", newDate);
var isDate = false;

for (var i=0; i<oMyDate.length; i++) {
if (oMyDate[i] !== null) {
event.value = util.printd("mm/dd/yyyy", oMyDate[i]);
isDate = true;
break;
}
}

if (isDate === false) {
app.alert("Invalid date entered! Expected mm/dd/yyyy", 0, 1, "Date Validation");
event.value = "";
}
}


Kripke Robert   


Please specify a reason: