Skip to main content
Sonny AR
Inspiring
March 3, 2023
Answered

Checkboxes Not showing checked or unchecked in Dynamic Stamp

  • March 3, 2023
  • 2 replies
  • 4727 views

Hello Members,

I have created a PDF DYNAMIC STAMP, consisting of 4 text fields and 4 checkboxes. Everything is working fine except for the checkboxes.

 

When I put data in the dialog box including the checkbox, The results of checkboxes (Checked/Unchecked) not showing up in Stamp.

 

//Checkbox Fields Name:

Check Box1

Check Box2

Check Box3

Check Box4

 

I have attached a ScreenShot of the stamp and JAVASCRIPT that I'm using now;

 

Thanks.

Software: Adobe Acrobat Pro DC.

 

 

var FormRouting = {
    return: 'cancel',
    DoDialog: function () {
        return app.execDialog(this);
    },

    bChk1: false,
    bChk2: false,
    bChk3: false,
    bChk4: false,

    str1: '',
    str2: '',
    str3: '',
    str4: '',

    initialize: function (dialog) {
        var dlgInit = {

            Chk1: this.bChk1,
            Chk2: this.bChk2,
            Chk3: this.bChk3,
            Chk4: this.bChk4,

            Str1: this.str1,
            Str2: this.str2,
            Str3: this.str3,
            Str4: this.str4,

        };

        dialog.load(dlgInit);
    },

    commit: function(dialog) {
        var oRslt = dialog.store();

        this.bChk1 = oRslt['chk1'];
        this.bChk2 = oRslt['chk2'];
        this.bChk3 = oRslt['chk3'];
        this.bChk4 = oRslt['chk4'];


        this.str1 = oRslt['str1'];
        this.str2 = oRslt['str2'];
        this.str3 = oRslt['str3'];
        this.str4 = oRslt['str4'];
    },

    description: {
        titlename: "Personal Data",align_children: "align_left", width: 500, height: 400, elements:
        [
            {
              type: "cluster", name: "Data Entry", align_children: "align_left", elements:
                [
                    // Text Field Code Below
                    {
                        type: "view", align_children: "align_row", elements:
                        [
                            {
                                type: "static_text", name: "Field 1:"
                            },
                            {
                                item_id: "str1", type: "edit_text", alignment: "align_fill", width: 400, height: 20
                            }
                        ]
                    },

                    {
                        type: "view", align_children: "align_row", elements:
                        [
                            {
                                type: "static_text", name: "Field 2:"
                            },
                            {
                                item_id: "str2", type: "edit_text", alignment: "align_fill", width: 400, height: 20
                            }
                        ]
                    },

                    {
                        type: "view", align_children: "align_row", elements:
                        [
                            {
                                type: "static_text", name: "Field 3:"
                            },
                            {
                                item_id: "str3", type: "edit_text", alignment: "align_fill", width: 400, height: 20
                            }
                        ]
                    },

                    // Check Box Field Code below
                    {
                        type: "view", align_children: "align_row", elements:
                        [
                            {
                                item_id: "Chk1", type: "check_box", alignment: "align_fill", width: 5, height: 20
                            },
                            {
                                type: "static_text", name: "CheckBox 1"
                            }
                        ]
                    },
                    
                    {
                        type: "view", align_children: "align_row", elements:
                        [
                            {
                                item_id: "Chk2", type: "check_box", alignment: "align_fill", width: 5, height: 20
                            },
                            {
                                type: "static_text", name: "CheckBox 2"
                            },
                        ]
                    },

                    {
                        type: "view", align_children: "align_row", elements:
                        [
                            {
                                item_id: "Chk3", type: "check_box", alignment: "align_fill", width: 5, height: 20
                            },
                            {
                                type: "static_text", name: "CheckBox 3"
                            },
                        ]
                    },

                    {
                        type: "view", align_children: "align_row", elements:
                        [
                            {
                                item_id: "Chk4", type: "check_box", alignment: "align_fill", width: 5, height: 20
                            },
                            {
                                type: "static_text", name: "CheckBox 4"
                            },
                        ]
                    },

                    // By Field
                    {
                        type: "view", align_children: "align_row", elements:
                        [
                            {
                                type: "static_text", name: "By: "
                            },
                            {
                                item_id: "str4", type: "edit_text", alignment: "align_fill", width: 400, height: 20
                            }
                        ]
                    },

                ]
            },
            {
                alignment: "align_right",
                type: "ok_cancel",
                ok_name: "ok",
                cancel_name: "Cancel"
            }
        ],
    },
};

if (event.source.forReal && event.source.stampName == "Sohail") {
    if ('ok' == FormRouting.DoDialog()) {
        // Checkboxes Fields
        if (FormRouting.bChk1) {
            this.getField('Check Box1').checkThisBox(0, true);
        }

        if (FormRouting.bChk2) {
            this.getField('Check Box2').checkThisBox(0, true);
        }

        if (FormRouting.bChk3) {
            this.getField('Check Box3').checkThisBox(0, true);
        }
        
        if (FormRouting.bChk4) {
            this.getField('Check Box4').checkThisBox(0, true);
        }

        // string fields

        this.getField('Field 1').value = FormRouting.str1;
        this.getField('Field 2').value = FormRouting.str2;
        this.getField('Field 3').value = FormRouting.str3;
        this.getField('By').value = FormRouting.str4;
    }
}

 

Correct answer Thom Parker

There is no need for the "if" statement used to set the checkbox value. Write it like this:

        this.getField('Check Box1').checkThisBox(0, FormRouting.bChk1);
        this.getField('Check Box2').checkThisBox(0, FormRouting.bChk2);
        this.getField('Check Box3').checkThisBox(0, FormRouting.bChk3);
        this.getField('Check Box4').checkThisBox(0, FormRouting.bChk4);

 

And after looking through the code again I found your error.  In the dialog  "commit" function the item_id for the checkbox is spelled incorrectly. 

 

Here is the corrected code:

        this.bChk1 = oRslt['Chk1'];
        this.bChk2 = oRslt['Chk2'];
        this.bChk3 = oRslt['Chk3'];
        this.bChk4 = oRslt['Chk4'];

 

Note also that the "str" item_id's are also spelled wrong in the "initialize" function. There are however, not being used at this time, so it doesn't have any effect on the stamp behavior. 

 

 

2 replies

Thom Parker
Community Expert
Community Expert
March 3, 2023

Also, the stamp name needs to be prefixed with "#" so the full stamp name is "#Sohail"

 

The "#" tells Acrobat to run the script everytime the stamp is applied.  

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Sonny AR
Sonny ARAuthor
Inspiring
March 3, 2023

Hello Thom, thanks for your help.


I have tried both variations, but the issue remains the same.


-Is anything reported in the console window?
No, I'm not seeing any errors in the console or Script window. 😞

 

And yes, I have added '#' to the stamp name. Thanks for letting me know. 🙂

Sonny AR
Sonny ARAuthor
Inspiring
March 4, 2023

There is no need for the "if" statement used to set the checkbox value. Write it like this:

        this.getField('Check Box1').checkThisBox(0, FormRouting.bChk1);
        this.getField('Check Box2').checkThisBox(0, FormRouting.bChk2);
        this.getField('Check Box3').checkThisBox(0, FormRouting.bChk3);
        this.getField('Check Box4').checkThisBox(0, FormRouting.bChk4);

 

And after looking through the code again I found your error.  In the dialog  "commit" function the item_id for the checkbox is spelled incorrectly. 

 

Here is the corrected code:

        this.bChk1 = oRslt['Chk1'];
        this.bChk2 = oRslt['Chk2'];
        this.bChk3 = oRslt['Chk3'];
        this.bChk4 = oRslt['Chk4'];

 

Note also that the "str" item_id's are also spelled wrong in the "initialize" function. There are however, not being used at this time, so it doesn't have any effect on the stamp behavior. 

 

 


Thank you so much, Thom, for helping me. I really appreciate the way you found the errors in my script. The corrections you sent me really work. Now my checkboxes are showing in the stamp the way I want them to.

Thanks

Thom Parker
Community Expert
Community Expert
March 3, 2023

 

There's nothing obviously wrong with the code. However, the checkboxes are only getting set in one direction. This may or may not be important. 

Here are two better variations:

 

1. 

    this.getField('Check Box1').checkThisBox(0, FormRouting.bChk1);

 

2. 

this.getField('Check Box1').value = FormRouting.bChk1?"Yes":"Off";

 

What kind of debug have you done? 

  • Is anything reported in the console window?
  • Have you used console.println() statements to report the values returned from the dialog?

 

 

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