this.getField array
Copy link to clipboard
Copied
Hi guys,
I am fairly new to Java so I apologise ahead of time if the problem is trivial. I am attempting to import an array from a drop-down list which I then wish to export as a .txt file. I can succesfully use this.getField to acquire the fields value but I'd like to be able to acquire all the fields that the drop-down list contains to an external text file. Is it possible to obtain all values of a drop-down box or even a list?
Thanks
Copy link to clipboard
Copied
I believe that you can do most, if not all of what you want, but there are some issues.
First, there is some terminology that is important for communicating to us what it is you want to do.
Acrobat uses JavaScript as it's internal programming language, same as HTML does. Java is a very different thing.
Dropdown lists do not contain other field, they contain a list of items. I believe it is this list of items you want to acquire?
You can read about the basics of list field programming here:
https://www.pdfscripting.com/public/List-Field-Usage-and-Handling.cfm
You'll see that list fields have a function named "getItemAt()". This is the function you'll need to use to acquire the list of items in the dropdown. Here's the Acrobat JavaScript reference entry for it.
That should get you started.
Next, a Script in Acrobat can't actually write to a text file on the user's hard drive. But there are some options depending the specifics of your process.
So, is this a one time thing? or does it need to be done repeatedly? Is it just you, or is this something other people will also need to do?
Let us know.
Use the Acrobat JavaScript Reference early and often
Copy link to clipboard
Copied
Hi Thom,
Thanks for the prompt response, I have currently had success in using the following code to be able to write to a text file, on the hard drive:
// set up output text
var TEMP_FIELD_NAME = "testHeader"
var textValue = this.getField("list");
// add a temporary text field
var f = this.addField(TEMP_FIELD_NAME, "text", 0, [30,30,100,20]);
f.value = textValue;
// export field name and value to defined file
this.exportAsText({aFields: TEMP_FIELD_NAME, cPath: "test-stock.txt"});
// remove text field
this.removeField(TEMP_FIELD_NAME);
// notify when done
app.alert("Done!");
I guess the purpose of the script ultimately would be to write a text file which contains a list of all options contained in a dropdown menu and be able to retrieve them and update the drop-down list. If possible I would like to use an array at getItemAt([0, n])
Copy link to clipboard
Copied
Use the Acrobat JavaScript Reference early and often
Copy link to clipboard
Copied
The list will only be accessible via Acrobat. The workflow would be as follows:
1. pdf file containing script is opened and retrieves the text, this will more than likely by assigned to a button that holds the function to retrieve text data.
2. If the drop down list does not contain what the user requires, a custom entry in the drop down list can be made (and comitted to the drop down list?)
3. The 'update list' action will be assigned to a button which can then update the entire list as appropriate appending new values as required ensuring that the drop down list is always updated
Copy link to clipboard
Copied
Ok, so anything that saves the data outside the form will work? Do other users need to access this data? Or is it just for a single person? Is this happening in a controlled environment? i.e. can you install scripts on the user's system?
All JavaScript functions that write to the user's file system require privilege. Which is why I asked how the script is being run now, because the script you posted will not work when run from inside a PDF.
Basically unless you have some control over the user's system you can't use a function that writes to the file system.
Read about privilege here:
https://www.pdfscripting.com/public/Trust-and-Privilege-in-Acrobat-Scripts.cfm?sd=40
Use the Acrobat JavaScript Reference early and often

