Copy link to clipboard
Copied
Hi All,
We have a PDF form which we print and manually and fill in but I am looking at making it interactive.
I would like to have a drop-down to select a folder from our network in one section of the form.
I know this cannot be done in Acrobat alone with JavaScript, but after a bit of searching online it can be achieved by creating an external file.
So far I have
3. Created a dropdown on the PDF but this is where it's falling over.
In the properties of the drop-down down I have selected 'On Mouse down' and set it to run a Java Script.
In the java script i have entered trustedImport();
After all this when i run it and try the Drop-down, nothing shows in the drop-down
Anyone done this or something similar? Javascript in not my thing so i may be doing something astoundingly obvious and it's not quite a straight forward as i thought.
Copy link to clipboard
Copied
What script do you use ?
Copy link to clipboard
Copied
This is the script i'm using
trustedImport = app.trustedFunction(function() {
app.beginPriv();
var path = "/Nas02/Livedata/folderlist.txt";
var stream = util.readFileIntoStream(path);
var content = util.stringFromStream(stream);
var folders = content.split("\n");
var f = this.getField("folderDropdown");
if (f) {
f.setItems(folders);
}
app.endPriv();
});
trustedImport(); // call the function
Copy link to clipboard
Copied
You can call the function at document open.
Copy link to clipboard
Copied
There are a few problems with your script. The main one is that you shouldn't use a variable name that is already defined. When you call path you are calling the path to the current PDF document, which will read a stream from the PDF itself. Change the two instances of path to pth. Next, when you split a text column with \n or \r it splits the values but also leaves the line breaks in the result. That will give you line breaks in the dropdown values. A workaround is to create a new array, loop through the existing one, and push the values (while removing the line breaks) into the new array and use the new array to set the dropdown items. Next, you should use event.target instead of this.getField("folderDrodown") since the script is in the "folderDropdown" field. However, it would be easier to call the function in a document level script so the dropdown items load as soon as the document opens, as @Bernd Alheit pointed out. It's good practice to define the document with a variable in the function. Here's the script with the corrections:
trustedImport = app.trustedFunction(function(oDoc) {
app.beginPriv();
var pth = "/Nas02/Livedata/folderlist.txt";
var stream = util.readFileIntoStream(pth);
var content = util.stringFromStream(stream);
var folders = content.split("\n");
var aray=[];
for(i in folders)
{aray.push(folders[i].replace("\r",""))}
var f = oDoc.getField("folderDropdown");
if (f) {
f.setItems(aray);
}
app.endPriv();
});
trustedImport(this);//
Note, the function is called from a document level script. If it's called from the field, change oDoc.getField("folderDropown") to event.target.
Copy link to clipboard
Copied
This is how I would approach it:
1) Create a hidden text field in the form ("Row"), with the following validation script:
this.getField("Items").value+=event.value+"\r";
2) Create a second hidden text field that is multiline and allows scrolling long text ("Items").
3) The mouse down action of the dropdown calls a trusted function from the JavaScript folder that clears "Items" and imports all rows of the text file to "Row".
4) The mouse down action of the dropdown creates an array from the rows in "Items" and sets the items of the dropdown with this array:
var aray=this.getField("Items").value.split("\r"); aray.pop();
event.target.setItems(aray);
The following article, while not exactly the same (the text file list is PDF files, not folders), outlines in detail the steps above.
https://pdfautomationstation.substack.com/p/how-to-create-your-own-action-batch
Copy link to clipboard
Copied
I will have a read, Thanks for the reply.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now