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

run a document level javascript function

Contributor ,
Mar 11, 2022 Mar 11, 2022

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

TOPICS
Create PDFs , JavaScript , PDF forms
3.0K
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
2 ACCEPTED SOLUTIONS
Community Expert ,
Mar 11, 2022 Mar 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.

View solution in original post

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
Contributor ,
Mar 11, 2022 Mar 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..

View solution in original post

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
Contributor ,
Mar 11, 2022 Mar 11, 2022

Btw in the Debugger the list under scripts is empty:

kingmasbw_0-1646986961346.pngexpand image

 

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 ,
Mar 11, 2022 Mar 11, 2022

Have you opened the document?

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 ,
Mar 11, 2022 Mar 11, 2022

How does you call the function?

Info: following will not work:

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

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
Contributor ,
Mar 11, 2022 Mar 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?

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
Contributor ,
Mar 11, 2022 Mar 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>
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 ,
Mar 11, 2022 Mar 11, 2022

Use this:
for(var n = 0; n<numFields; n++) {

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
LEGEND ,
Mar 11, 2022 Mar 11, 2022

"Undefined" does not mean that it didn't find the function. It's normal. To try this out I suggest you add an app.alert call to your document level script. 

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 ,
Mar 11, 2022 Mar 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.

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
Contributor ,
Mar 11, 2022 Mar 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..

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
Contributor ,
Mar 14, 2022 Mar 14, 2022
LATEST

Thanks for the tip @try67 

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