Skip to main content
deckarduk
Inspiring
April 17, 2018
Answered

Help with Acrobat JS dialog box please...

  • April 17, 2018
  • 3 replies
  • 2456 views

Hi there,

Please can someone point me in the right direction or to some examples that may help.

I'm trying to create a dialog that lists all the fields, on a given page, by field name.

That list is then presented to the user who then chooses/selects items in the field name list.

The script will then iterate through the selected list and then perform some style adjustments.

I've manage to create the dialog, containing a list of field names.

I'm stuck on how to pick up the highlighted items in the list in the dialog.

Please can someone help.

Thanks.

function getPageFields(doc, p) {

var fields = [];

for (var i=0; i<doc.numFields; i++) {

var f = doc.getField(doc.getNthFieldName(i));

if ((typeof f.page=="number" && f.page==p) || (typeof f.page!="number" && f.page.indexOf(p)>-1)) fields.push(f.name);

}

var fieldsNew = "";

for ( var i = 1; i < (fields.length); i++) {

    fieldsNew = fieldsNew + fields;

    if (i < (fields.length-1)) {

      fieldsNew = fieldsNew + "\n"; 

    }

}

return fieldsNew;

}

// Dialog Definition

var oDlg = {

    strName: "", initialize: function(dialog) {

        dialog.load({"usnm":this.strName});

    },

    commit: function(dialog) {

        var data = dialog.store();

        this.strName = data[ "usnm"];

    },

    description: {

        name: "PDF Combiner - Processing Parameters", elements: [ {

            type: "view", elements: [

                { name: "Instructions:\n\n1. Select the fields to process by highlighting them.\n2. Click 'OK' to process.", type: "static_text", height:65, },

                { item_id: "usnm", type: "edit_text", width:300, height: 300, multiline: "true", },

                { type: "ok_cancel", },

            ]

        },]

    }

};

// Dialog Activation

oDlg.strName = "" + getPageFields(this, 0);

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

    app.alert("Fields chosen\n" + oDlg.strName);

}

This topic has been closed for replies.
Correct answer Joel Geraci

You'll need to change your dialog so that you create two list boxes. Initially, the one will have a list of all the fields, the second will be empty. As users click on the list of fields, the field name will be added to the second list and removed from the field name list. Your second list is the list you perform the style changes to.

3 replies

deckarduk
deckardukAuthor
Inspiring
April 18, 2018

Hi Try67, Joel & Thom,

Thanks for the help with my query.

I've got a version working where the user enters a single field name. I'll have to go with that until I can get Joel's version working. I'm still newbie-ish to Acrobat Javascript. Please can anyone point me to some nice 'list-box' examples?

Thanks once again everyone.

try67
Adobe Expert
April 18, 2018

If you're having them select just a single field you can use a drop-down list, which is actually a text field with the PopupEdit and SpinEdit properties enabled. The list-boxes in dialogs are kind of misleading, as they don't allow multiple item selection, which is the whole point behind such a control, really.

deckarduk
deckardukAuthor
Inspiring
April 18, 2018

Thanks for the help try67.

The dropdown list sounds like a good idea.

Currently I prompt the user to enter the name of the field like this:

// Dialog Definition

global.ghjk = "";

var oDlg =

{

commit:function (dialog) { // called when OK pressed

var results = dialog.store();

// now do something with the data collected, for example,

console.println("The field entered was: " + results["usnm"] );

global.ghjk = results["usnm"];

},

   description:

   {

      name: "Field Zapper!",

      elements:

      [

         {

            type: "cluster",

            name: "Field Name:",

            elements:

            [

               {

                  name: "Enter the name of the\nfield you wish to edit\nin the box below:",

                  height: 45,

                  type: "static_text",

               },

               {

                  item_id: "usnm",

                  type: "edit_text",

                  char_width: 15

               },

                {

                  name: "",

                  height: 5,

                  type: "static_text",

               },

               {

                  type: "ok_cancel",

               },

            ]

         },

      ]

   }

};

// Dialog Activation

app.execDialog(oDlg);

One other thing I was unsure about is how to store the field name entered by the user.

I had trouble with this and in the end used  global.ghjk = results["usnm"];

I'm guessing that isn't the best way to do it but I couldn't get the value to exist outside of the dialog?

Please can you point me in the right direction.

Thanks.

Joel Geraci
Joel GeraciCorrect answer
Adobe Expert
April 17, 2018

You'll need to change your dialog so that you create two list boxes. Initially, the one will have a list of all the fields, the second will be empty. As users click on the list of fields, the field name will be added to the second list and removed from the field name list. Your second list is the list you perform the style changes to.

Thom Parker
Adobe Expert
April 17, 2018

I'll second Joel's suggestion. This is much much better than creating checkboxes for each field name. Not only is the scripting easier, but a list box can hold an arbitrary number of names, where as a dialog full of checkboxes only has so much space before it won't fit on the screen.

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
try67
Adobe Expert
April 17, 2018

That's not possible. The script has no way of knowing what text is highlighted in the text field.

What you can ask the users is to delete the fields they don't want to process from the list, or use some other kind of selection mechanism.

deckarduk
deckardukAuthor
Inspiring
April 17, 2018

Hi try67,

Thanks for the help.

I'd thought of the deletion method, just wondered if there was something a little safer in that a user could accidentally delete part of a field name. That could be solved though by validation etc.

Is it possible to generate check boxes depending on the number of fields on a page?

Can the dialog code be generated like that? Potentially the number of fields will vary from page to page.

Thank you once again for your help.

try67
Adobe Expert
April 17, 2018

It's possible to do that, yes, as the dialog object is just that, a literal object which can be edited like anything else, but it's not simple.

Also, you might get into issues with things like the field id's, the size of the dialog window, etc.

I've actually developed a method that allows multiple item selection which is beyond what can be done with a dialog, so if you're interested in something like that feel free to contact me privately (try6767 at gmail.com) and we could discuss the details.