These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Populating a combobox from a text file

gjconely
Registered: Oct 23 2007
Posts: 3

I have a text file attached to my PDF. Each line in the text file should be an unique value within the combobox. I am adding a "OnFocus -> Run Javascript" command to the text box. I am then using the following code:

var oFile = this.getDataObjectContents("doctypes.txt");
var cFile = util.stringFromStream(oFile, "utf-8");
this.getField("txtDocTypeText").clearItems;
var cLines = cFile.split("\r\n");
for (i=0; i Parameter cName.
-----------------------------

Thanks.

gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4308
Have you created the data object within the PDF?

The following script works for me in version 7:

console.show(); console.clear();

console.println("\nCreate data object:");
// create data object
this.createDataObject("doctypes.txt", "TXT\r\nDAT\r\nDOC\r\nDOCX", "utf-8");

// list data objects
console.println("\nList of data objects:");
var d = this.dataObjects;
for (var i = 0; i < d.length; i++)
console.println("Data Object[" + i + "]=" + d[i].name);

// list data object properties
console.println("\nProperites for data object:");
var MyData = this.getDataObject("doctypes.txt");
for (var i in MyData) console.println("DocTypes." + i + "=" + MyData[i]);


// list contents
console.println("\nContents of data object:");
if ( app.viewerVersion >= 7 ) {
// get the file stream object of the embedded file
var oFile = this.getDataObjectContents("doctypes.txt");
// convert to a string & print
var sContents = util.stringFromStream(oFile, "utf-8");
console.println("Lenght of contents: " + sContents.length);
console.println(sContents);
} else {
app.alert("Acrobat 7.0 or later is required.");
}

console.println("\nArray element contents");
var aContents = sContents.split("\r\n");
console.println("Number of arrays: " + aContents.length);
for (i = 0; i < aContents.length; i++)
console.println(i + ": " + aContents[i]);

var oFile = this.getDataObjectContents("doctypes.txt");
var cFile = util.stringFromStream(oFile, "utf-8");
this.getField("txtDocTypeText").clearItems();
var cLines = cFile.split("\r\n");
for (i=0; i

George Kaiser

gjconely
Registered: Oct 23 2007
Posts: 3
Okay, last question on this. How can I get it to run this code when the PDF is opened?
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4308
You can add it to the "PageOpen" action, best on a one page form and add some code to prevent multiple runs, or as a Document Level JavaScript.

Be sure to check the documentation for restrictions, especially for the "importDataObject()" method.

George Kaiser