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

Adobe Acrobat Pro Javascript Import Dropdown list items from text file

New Here ,
Nov 12, 2024 Nov 12, 2024

Copy link to clipboard

Copied

Good afternoon,

I've been struggling with this all day and can't seem to figure it out. I would like to have a text file that can be relatively easily edited with a list of names to fill into a drop down list as names get removed and added somewhat frequently and updating each individual dropdown is a pain. I have gotten as far as getting the text files attached as annotation file attachments but it is not saving the file name or file contents, it keeps outputting "undefined". Ive tried a few different ways of trying to get the info through arrays and for loops but it still always gets stopped by the undefined error so it kicks out "no file attachment found".

// Function to load text file data and populate the dropdown list
function loadDropdownData() {
    // Get the file attachment (assuming the file is named 'Team Leader Names.txt')
    var fileName = "Team Leader Names.txt";
    var annotations = this.getAnnots({nSubtype: "FileAttachment"});
    
    // Convert the annotations (array-like object) to a true array using slice()
    annotations = Array.prototype.slice.call(annotations);  // Workaround for Array.from()

    // Variable to hold the found file annotation
    var foundFile = null;

    // Loop through annotations and find the file with the matching name
    annotations.forEach(function(annot) {
        if (annot.fileName === fileName) {
            foundFile = annot;  // Store the matching annotation
        }
    });

    if (foundFile) {
        // Access file contents (make sure it's embedded and not external)
        var fileData = foundFile.fileContents; // Get the file contents from the annotation

        if (fileData) {
            // Split the file data into an array of lines
            var options = fileData.split("\n").map(function(line) {
                return line.trim(); // Remove extra whitespace
            });

            // Get the dropdown field (replace 'dropdownFieldName' with your actual field name)
            var dropdown = this.getField("dropdownFieldName");

            // Clear existing options in the dropdown
            dropdown.clearItems();

            // Add new options from the file data
            options.forEach(function(option) {
                dropdown.addItem(option);
            });

            console.println("Dropdown populated with " + options.length + " options.");
        } else {
            console.println("File contents not available.");
        }
    } else {
        console.println("File attachment not found: " + fileName);
    }
}

// Call the function to load the dropdown data
loadDropdownData();

 

Hyrum35484607an2r_0-1731443639993.png

 

here is when i run a debugger to just output the file attachment info. So it recognizes it as a file attachment but its not getting any of the other info like name or contents, even though the annotation attchment shows in the attachment sidebar and when i open it, it opens to a poputated text file.

Hyrum35484607an2r_1-1731443875167.png

 

any help would be appreciated! Maybe I'm over complicating things but im trying to avoid hard coding it (i did get that to work by just manually creating the arrays but id rather not to that) as multiple people access this PDF and if I'm not around to update the list then the others will be lost so if I could just have them put the names into the text file then that would be a lot easier than telling them to mess with the code.

 

Also I am pretty rusty on javascript, i took it back in HS and a little in college so I did use GPT for some guidance so that may be why some of it is not working. ¯\_(ツ)_/¯

 

Thanks for your time!

Hyrum J

TOPICS
General troubleshooting , JavaScript , PDF , PDF forms

Views

201

Translate

Translate

Report

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 ,
Nov 12, 2024 Nov 12, 2024

Copy link to clipboard

Copied

There are easier ways to update a list than this.  Have you thought about a hidden text field with a list of names that you can easily split into an array to pass to the setItems method of the dropdown?  I created a (paid-for) tool that allows you to easily load a list of items into a dropdown by copying and pasting if you're interested.

Votes

Translate

Translate

Report

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 ,
Nov 13, 2024 Nov 13, 2024

Copy link to clipboard

Copied

I hadn't thought of that but I don't know if I could get that to work as nicely as I would like, additionally like i said to Try67, this is only for one document that doesnt use that much data so I don't know If i can justify spending the money on a tool, and it's a work computer too so I don't know if i would be allowed to load a tool/plugin

Votes

Translate

Translate

Report

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 ,
Nov 13, 2024 Nov 13, 2024

Copy link to clipboard

Copied

oh i forgot to ask, do you know if putting it in a comment would work? or would i run into similar issues as i did with this method of trying to turn an array-like object into a true array?

Votes

Translate

Translate

Report

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 ,
Nov 13, 2024 Nov 13, 2024

Copy link to clipboard

Copied

I have a few questions to help you with an easy solution:

1)  Who will be updating the dropdowns, a form administrator, or the end users?

2)  Do all the updaters have access to Acrobat?

Votes

Translate

Translate

Report

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 ,
Nov 13, 2024 Nov 13, 2024

Copy link to clipboard

Copied

1. there are a few people that work in my section that will be updating it but this form will end up passing hands as people move in and out of the section every year or so.

2. yes everyone has access to acrobat

So I ended up hardcoding it but making an excel that makes copying the names into the code easy and I made a How-to word doc for people to use. Although if you have a simpler way to do it i would still appreciate it!

Votes

Translate

Translate

Report

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 ,
Nov 13, 2024 Nov 13, 2024

Copy link to clipboard

Copied

LATEST

There are so many ways to do this.  This is probably the easiest:

1)  Create a hidden text field in the form (I'll call it "Data" in this example).

2) Enter the following validation script in the field:  event.target.defaultValue=event.value;  (This script updates the default value whenever the value changes.  This will prevent the field from getting cleared by any reset buttons on the form).

3)  Create a text file with 2 rows.  The first row will have the name of the field in step one (Data).  The second row will be list of dropdown entries, separated with a separator.  (A semi-colon is the separator in this example).  The text file content will look like this:

 

 

Data
A;B;C;D;E

 

 

If you want the first dropdown entry to be empy, add a ; at the beginning of the row:  ;A;B;C;D;E

4)  Enter the following script as a Mouse Down action in the dropdown:

event.target.setItems(this.getField("Data").value.split(";"));

5)  To update the list after the text file has been updated, simply run this line of code:

this.importTextData();

The script can be run from the console, from a button on the form, or from a custom menu item or toolbar  button.  It is not privileged so it will open a browse window to select the text file and the first row of data (there is only one row).  The Data field is now updated and the dropdown will be updated when the user clicks it.  You can full automate this to one click by creating a folder level script with a trusted function and entering the cPath parameter and the row number of importTextData method, and calling the function from any of the previously mentioned locations.

You can also update the dropdown in the same script instead of a mouse down action in the dropdown itself by adding this line of code at the end:

this.getField("Dropdown1").setItems(this.getField("Data").value.split(";"));

 

To answer your question about storing the data in a comment annotation, you can store it in the contents property of any annotation.  I find a form field is a lot easier.

Votes

Translate

Translate

Report

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 ,
Nov 12, 2024 Nov 12, 2024

Copy link to clipboard

Copied

Annotations doesn't have a property filename.

Votes

Translate

Translate

Report

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 ,
Nov 13, 2024 Nov 13, 2024

Copy link to clipboard

Copied

ah that would explain why it doesn't work then.

Votes

Translate

Translate

Report

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 ,
Nov 12, 2024 Nov 12, 2024

Copy link to clipboard

Copied

If you're interested, I've developed this (paid-for) tool that does that just for you. The data file can be attached to the PDF (as an attachment, not a comment, though), and then used to populate the field whenever the file is opened. See: https://www.try67.com/tool/acrobat-import-items-from-a-text-file-to-a-combo-box-or-list-field

 

Votes

Translate

Translate

Report

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 ,
Nov 13, 2024 Nov 13, 2024

Copy link to clipboard

Copied

Yeah I saw a link to your tool earlier and it does look very nice and simple to use however I am only working on one document and its on a work computer so im not sure if i could install it, i might be able to but i am unsure. I thought it would be a lot easier than this lol, but i guess Adobe had to make things difficult

Votes

Translate

Translate

Report

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