Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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!
Copy link to clipboard
Copied
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!
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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)
But when I use the stamp, I only see the table—no popup dialog appears.
Thank you in advance
Kremer
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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/
Copy link to clipboard
Copied
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
}
}
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
try67 thank you very much
that worked. But still getting [object Aggregate] when validating the input windows
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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);
}
Copy link to clipboard
Copied
Don't load the entire "store" object. Instead load only the specific field of interest.
var oInit = {};
oInit["Com"+index] = compteValues;
dialog.load(oInit);
Copy link to clipboard
Copied
As @Thom Parker said, your dialog script has some issues with item_id, this, and a missing OK button. Here's some articles that might help:
https://pdfautomationstation.substack.com/p/popups-and-browse-windows-part-v
https://pdfautomationstation.substack.com/p/popups-and-browse-windows-part-vi
https://pdfautomationstation.substack.com/p/popups-and-browse-windows-part-vii