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

Dynamic Stamp with auto Popup for data entry

New Here ,
Apr 09, 2025 Apr 09, 2025

Dear Adobe Community,

I’m trying to create a custom stamp that, when added to a document, automatically shows a popup dialog. This dialog should first ask the user (a) how many rows need to be filled, and then (b) display input fields for each row where the user can enter data separated by semicolons. These values are then parsed and placed into individual fields.

This works perfectly when I use the JavaScript in a regular PDF form. However, as soon as I turn it into a dynamic stamp, the input dialog no longer appears.

 

var maxRows = 5;

var zeilenStr = app.response({
    cQuestion: "Wie viele Zeilen möchten Sie befüllen? (1–5)",
    cTitle: "Zeilenanzahl eingeben",
    cDefault: "3"
});

var anzahl = parseInt(zeilenStr);
if (isNaN(anzahl) || anzahl < 1 || anzahl > maxRows) {
    app.alert("Ungültige Anzahl. Bitte zwischen 1 und 5 angeben.");
} else {
    for (var i = 1; i <= anzahl; i++) {
        var eingabe = app.response({
            cQuestion: "Werte für Zeile " + i + " eingeben (mit Semikolon getrennt):\nMontant;Compte;Analytique;Type activite",
            cTitle: "Zeile " + i,
            cDefault: ""
        });

        if (eingabe) {
            var werte = eingabe.split(";");

            if (werte.length > 0) this.getField("MontantRow" + i).value = werte[0];
            if (werte.length > 1) this.getField("CompteRow" + i).value = werte[1];
            if (werte.length > 2) this.getField("AnalytiqueRow" + i).value = werte[2];
            if (werte.length > 3) this.getField("Type activiteRow" + i).value = werte[3];
        }
    }
}

 

Thank you very much for your help!
Regards
Kremer

TOPICS
JavaScript , PDF
522
Translate
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 09, 2025 Apr 09, 2025

Please search the forum. This issue was discussed here plenty of times in the past, including full tutorials, code examples and even a book written on it!

Translate
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
New Here ,
Apr 09, 2025 Apr 09, 2025

Hello,
I've already read through several posts and tried many different approaches, but unfortunately, the popup never appears when using the stamp.

Attached is a sample PDF with the stamp. All form fields are properly defined, and there's a hidden field containing the JavaScript code. The stamp name is also set correctly.

Thank you in advance!

Translate
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 09, 2025 Apr 09, 2025

The popup appears when I installed your stamp. So it works, but the dialog has some problems. 

First, it needs a an "Ok" button. That's the only way to exit the dialog and run the commit code. 

Next, the keyword "this", in any function(method) in any object, is a pointer to the object itself. So, this.getField doesn't work inside the dialog object. The code needs to either include pointer to the current doc as a member of the dialog object, or placing data into fields needs to be done outside the dialog. 

And finally, the "item_id" of a field in the dialog object can only be 4 characters. 

 

I would suggest getting the dialog code to work first, before including it into the stamp. 

 

Here's a tool for creating dialogs. 

https://www.pdfscripting.com/public/ACRODIALOGS-OVERVIEW.cfm

 

 

 

 

 

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

Translate
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
New Here ,
Apr 09, 2025 Apr 09, 2025

Hello Thom,

Thank you for your response!


I wasn’t aware of the character limitation for "item_id". I’ll definitely look into getting the dialog code to work. However, I still have some doubts about whether it will function correctly as a stamp, since I'm not seeing the popup appear when I apply it—unlike you with my pdf 🙂


In order to add the stamp : Stamps -> Custom Stamps -> Create. Then select the pdf I attached before and name it #SNH1 (the name of the template)

kremerSKB_0-1744265217726.png

But when I use the stamp, I only see the table—no popup dialog appears.

Thank you in advance

Kremer

Translate
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 10, 2025 Apr 10, 2025

The Stamp Name in the dialog is not the same as the internal stampName property that you need to use in your code. Again, this is explained in detail in those tutorials and examples I've mentioned.

Translate
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 10, 2025 Apr 10, 2025

Dynamic stamps cannot be added to Acrobat using the Acrobat stamp tools. These tools remove all scripts, and rename the stamp. 

The stamp file has to be manually placed in one of the Acrobat stamp folders. 

Read these articles!!

https://www.pdfscripting.com/public/All_About_PDF_Stamps.cfm

https://acrobatusers.com/tutorials/dynamic_stamp_secrets/

 

 

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

Translate
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
New Here ,
Apr 14, 2025 Apr 14, 2025

Hi,
yes got it working 🙂
But now I have another issue.:

I want to have some predefined values in a dropdown. But I'm only getting empty dropdowns.
Andy idea?

 

if (event.source.forReal && (event.source.stampName == "#dSNH")) {
    var doc = this;

    var dialog = {
        initialize: function(dialog) {
            dialog.load(this.data);
        },
        commit: function(dialog) {
            this.data = dialog.store();

            for (var i = 1; i <= 5; i++) {
                doc.getField("MontantRow" + i).value = this.data["Mon" + i];
                doc.getField("CompteRow" + i).value = this.data["Com" + i];
                doc.getField("AnalytiqueRow" + i).value = this.data["Ana" + i];
                doc.getField("TypeRow" + i).value = this.data["Typ" + i];
            }
        },
        data: (function () {
            var d = {};
            for (var i = 1; i <= 5; i++) {
                d["Mon" + i] = "";
                d["Com" + i] = "";
                d["Ana" + i] = "";
                d["Typ" + i] = "";
            }
            return d;
        })(),
        description: {
            name: "Dropdown",
            elements: [
                {
                    type: "view",
                    align_children: "align_left",
                    elements: (function () {
                        var rows = [];
                        rows.push({ type: "static_text", name: "Bitte die Daten für die 5 Zeilen eingeben:" });

                        for (var i = 1; i <= 5; i++) {
                            rows.push({
                                type: "view",
                                orientation: "column",
                                elements: [
                                    { type: "static_text", name: "Zeile " + i + ":" },
                                    {
                                        type: "view",
                                        orientation: "row",
                                        align_children: "align_top",
                                        elements: [
                                            {
                                                item_id: "Mon" + i,
                                                type: "popup",
                                                name: "Montant",
                                                char_width: 10,
                                                items: [ { name: "100€" }, { name: "200€" }, { name: "300€" } ]
                                            },
                                            { item_id: "Com" + i, type: "edit_text", name: "Compte", char_width: 8 },
                                            { item_id: "Ana" + i, type: "edit_text", name: "Analytique", char_width: 8 },
                                            { item_id: "Typ" + i, type: "edit_text", name: "Type", char_width: 10 }
                                        ]
                                    }
                                ]
                            });
                        }
                        return rows;
                    })()
                },
                {
                    type: "ok_cancel",
                    ok_name: "OK",
                    cancel_name: "Abbrechen"
                }
            ]
        }
    };

    if (app.execDialog(dialog) === "ok") {
        // commit() wird automatisch aufgerufen
    }
}

 

Translate
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 14, 2025 Apr 14, 2025

This line of the code does nothing, since that variable is not defined at that point:

dialog.load(this.data);

 

If you want to load items into the pop-ups you need to do it at the initialize function, not in the description, and using a literal object, not an array. Like this:

 

dialog.load({
"Mon1" : {"100€": 1, "200€": -2, "300€": -3}
});

 

Edit: Small mistake fixed in the code

Translate
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
New Here ,
Apr 14, 2025 Apr 14, 2025

try67 thank you very much
that worked. But still getting [object Aggregate] when validating the input windows

Translate
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 14, 2025 Apr 14, 2025

Dropdowns are initialized with an Object, as shown by Try67 above:

dialog.load({
"Mon1" : {"100€": 1, "200€": -2, "300€": -3}
});

 

This same object is returned with the "dialog.store()" function.  So to get a selected value the script must loop over all object members and find the one with a positive value. 

 

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

Translate
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
New Here ,
Apr 16, 2025 Apr 16, 2025

Hello,
thank you for your information. This now is working fine.
Ass a nice to have, I wanted to create for each line a dropdown in the secon column that loads different content reagrding of the selected value in the first dropdown.

Is this even possible in Acrobat Stamps?

 

Thank you in advance

Translate
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 16, 2025 Apr 16, 2025

Yes.  Get the selected dropdown value from dialog.store() and write an if statement that loads another dropdown based on the result of the first dropdown.

Translate
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 16, 2025 Apr 16, 2025

Yes, it is possible. Use the selection event for the  first dropdown to acquire the selection value and then load a new list into the second dropdown. 

The selection event for the first dropdown is a function in the dialog object just like the "commit" member. Only it is named for the 4 character item_id of the dropdown. 

 

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

Translate
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
New Here ,
Apr 17, 2025 Apr 17, 2025

Hello

 

thank you very much @Thom Parker for the tip. this worked.
I now just have to figure out why the OK an Cancel label are disappearing after chosing the first dropdown 🙂

 

 description: {
            name: "Stempel mit Dropdown",
            elements: (function () {
                var rows = [];

                for (var i = 1; i <= 5; i++) {
                    rows.push({
                        type: "view",
                        orientation: "row",
                        align_children: "align_top",
                        elements: [
                            {
                                type: "view",
                                orientation: "column",
                                align_children: "align_left",
                                elements: [
                                    (i === 1 ? { type: "static_text", name: "Type" } : {}),
                                    {
                                        item_id: "Typ" + i,
                                        type: "popup",
                                        char_width: 7,
                                        items: [
                                            { name: "-" },
                                            { name: "Loc" },
                                            { name: "Constr." },
                                            { name: "Infra." },
                                            { name: "Refact." }
                                        ]
                                    }
                                ]
                            },
                            {
                                type: "view",
                                orientation: "column",
                                align_children: "align_left",
                                elements: [
                                    (i === 1 ? { type: "static_text", name: "Compte" } : {}),
                                    {
                                        item_id: "Com" + i,
                                        type: "popup",
                                        char_width: 9,
                                        items: [
                                            { name: "-" }
                                        ]
                                    }
                                ]
                            },
                            {
                                type: "view",
                                orientation: "column",
                                align_children: "align_left",
                                elements: [
                                    (i === 1 ? { type: "static_text", name: "Analytique" } : {}),
                                    {
                                        item_id: "Ana" + i,
                                        type: "edit_text",
                                        char_width: 25
                                    }
                                ]
                            },
                            {
                                type: "view",
                                orientation: "column",
                                align_children: "align_left",
                                elements: [
                                    (i === 1 ? { type: "static_text", name: "Montant" } : {}),
                                    {
                                        item_id: "Mon" + i,
                                        type: "edit_text",
                                        char_width: 9
                                    }
                                ]
                            }
                        ]
                    });
                }

                rows.push({
                    type: "ok_cancel",
                    ok_name: "OK",
                    cancel_name: "Abbrechen"
                });

                return rows;
            })()
        }
    };

    function updateCompte(dialog, index) {
        var store = dialog.store();
        var selectedType = getSelectedValue(store["Typ" + index]);

        var comptes = compteOptions[selectedType] || ["-"];
        var compteValues = {};
        comptes.forEach(function(val, idx) {
            compteValues[val] = (idx === 0) ? 1 : 0;
        });

        store["Com" + index] = compteValues;
        dialog.load(store);
    }
Translate
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, 2025 Apr 17, 2025
LATEST

Don't load the entire "store" object. Instead load only the specific field of interest. 

 

var oInit = {};

oInit["Com"+index] = compteValues;

dialog.load(oInit);

 

 

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

Translate
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 09, 2025 Apr 09, 2025
Translate
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