Skip to main content
Inspiring
March 11, 2022
Answered

run a document level javascript function

  • March 11, 2022
  • 3 replies
  • 3622 views

I've defined a function thusly:

//-------------------------------------------------------------
//-----------------Do not edit the XML tags--------------------
//-------------------------------------------------------------

//<Document-Level>
//<ACRO_source>set_all_fields</ACRO_source>
//<ACRO_script>
/*********** belongs to: Document-Level:set_all_fields ***********/
function set_all_fields()
{
	for(var n; n<n; n++) {
		var fieldName = getNthFieldName(n); 
		var field = getField(fieldName);
		console.println(fieldName);
		
		switch(field.type) {
			case 'text' :
				field.fontFamily = 'Source Sans Pro';
				field.fontSize = 9;
				break;
			case 'checkbox':
				field.style = style.ch;
				break;
			default: console.println(field.type);
				break;
		}
	}
}

 

How do I call this function from the debugger? All I get is undefined in the console, which lets me believe that the function could not be find

This topic has been closed for replies.
Correct answer kingma-sbw

To answer my question:

 

Under "C:\Program Files (x86)\Adobe\Acrobat DC\Acrobat\Javascripts" create a file with a JavaScript extension (.js) Contents of this file:

/* inside C:\Program Files (x86)\Adobe\Acrobat DC\Acrobat\Javascripts */
app.addMenuItem({
	cName: "Set Fields",
	cParent: "Edit",
	cExec: "set_all_fields();"
});


function set_all_fields()
{
    console.show();

	for(var n=0; n<this.numFields; n++) {
		var fieldName = this.getNthFieldName(n);
		var field = this.getField(fieldName);
		console.println(fieldName);
		switch(field.type) {
			case 'text' :
				field.textFont = 'Arial';
				field.textSize = 9;
				break;
			case 'checkbox':
				field.style = style.ch;
				break;
			default: console.println(field.type);
				break;
		}
	}
}

 

which will set the font family and size for all text fields and a check mark for all checkboxes..

3 replies

try67
Community Expert
Community Expert
March 11, 2022

I hope you're not editing the scripts via the Edit All JavaScripts command. That can seriously screw up the file, sometimes beyond repair. Use ONLY the Document JavaScripts command to add, edit or delete the doc-level scripts.

kingma-sbwAuthorCorrect answer
Inspiring
March 11, 2022

To answer my question:

 

Under "C:\Program Files (x86)\Adobe\Acrobat DC\Acrobat\Javascripts" create a file with a JavaScript extension (.js) Contents of this file:

/* inside C:\Program Files (x86)\Adobe\Acrobat DC\Acrobat\Javascripts */
app.addMenuItem({
	cName: "Set Fields",
	cParent: "Edit",
	cExec: "set_all_fields();"
});


function set_all_fields()
{
    console.show();

	for(var n=0; n<this.numFields; n++) {
		var fieldName = this.getNthFieldName(n);
		var field = this.getField(fieldName);
		console.println(fieldName);
		switch(field.type) {
			case 'text' :
				field.textFont = 'Arial';
				field.textSize = 9;
				break;
			case 'checkbox':
				field.style = style.ch;
				break;
			default: console.println(field.type);
				break;
		}
	}
}

 

which will set the font family and size for all text fields and a check mark for all checkboxes..

Bernd Alheit
Community Expert
Community Expert
March 11, 2022

How does you call the function?

Info: following will not work:

for(var n; n<n; n++)

Inspiring
March 11, 2022

Thanks , I know. I want to run it in the debugger cause that is what debuggers are for.

 

How do I debug this function?

Inspiring
March 11, 2022
//-------------------------------------------------------------
//-----------------Do not edit the XML tags--------------------
//-------------------------------------------------------------

//<Document-Level>
//<ACRO_source>set_all_fields</ACRO_source>
//<ACRO_script>
/*********** belongs to: Document-Level:set_all_fields ***********/

function set_all_fields()
{
    console.show();

	for(var n; n<numFields; n++) {
		var fieldName = getNthFieldName(n); 
		console.println(fieldName);

		var field = getField(fieldName);
		switch(field.type) {
			case 'text' :
				field.fontFamily = 'Source Sans Pro';
				field.fontSize = 9;
				break;
			case 'checkbox':
				field.style = style.ch;
				break;
			default: console.println(field.type);
				break;
		}
	}
}


//</ACRO_script>
//</Document-Level>
Inspiring
March 11, 2022

Btw in the Debugger the list under scripts is empty:

 

Bernd Alheit
Community Expert
Community Expert
March 11, 2022

Have you opened the document?