So I want some JavaScript code to run at Document Open, so I have placed my .js file into my Acrobat\JavaScripts folder (in Program Files). I'm trying to run the following code:
function ChangeFont()
{
var nNumFields = this.numFields;
console.println("There are " + nNumFields + " in this document.");
var cFieldName; var oField;
for(i = 0; i < nNumFields; i++)
{
cFieldName = this.getNthFieldName(i);
oField = this.getField(cFieldName);
oField.textFont = "PTSans";
}
}
ChangeFont()
Opening a PDF runs other code I have inserted into the same folder, but not this one (as the fonts do not change). When manually pasting this code into the Acrobat JavaScript Debugger, selecting all of the code and hitting Ctrl+Enter, it works perfectly. So why isn't it working at Document open??
I tried to isolate the problem by making a separate .js that selects one specific field and changes it's font, like so:
var f = this.getField("myField");
f.textFont = "PTSans";
After placing the the test.js into the JavaScripts folder, and opening a PDF document with Acrobat, I get the following error immediately:
"TypeError: "this.getField" is not a function."
Again, I ran it manually in the debugger console and it works perfectly.
What's going on here? I don't think I'm using an LiveCycle Designer interface, it's just Adobe Acrobat 10.
Edit: The purpose is to have all the fields at a certain font when the document is opened.
Scripts at the folder level run when Acrobat loads but before a document is opened. The numFields property belongs to the Doc object, which doesn't exist yet. Just remove the last line and you'll be able to run the function from buttons within the document.