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

Help with Acrobat JS dialog box please...

Contributor ,
Apr 17, 2018 Apr 17, 2018

Copy link to clipboard

Copied

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);

}

TOPICS
Acrobat SDK and JavaScript

Views

1.6K

Translate

Translate

Report

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 , Apr 17, 2018 Apr 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.

Votes

Translate

Translate
Community Expert ,
Apr 17, 2018 Apr 17, 2018

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

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 ,
Apr 17, 2018 Apr 17, 2018

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

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 ,
Apr 17, 2018 Apr 17, 2018

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

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 ,
Apr 17, 2018 Apr 17, 2018

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

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 ,
Apr 17, 2018 Apr 17, 2018

Copy link to clipboard

Copied

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 PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

Translate

Translate

Report

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 ,
Apr 18, 2018 Apr 18, 2018

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

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 ,
Apr 18, 2018 Apr 18, 2018

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

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 ,
Apr 18, 2018 Apr 18, 2018

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

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 ,
Apr 18, 2018 Apr 18, 2018

Copy link to clipboard

Copied

You can drop the global definition. It should work...

Votes

Translate

Translate

Report

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 ,
Apr 18, 2018 Apr 18, 2018

Copy link to clipboard

Copied

Thanks for the help.

Not sure where I'm going wrong but I get ghjk not defined and when trying something slightly different, f is null ?

Votes

Translate

Translate

Report

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 ,
Apr 18, 2018 Apr 18, 2018

Copy link to clipboard

Copied

Post your full code, please.

Votes

Translate

Translate

Report

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 ,
Apr 18, 2018 Apr 18, 2018

Copy link to clipboard

Copied

This is the full code so far.

// 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);

var f = this.getField(global.ghjk);

f.strokeColor = color.transparent;

f.fillColor = color.transparent;

Thanks.

Votes

Translate

Translate

Report

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 ,
Apr 18, 2018 Apr 18, 2018

Copy link to clipboard

Copied

That should work, but as I said, you can drop the global part and just use a regular variable.

If you're getting an error message saying that f is null it means you're specifying an incorrect field name.

Votes

Translate

Translate

Report

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 ,
Apr 18, 2018 Apr 18, 2018

Copy link to clipboard

Copied

Thanks for the reply.

Yeah, the code above does work. It's when I try and take out the 'global' I start getting the errors?

Votes

Translate

Translate

Report

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 ,
Apr 18, 2018 Apr 18, 2018

Copy link to clipboard

Copied

What errors? Post the code without it, then...

Votes

Translate

Translate

Report

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 ,
Apr 18, 2018 Apr 18, 2018

Copy link to clipboard

Copied

Just tried again and it works!

Think it's because I added in 'var =' in place of 'global'.

Am I correct in thinking I've then declared 2 variables, one outside the dialog and one in it.

Think that may explain the error.

Thanks again for your help.

Votes

Translate

Translate

Report

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 ,
Apr 18, 2018 Apr 18, 2018

Copy link to clipboard

Copied

Yes, that's probably what happened.

Votes

Translate

Translate

Report

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 ,
Apr 18, 2018 Apr 18, 2018

Copy link to clipboard

Copied

A much better way to create dialogs is to use a dialog editor:

ACRODIALOGS OVERVIEW

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

Votes

Translate

Translate

Report

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 ,
Apr 19, 2018 Apr 19, 2018

Copy link to clipboard

Copied

LATEST

Hi Thom,

Thanks for your post.

The AcroDialogs looks great and would certainly reduce the time to create useful dialogs.

I'll have a better look at the literature on the overview page.

Thanks once again for the help Thom.

Votes

Translate

Translate

Report

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