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

Take the pop-up selection from the dialog box and process it in a text field

Explorer ,
Dec 12, 2024 Dec 12, 2024

Hello,
using a dialog box in which I can make a selection, I would like to filter out the selection made in order to be able to perform further operations with it.
In the Adobe AcrobatDC JavaScript API reference, example #3 is shown, which I used to select a font from several fonts.
Example 3: This example uses a list box or a popup box. 
But I am not able to format the text of a form field with the selected font.
How do I get the font assigned in the 'f.textFont =' option so that the text appears in the selected font?

function PlaceText() {

    var dialog3 = {
        // This dialog box is called when the dialog box is created
        initialize: function (dialog) {
            this.loadDefaults(dialog);
        },
        // This dialog box is called when the OK button is clicked.
        commit: function (dialog) {
            // See the Dialog object for a description of how dialog.load
            // and dialog.store work.
            var elements = dialog.store()["subl"];
            // do something with the data.
            for (var s in elements) {
                if (elements[s] > 0) {
                    console.println("Gewählter Schriftfont ist: " + s + ", which has a value of " + elements[s]);
                }
            }
        },
        // Callback for when the button "butn" is clicked.
        butn: function (dialog) {
            var elements = dialog.store()["subl"]
                for (var s in elements) {
                    if (elements[s] > 0) {
                        app.alert("You have selected: " + s
                             + ", which has a value of " + elements[s]);
                    }
                }
        },
        loadDefaults: function (dialog) {
            dialog.load({
                subl: {
                    "CourierStd": +1,
                    "Helvetica": -2,
                    "Times-Roman": -3,
                }
            })
        },
        // The dialog box description
        description: {
            name: "Font selection", // Title of the dialog box
            elements: // Child element array
            [{
                    type: "view",
                    align_children: "align_left",
                    elements: // Child element array
                    [{
                            type: "cluster",
                            name: "Select",
                            elements: // Child Element Array
                            [{
                                    type: "static_text",
                                    name: "Select a font",
                                    font: "default"
                                }, {
                                    type: "popup", // Aufklappmenü
                                    item_id: "subl",
                                    width: 200,
                                }, {
                                    type: "button",
                                    item_id: "butn",
                                    name: "Press Me"
                                }
                            ]
                        }, {
                            type: "ok_cancel"
                        }
                    ]
                }
            ]
        }
   }

    //    if (dialog3.DoDialog() == "ok") {


    // var WunschFont = s
    var re = /.*\/|\.pdf$/ig;
    var FileNM = this.path.replace(re, "") + ".pdf";
    var Path = this.path;

    var f = this.addField("header1", "text", 0, [100, 250, 150, 200]);
    f.textColor = color.black;
    f.textSize = 12;
//    f.textFont = "Helvetica";
    f.textFont = s;  //  ????????????????
    f.strokeColor = color.transparent;
    f.fillColor = color.transparent;
    f.value = FileNM;
    f.alignment = "left";

app.execDialog(dialog3);

}
PlaceText()
TOPICS
Acrobat SDK and JavaScript
694
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

correct answers 1 Correct answer

Community Expert , Dec 15, 2024 Dec 15, 2024

Works fine for me, but there's an error in your code that will cause the final block to execute no matter what button you click in the dialog. You must move the opening curly bracket directly after the if statement. Otherwise, only the first line following the if-condition (the "speak to me" alert, in this case) will be connected to it. All the rest of the code will execute no matter what.

Translate
Community Expert ,
Dec 12, 2024 Dec 12, 2024

For starters, you have to first display the dialog, and only then use the values entered it in, which you can save to a variable declared outside of it, like this:

 

var selectedFont;
var dialog3 = {
	// This dialog box is called when the dialog box is created
	initialize: function (dialog) {
		this.loadDefaults(dialog);
	},
	// This dialog box is called when the OK button is clicked.
	commit: function (dialog) {
		// See the Dialog object for a description of how dialog.load
		// and dialog.store work.
		var elements = dialog.store()["subl"];
		// do something with the data.
		for (var s in elements) {
			if (elements[s] > 0) {
				console.println("Gewählter Schriftfont ist: " + s + ", which has a value of " + elements[s]);
				selectedFont = s;
				break;
			}
		}
	},
	// Callback for when the button "butn" is clicked.
	butn: function (dialog) {
		var elements = dialog.store()["subl"]
			for (var s in elements) {
				if (elements[s] > 0) {
					app.alert("You have selected: " + s
						 + ", which has a value of " + elements[s]);
				}
			}
	},
	loadDefaults: function (dialog) {
		dialog.load({
			subl: {
				"CourierStd": +1,
				"Helvetica": -2,
				"Times-Roman": -3,
			}
		})
	},
	// The dialog box description
	description: {
		name: "Font selection", // Title of the dialog box
		elements: // Child element array
		[{
				type: "view",
				align_children: "align_left",
				elements: // Child element array
				[{
						type: "cluster",
						name: "Select",
						elements: // Child Element Array
						[{
								type: "static_text",
								name: "Select a font",
								font: "default"
							}, {
								type: "popup", // Aufklappmenü
								item_id: "subl",
								width: 200,
							}, {
								type: "button",
								item_id: "butn",
								name: "Press Me"
							}
						]
					}, {
						type: "ok_cancel"
					}
				]
			}
		]
	}
}

if (app.execDialog(dialog3)=="ok") {

	var re = /.*\/|\.pdf$/ig;
	var FileNM = this.path.replace(re, "") + ".pdf";
	var Path = this.path;

	var f = this.addField("header1", "text", 0, [100, 250, 150, 200]);
	f.textColor = color.black;
	f.textSize = 12;
	f.textFont = selectedFont;
	f.strokeColor = color.transparent;
	f.fillColor = color.transparent;
	f.value = FileNM;
	f.alignment = "left";
}

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 ,
Dec 12, 2024 Dec 12, 2024

There are several ways to acquire a value from a custom dialog in the Acrobat JavaScript model. The simplest and most staight forward technique is to place the value in a member of the dialog object. 

 

var dialog3 = {
    strMyFont:"",
    GetListSel:function(oLst){for(var x in oLst){if(oLst[x]>0)return x;}return null;},
    initialize:function(dialog){
      this.loadDefaults(dialog);
    },
    commit:function(dialog){
       this.strMyFont = this.GetListSel(dialog.store("subl"));
    }
.... rest of dialog ...
}

if("ok" == app.execDialog(dialog3))
{
   ...
   f.textFont = dialog3.strMyFont;
   ...
}

 

In the modifided code above the font name selected from the popup is placed in the "strMyFont" member of the dialog3 object.  Note that this member is declared at the very top of the object definition.  The popup selection is acquired using a member function that is also defined at the top of the object definition. 

 

You'll find a tool that makes it much easier to build ialogs here:

https://www.pdfscripting.com/public/AcroDialogs.cfm

 

It massively simplifies the process by filling in a lot of these details for you and providing skeleton usage code. 

There are some instructions on coding dialogs here:

https://www.pdfscripting.com/members/AcrobatJavaScriptCustomDialogs.cfm

 

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

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
Explorer ,
Dec 13, 2024 Dec 13, 2024

Thank you for your support.

 

@try76
Yes, that's what I was looking for. But somehow I couldn't get it right. Thank you, now it works.


@Thom Parker
Your suggestion is interesting. I think it probably comes from your AcroDialogBuilder?

But at the moment I only get an error message:

InvalidSetError: Set not possible, invalid or unknown.
Field.textFont:77:Console undefined:Exec

I took a closer look at the lines, but I can't figure it out. I couldn't find anything about 'GetListSel:function' in the Acrobat JavaScript API Reference.
Unfortunately, the search window at the top left of the Online Acrobat JavaScript API Reference doesn't work: whatever you enter there, it doesn't find anything. This is very tedious. Is there any further literature on this?

 

yosimo

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 ,
Dec 14, 2024 Dec 14, 2024

It's not a part of the API. It's a custom function that's defined in your Dialog variable (which the Dialog Builder created, most likely). You need to debug it. For example, add a command to show the value it returns in an alert, to make sure it's what you're expecting.

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 ,
Dec 14, 2024 Dec 14, 2024

The definition for the "GetListSelection()"  function is provided in my post on the third line. 

Please take a closer look at the posted code.

 

The error reported is a direct result of the imcomplete implementation of the posted code. 

Acrobat is saying that the "dialog3.strMyFont" member property is undefined or null.

 

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

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
Explorer ,
Dec 15, 2024 Dec 15, 2024

Hi Thom,

thanks again for the advice. I have looked at it again and I think I have understood it.
But the value of 'strMyFont' always remains 'null'.

I have now changed the 'commit:function(dialog)' section a bit - so it seems to work.

 

var dialog3 = {
    strMyFont:"",
    GetListSel:function(oLst){for(var x in oLst){if(oLst[x]>0)return x;}return null;},
    initialize:function(dialog){
      this.loadDefaults(dialog);
    },
    commit:function(dialog){
 //      this.strMyFont = this.GetListSel(dialog.store("subl"));
        var storedData = dialog.store();
        var selectedValue = storedData.subl;
        this.strMyFont = this.GetListSel(selectedValue);
    },
        loadDefaults: function (dialog) {
            dialog.load({
                subl: {
                    "CourierStd": +1,
                    "Helvetica": -2,
                    "Times-Roman": -3,
                }
            })
        },
        // The dialog box description
        description: {
            name: "Font selection", // Title of the dialog box
            elements: // Child element array
            [{
                    type: "view",
                    align_children: "align_left",
                    elements: // Child element array
                    [{
                            type: "cluster",
                            name: "Select",
                            elements: // Child Element Array
                            [{
                                    type: "static_text",
                                    name: "Select a font",
                                    font: "default"
                                }, {
                                    type: "popup", // popup-menu
                                    item_id: "subl",
                                    width: 200,
                                }
                            ]
                        }, {
                            type: "ok_cancel"
                        }
                    ]
                }
            ]
        }
   }

if("ok" == app.execDialog(dialog3))
//	console.println("Speak to me: " + dialog3.strMyFont);
  app.alert("Speak to me: " + dialog3.strMyFont);

{    var re = /.*\/|\.pdf$/ig;
    var FileNM = this.path.replace(re, "") + ".pdf";
    var Path = this.path;

    var f = this.addField("header1", "text", 0, [100, 250, 400, 200]);
    f.textColor = color.black;
    f.textSize = 12;
    f.textFont = dialog3.strMyFont;
    f.strokeColor = color.transparent;
    f.fillColor = color.transparent;
    f.value = FileNM;
    f.alignment = "left";
}

 

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 ,
Dec 15, 2024 Dec 15, 2024

Works fine for me, but there's an error in your code that will cause the final block to execute no matter what button you click in the dialog. You must move the opening curly bracket directly after the if statement. Otherwise, only the first line following the if-condition (the "speak to me" alert, in this case) will be connected to it. All the rest of the code will execute no matter what.

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
Explorer ,
Dec 15, 2024 Dec 15, 2024
LATEST

Hi try76,
thanks for the hint. I have moved the opening curly bracket behind the 'if'-statement. Now there is no more error message in the js-console when the 'cancel' button is clicked.

 

Thank you for your great help! 👍

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