Trying to build a dynamic stamp with a dropdown menu.

Forum for the PDF-XChange Editor - Free and Licensed Versions

Moderators: PDF-XChange Support, Daniel - PDF-XChange, Chris - PDF-XChange, Sean - PDF-XChange, Paul - PDF-XChange, Vasyl - PDF-XChange, Ivan - Tracker Software, Stefan - PDF-XChange

cadirkin
User
Posts: 5
Joined: Tue Oct 02, 2018 3:24 pm

Trying to build a dynamic stamp with a dropdown menu.

Post by cadirkin »

Hello all,

I am trying to build a dynamic stamp that has a dropdown menu in the dialog box that pops up when you place the stamp. The dropdown menu should have the following text: "AS BUILT", "CIRCUIT CHECK", "DESIGN CHECK"...ect. When the user makes their selection and clicks OK that value should be added to the stampText field on the stamp.

I was able to get the dropdown menu to appear in the dialog box, but no values are selectable. How can I get the dropdown menu to populate with the desired text? also how can I add the selection to the stamp? If I can get this to work I can replace 10+ stamps with this dynamic stamp.
image.png
Here is my javascript so far

Code: Select all

var dialog = {
    stampTextValue: "AS BUILT", // Default value
    stampTextOptions: ([
        "AS BUILT",
        "CIRCUIT CHECK",
        "DESIGN CHECK",
    ]),

//    initialize: function (dialog) {
//        var stampText = dialog.store()["stampText"];
//        console.println(this.stampTextValue);
//        dialog.load({"stampText": this.stampTextValue});
//    },

//    commit: function (dialog) { // called when OK pressed
//        var results = dialog.store();
//        this.stampTextValue = results["stampText"]; // Capture the selected value
//    },

    description: {
        name: "Dropdown Menu Test", // Dialog box title
        elements: [
            {
                type: "view",
                elements: [
                    {
                        name: "Select the thing: ",
                        type: "static_text",
                    },
                    {
                        item_id: "stmp",
                        type: "popup",
                        options: this.stampTextOptions, // Populate dropdown with options
                        value: this.stampTextValue, // Set default value here
                        width: 190,
                    },
                    {
                        type: "ok_cancel",
                        ok_name: "Go!",
                        cancel_name: "Cancel"
                    },
                ]
            },
        ]
    }
};

if(event.source.forReal && (event.source.stampName == "#q65hv997#7_NNA29eC9sC0")) {
    if ("ok" == app.execDialog(dialog)) {
        var stampTextField = this.getField("stampText");

        if (stampTextField !== null) {
            stampTextField.value = dialog.stampTextValue; // Update the field with the selected value
        } else {
            console.println("Error: stampText field not found.");
        }
    }
}
I am new to javascript so any help and guidance you can give will be greatly appreciated.
DropDownMenuTest.pdf
You do not have the required permissions to view the files attached to this post.
Mathew
User
Posts: 573
Joined: Thu Jun 19, 2014 7:30 pm

Re: Trying to build a dynamic stamp with a dropdown menu.

Post by Mathew »

I just posted a new tool that would make this easy for you.

This would be the code:

Code: Select all

xutil.stampDialog( "#q65hv997#7_NNA29eC9sC0",
{description: "Select the thing:"},
{field: "stampText", description: '', type: 'popup', value: ["AS BUILT", "CIRCUIT CHECK", "DESIGN CHECK"]} )
Or if you want to make it a one-liner:

Code: Select all

xutil.stampDialog( "#q65hv997#7_NNA29eC9sC0",
{field: "stampText", description: 'Select the thing:', labelWidth:100, type: 'popup', value: ["AS BUILT", "CIRCUIT CHECK", "DESIGN CHECK"]} )
User avatar
Stefan - PDF-XChange
Site Admin
Posts: 19868
Joined: Mon Jan 12, 2009 8:07 am

Re: Trying to build a dynamic stamp with a dropdown menu.

Post by Stefan - PDF-XChange »

Hello Mathew,

Many thanks for that!
I had some stamps I could share, but they have a lot in them, so were not right as a sample. I was planning on making such a sample myself but you "beat me to it" - which is greatly appreciated!

Kind regards,
Stefan
cadirkin
User
Posts: 5
Joined: Tue Oct 02, 2018 3:24 pm

Re: Trying to build a dynamic stamp with a dropdown menu.

Post by cadirkin »

I figured out how to make the values selectable with the help of https://www.pdf-xchange.com/fckfiles/Da ... e%20JS.txt

I made the rest of the fields editable. The user name field is auto populated from the users computer name. The mod kit field is empty by default. The check date field is auto populated with the date when the stamp is placed. The project number and part number are prepopulated but editable.
2025-04-10 07_59_01-22X34XX_ - PDF-XChange Editor.png
2025-04-10 07_58_33-22X34XX_ - PDF-XChange Editor.png

Code: Select all

var MenuDlg = {
    result: "cancel",
    DoDialog: function() {
        return app.execDialog(this);
    },

    DateValue: "",
    NameValue: ((!identity.name || identity.loginName != (event.source.source || this).Collab.user) ? (event.source.source || this).Collab.user : identity.loginName),
    RefValue: "",
    ProjNumValue: "61OP-00066.p.01.15.02",
    PartNumValue: "NYK: MT-XXX-X-XXX",

    strSelection: "AS BUILT",

    SetListSel: function(list, path) {
        if (path.length == 0) return;
        eval("list[\"" + ((path.join != "function") ? path : path.join("\"][\"")) + "\"] = 1");
    },

    GetListSel: function(oLstResult) {
        for (var item in oLstResult) {
            if ((typeof oLstResult[item] == "number") && (oLstResult[item] > 0))
                return item;
        }
        return "";
    },

    initialize: function(dialog) {
        var menu_list = {
            "AS BUILT": -1,
            "CIRCUIT CHECK": -1,
            "DESIGN CHECK": -1,
            "DETAIL CHECK": -1,
            "FIELD CONSTRUCTION": -1,
            "FIELD TEST": -1,
            "INFORMATION ONLY": -1,
            "MAINTAINERS COPY": -1,
            "MOD KIT": -1,
            "OFFICE COPY": -1,
            "SHOP PRODUCTION": -1,
            "TEST COPY": -1,
        };

        this.SetListSel(menu_list, this.strSelection);

        // Initialize user information
        var userName = dialog.store()["Name"];
        //    userName = console.println(this.NameValue);
        dialog.load({"Name": this.NameValue})

        var RefNum = dialog.store()["ref"];
        //    RefNum = console.println(this.RefValue);

        // Initialize Date
        var todayDate = dialog.store()["Date"];
        todayDate = util.printd("dd-mmm-yyyy", new Date());
        dialog.load({"date": todayDate})

        // Initialize project and part numbers
        var ProjNum = dialog.store()["Project"];
        //    ProjNum = console.println(this.ProjNumValue);
//        dialog.load({"Project": this.ProjNumValue})

        var PartNum = dialog.store()["Part"];
        //    PartNum = console.println(this.PartNumValue);
//        dialog.load({"Part": this.PartNumValue})

        // Load all values into dialog
        var dlgInit = {
            "fmt1": menu_list,
            "type": menu_list,
            "prev": this.strSelection,
            "Name": this.NameValue,
            "RefNum": this.RefValue,
            "Date": todayDate,
            "Project": this.ProjNumValue,
            "Part": this.PartNumValue,
        };

        dialog.load(dlgInit);
    },

    commit: function(dialog) {
        var oRslt = dialog.store();
        this.strSelection = this.GetListSel(oRslt["fmt1"]);

        var results = dialog.store()
        this.DateValue = results["Date"];
        this.RefValue = results["RefNum"];
        this.NameValue = results["Name"];
        this.ProjNumValue = results["Project"];
        this.PartNumValue = results["Part"];

    },

    "fmt1": function(dialog) {
        var rslts = dialog.store();
        var sel = this.GetListSel(rslts["fmt1"]);
        this.strSelection = sel;

        var l = {"prev": sel};
        dialog.load(l);
    },

    description: {
        name: "Drawing Check Stamps",
        elements: [{
            type: "view",
            elements: [

                {
                    type: "cluster",
                    name: "Selection",
                    elements: [{
                        type: "view",
                        elements: [{
                                item_id: "lbl1",
                                name: "Choose Stamp Option:",
                                type: "static_text"
                            },
                            {
                                item_id: "fmt1",
                                type: "popup",
                                width: 150
                            }
                        ]
                    }]
                },
                {
                    name: "User Name: ",
                    type: "static_text"
                },
                {
                    item_id: "Name",
                    type: "edit_text",
                    multiline: false,
                    width: 180
                },

                {
                    name: "Mod Kit Reference Number: ",
                    type: "static_text"
                },
                {
                    item_id: "RefNum",
                    type: "edit_text",
                    multiline: false,
                    width: 180
                },
                {
                    name: "Check Date: ",
                    type: "static_text"
                },
                {
                    item_id: "Date",
                    type: "edit_text",
                    multiline: false,
                    width: 180
                },

                {
                    name: "Project Number: ",
                    type: "static_text"
                },
                {
                    item_id: "Project",
                    type: "edit_text",
                    multiline: false,
                    width: 180
                },
                {
                    name: "Part Number: ",
                    type: "static_text"
                },
                {
                    item_id: "Part",
                    type: "edit_text",
                    multiline: false,
                    width: 180
                },

                {
                    type: "ok_cancel"
                }
            ]
        }]
    }
};


if (event.source.forReal && (event.source.stampName == "#q65hv997#7_NNA29eC9sC0")) {
    if (MenuDlg.DoDialog() == "ok") {
        // Set the type selection
        event.value = MenuDlg.strSelection;

        // Debug logging
        console.println("fmt1Value: " + MenuDlg.strSelection);
        console.println("NameValue: " + MenuDlg.NameValue);
        console.println("RefValue: " + MenuDlg.RefValue);
        console.println("DateValue: " + MenuDlg.DateValue);
        console.println("ProjNumValue: " + MenuDlg.ProjNumValue);
        console.println("PartNumValue: " + MenuDlg.PartNumValue);

        // Update all form fields
        this.getField("Name").value = MenuDlg.NameValue;
        this.getField("RefNum").value = MenuDlg.RefValue;
        this.getField("Date").value = MenuDlg.DateValue;
        this.getField("ProjNum").value = MenuDlg.ProjNumValue;
        this.getField("PartNum").value = MenuDlg.PartNumValue;

    } else {
        // Handle cancellation
        event.value = "Canceled!";
        this.getField("Name").value = "";
        this.getField("RefNum").value = "";
        this.getField("Date").value = "";
        this.getField("ProjNum").value = "";
        this.getField("PartNum").value = "";
    }

} else {
    event.value = MenuDlg.strSelection;
}
DropDownMenuTest.pdf
You do not have the required permissions to view the files attached to this post.
User avatar
Stefan - PDF-XChange
Site Admin
Posts: 19868
Joined: Mon Jan 12, 2009 8:07 am

Re: Trying to build a dynamic stamp with a dropdown menu.

Post by Stefan - PDF-XChange »

Hello cadirkin,

Many thanks for sharing your work with everyone!
Glad to see that our samples also helped!

Kind regards,
Stefan