Skip to main content
Participating Frequently
November 12, 2024
Question

Adobe Acrobat Pro Javascript Import Dropdown list items from text file

  • November 12, 2024
  • 3 replies
  • 1428 views

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();

 

 

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.

 

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

This topic has been closed for replies.

3 replies

try67
Community Expert
Community Expert
November 12, 2024

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

 

Participating Frequently
November 13, 2024

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

Bernd Alheit
Community Expert
Community Expert
November 12, 2024

Annotations doesn't have a property filename.

Participating Frequently
November 13, 2024

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

PDF Automation Station
Community Expert
Community Expert
November 12, 2024

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.

Participating Frequently
November 13, 2024

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