Need help with dynamic stamp.

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

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

unix2000
User
Posts: 1
Joined: Thu Nov 30, 2023 1:29 am

Need help with dynamic stamp.

Post by unix2000 »

Hi,

I'm new to this group and I want to incorporate as checklist shown in the attached image, I'm a newbie in Javascript and still learning.
I want to create a simple dynamic stamp with checkboxes, dropdown and radio buttons. when placing the dialog box will popup
to answer the checklist. Not sure where to start, I can create dynamic stamp with dates and dynamic text, but not with checkboxes, dropdown and radio buttons. Please help me to accomplish this or perhaps send me some link or sample to where I can start.|

I really appreciate your help everyone.
sample_dstamp.pdf
image.png
You do not have the required permissions to view the files attached to this post.
User avatar
Stefan - PDF-XChange
Site Admin
Posts: 19846
Joined: Mon Jan 12, 2009 8:07 am

Re: Need help with dynamic stamp.

Post by Stefan - PDF-XChange »

Hello unix2000,

You can have drop downs, check boxes and text fields in the pop-up dialogue that will show for your stamp, but once placed on the page - all the information would be static.

How to develop such a dialogue with stamp will require some reading, specially to understand the JS API reference Manual, published by Adobe: https://opensource.adobe.com/dc-acrobat ... croJS.html

After that, a good start would be some of Mathew's examples here in our forums. Here is a quick reference to one of the latest scripts he has done with a not too complex dialogue and still some checkboxes and fields for you to see how to use yourself:
viewtopic.php?t=42751

And here is a collection of his work:
viewtopic.php?t=42190

Kind regards,
Stefan
Mathew
User
Posts: 567
Joined: Thu Jun 19, 2014 7:30 pm

Re: Need help with dynamic stamp.

Post by Mathew »

unix2000 wrote: Fri Jun 07, 2024 11:47 pm Not sure where to start, I can create dynamic stamp with dates and dynamic text, but not with checkboxes, dropdown and radio buttons.
Hi Unix2000,

Well currently there's not an easy way to make pop-up dialogs for stamps - you'll need to get into javascript a bit. One gotcha I ran into in the past is that for some reason PDF-XChange seems to flatten all form fields when you make the stamp, so you need to edit the file in %APPDATA%\Tracker Software\PDFXEditor\3.0\Stamps directly by copying it out, editing it, then copying it back. I pasted some sample code below you may be able to change to your uses. You'll need to edit the stamp id (the long string starting with #), and the stampData to make it work for you.

To make forms work with the code below, I make each form Field Name on the stamp the same as the item_id on the dialog. A limitation here is that the item_id must be four or fewer digits (letters or numbers) long - it's case sensitive also. I pick one field on the form and set value calculation to "Custom Action", and paste the code into the javascript for the calculate action.

Code: Select all

// get stamp id by selecting stamp and using this.selectedAnnots[0].AP
// Note that field name, particularly for the field that calls this script, must be unique to this stamp
//        The item_id for the dialog must be four characters, but the field name can be longer, so the script
//        has an option to add a prefix to the field names
// edit stampData for each form.
//    event_hdlr the list of event handlers to add to the dialog. key is the item_id from the dialog for the onchange event
var stampData = { "#A5Z0yFTan5MXDB_cjD#RE1" : {
    // global variable name to store dialog results - must be unique for each stamp dialog box
    global: "_ShopStamp",
    // prefix for field name to add to the item_id from the dialog (so that field names can be unique to this stamp)
    prefix: "",
    // default values for the dialog fields
    global_defaults: {
        "date": util.printd("mmm d, yyyy", new Date()),//(new Date()).toLocaleDateString("en-US",{dateStyle:"short"})
        "rvBy": identity.name,
        "CoNm": "***ENTER COMPANY NAME HERE***", // edit to put company name here
    },
    // this is an object in the form { item_id: function, ...} - set it to {} if no onchange events needed in the dialog
    event_hdlr: { "URES": function(dialog) {
            var results = dialog.store();
            if (results["URES"] != "") {
                dialog.load( {"USR": true});
            }
        }},
    // edit this to make dialog
    dialog_description: {
        name: "Shop Drawing",
        elements:
        [
            {
                type: "view",
                align_children: "align_left",
                elements:
                [
                    {
                        type: "cluster",
                        name: "Shop Drawing Response",
                        item_id:"SD_R",
                        elements:
                        [
                            {
                                type: "check_box",
                                item_id: "NET",
                                name: "No Exceptions Taken",
                            },{
                                type: "check_box",
                                item_id: "MCN",
                                name: "Make Corrections Noted",
                            },{
                                type: "check_box",
                                item_id: "REJ",
                                name: "Rejected",
                            },{
                                type: "check_box",
                                item_id: "RR",
                                name: "Resubmit",
                            },{
                                type: "check_box",
                                item_id: "SSI",
                                name: "Submit Specified Item",
                            },{
                                type: "view",
                                align_children:"align_distribute",
                                elements:
                                [{
                                    type: "check_box",
                                    item_id: "USR",
                                    name: "",
                                    },{
                                    type: "edit_text",
                                    item_id: "URES",
                                    width: 200,
                                }],
                            },    
                        ],
                        },{
                        type: "cluster",
                        name: "Company Name",
                        elements:
                            [ {
                            type: "edit_text",
                            name: "Company Name",
                            item_id:"CoNm",
                            height: 36,
                            multiline: true,
                            alignment: "align_fill" }
                            ]
                        },{
                        type: "cluster",
                        align_children:"align_distribute",
                        elements:
                        [
                            { type: "static_text", 
                                name: "Review Date:",
                            },
                            { type: "edit_text",
                                item_id: "date",
                                width:150,
                            },
                        ],
                        },{
                        type: "cluster",
                        align_children:"align_distribute",
                        elements:
                        [
                            { type: "static_text", 
                                name: "Reviewed By:",
                            },
                            { type: "edit_text", 
                                item_id: "rvBy", 
                                width:150,
                            },
                        ]
                    },
                    {
                        type: "ok_cancel", cancel_name: "Leave Blank"
                    }
                ]
            }
        ]
        },
    },
};

// This part should not need editing for different stamps. Maybe it could be stored in a javascript file instead?
// run stamp dialog only on insert
if ( event.source.forReal ) {
    // stampName is the key
    let tStamp = stampData[event.source.stampName];
    // undefined if no key for the stamp
    // only runs for the stamp AP matching the key
    if ("undefined" === typeof tStamp) return; 
    
    // define the dialog
    let StampDialog = {
        results: {},
        initialize: function(dialog) {
            // check for saved defaults
            //console.println("Saved data: "+global[global_var]);
            if (typeof global[tStamp.global] != 'undefined') {
                this.results = JSON.parse(global[tStamp.global]);
            }else{
                this.results = tStamp.global_defaults;
            }
            dialog.load(this.results);
        },
        commit: function(dialog) {
            // Retrieve the values stored in the form and save to results:            
            this.results = dialog.store();
        },
        // Dialog object descriptor (root node)
        description: tStamp.dialog_description,
    };
    // add event handlers to the stamp dialog
    for (let i in tStamp.event_hdlr) {
        StampDialog[i] = tStamp.event_hdlr[i];
    }
    // run the dialog
    let sr = app.execDialog(StampDialog);
    
    // get the stamp document
    let md = ("undefined" == typeof event.target.doc ? event.target : event.target.doc); //event.target.doc on bluebeam
    
    // try to update fields
    if ("ok"==sr && md) {
        // get dialog results
        let results = StampDialog.results;
        // to hold fields that exist in the stamp
        let saveData = {};
        for (let e in results) {
            // get the field from the form
            var fd = md.getField( tStamp.prefix + e ); // add prefix to field
            // field must have the same name as the dialog box field with prefix
            if (fd) {
                saveData[e] = results[e]; // save it
                switch(fd.type) {
                    case "checkbox":
                    case "radiobutton":
                        fd.checkThisBox(0,results[e]);
                        break;
                    default:
                        fd.value = results[e];
                }
            }
        }
        // save for next time it's run
        global[tStamp.global] = JSON.stringify(saveData);
    } else {
        // try to clear all fields in the form
        if (md) {
            for (let i = 0; i < md.numFields; i++) {
                // get the field from the form
                var fd = md.getField(md.getNthFieldName(i));
                if (fd) {
                    switch(fd.type) {
                        case "checkbox":
                        case "radiobutton":
                            fd.checkThisBox(0,false);
                            break;
                        default:
                            fd.value = "";
                    }
                }
            }
        //    md.resetForm;
        }
    }
}
User avatar
Stefan - PDF-XChange
Site Admin
Posts: 19846
Joined: Mon Jan 12, 2009 8:07 am

Re: Need help with dynamic stamp.

Post by Stefan - PDF-XChange »

Hello Mathew,

Many thanks for the above!
And this KB article can show unix2000 how he can get the unique name of their own stamp:
https://www.pdf-xchange.com/knowledgeba ... p-Creation

Kind regards,
Stefan
Mathew
User
Posts: 567
Joined: Thu Jun 19, 2014 7:30 pm

Re: Need help with dynamic stamp.

Post by Mathew »

... and here's a sample using the above script ...
SampleShopstamp.pdf
You do not have the required permissions to view the files attached to this post.
User avatar
Paul - PDF-XChange
Site Admin
Posts: 7359
Joined: Wed Mar 25, 2009 10:37 pm

Need help with dynamic stamp.

Post by Paul - PDF-XChange »

:)
Best regards

Paul O'Rorke
PDF-XChange Support
http://www.pdf-xchange.com