19 Replies Latest reply on Jun 23, 2017 1:17 PM by Joel_Geraci

    How to pull data from attachment?

    FyreHawke Level 1

      I am trying to pull data from an excel file into a form or text box.

      I have tried using importTextData, which works on a one-by-one basis, but can this be used to pull an attached file?

       

      The end result that i'm hoping for (in case there is a better way to do this) is this:

      My PDF will be a map with several points on it. I would like each point to be a clickable button/link, and when the user clicks on the point, either a text box pops up with additional info on that site, or there is a form/side panel that displays the additional info.

        • 1. Re: How to pull data from attachment?
          try67 MVP & Adobe Community Professional

          You can do it using the getDataObjectContents method.

          • 2. Re: How to pull data from attachment?
            FyreHawke Level 1

            I saw some into on getDataObjectContents, but how do I make it look up the specific point that i want into for (each point has a unique ID) and output to the form fields?

            • 3. Re: How to pull data from attachment?
              try67 MVP & Adobe Community Professional

              You need to parse the file, searching for the desired data. I don't know the structure of your file so I can't really help beyond that...

              However, this is not going to work with an Excel file (xls/xlsx/etc.). The file will have to be in plain-text format.

              • 4. Re: How to pull data from attachment?
                FyreHawke Level 1

                I have saved the file as a tab delimited txt file.

                The end result will have more fields, but for my test I only have 3 fields: ID, City, and Employees. These fields are the same in my PDF as they are in my txt file.

                 

                I found the code online to run getDataObjectContents, i'm using

                 

                var stmData = this.getDataObjectContents("importdata.txt");

                var strData = util.stringFromStream(stmData);

                 

                I run a app.alert(strData); to make sure the data is pulling, and it all comes up (I only have about 5 records for the text), but i'm not sure how to parse it to isolate the record that i'm looking for and fill the ID, City, and Employees fields?

                 

                Thanks for your help!

                • 5. Re: How to pull data from attachment?
                  Joel_Geraci Adobe Community Professional

                  try67  wrote

                   

                  The file will have to be in plain-text format.

                   

                  Not necessarily. XSLX is just XML and you can parse the XML using the XMLData object. You'll need to get really good at using XPath... which I don't consider to be trivial... but it is possible. The documentation infers that XMLData is for XFA manipulation but it works with any XML as you can see in the examples for the object.

                   

                  Acrobat DC SDK Documentation

                  • 6. Re: How to pull data from attachment?
                    try67 MVP & Adobe Community Professional

                    That's true. Thanks for the info!

                    • 7. Re: How to pull data from attachment?
                      FyreHawke Level 1

                      I've made a bit of progress in parsing, using the following:

                      var nPos = strData.indexOf(num);

                      var nvalue = strData.substr(nPos+3,10);

                      this.getField("City").value=nvalue;

                       

                      i'm able to have it search for an ID and post the first 10 characters after the ID into the city field

                      but there must be a way to have it go until the next tab instead of just saying "10 characters" right? because not all cities are 10 characters long.

                      There is a tab between my IDs, Cities, and Employees columns.

                      • 8. Re: How to pull data from attachment?
                        try67 MVP & Adobe Community Professional

                        You can split each line with the split method, and the tab character ("\t")

                        as the delimiter.

                        It will return an array with all the values in the line.

                        • 9. Re: How to pull data from attachment?
                          Joel_Geraci Adobe Community Professional

                          You can use the split method on the text file to create an array of arrays. First split the entire file by "\n" then split each element of the resulting array by "\t" and you'll get an array of rows that contains an array of cells per row. That's relatively easy but it does involve keeping track of what index element corresponds to which column.

                           

                          What I prefer to do is convert CSV text to JSON. It's much easier to work with and update the code later.

                           

                          Convert CSV to JSON in JavaScript | TechSlides 

                          • 10. Re: How to pull data from attachment?
                            FyreHawke Level 1

                            Sorry but i'm really new to this and i'm still having troubles.

                            I can split my data by "\n" and it splits at the line breaks, OR I can split it by "\t" and it splits at tabs, but I can't seem to do both.

                            • 11. Re: How to pull data from attachment?
                              Joel_Geraci Adobe Community Professional

                              You have to do one, then the other.

                               

                              You're starting with a long string that has line separators and within each line, cell separators.

                               

                              You first split by line ("\n"). This will return an array where each element of the array is a string that contains tabs.

                              You now loop through each element of the array of lines and split that by the tab ("\t");

                              You now have an array of arrays... let's call this myTable

                               

                              So the third cell in the 5th row can be accessed like this... arrays are zero based

                               

                              var myCell =  myTable[4][2]

                               

                              The code to do all that might look like this...

                               

                              var myTable = yourData.split('\n');

                              for (var i=0; i<myTable.length; i++) {

                                  y = myTable[i].split('\t');

                                  myTable[i] = y;

                              }

                              1 person found this helpful
                              • 12. Re: How to pull data from attachment?
                                FyreHawke Level 1

                                That worked great! Thank you!

                                 

                                Now I need to figure out how to find the ID that the user clicks on in the array, and return the next two values.

                                 

                                I found this code online to search, but when I put it into my code and run it, i'm getting a value of -1, any ideas?

                                 

                                var num = this.getField("ID").value;

                                var stmData = this.getDataObjectContents("importdata.txt");

                                var strData = util.stringFromStream(stmData);

                                var strArray = strData.split("\n");

                                for(var i=0; i<strArray.length; i++){

                                y=strArray[i].split('\t');

                                strArray[i]=y;

                                }

                                if(typeof Array.prototype.indexOf != "function"){

                                Array.prototype.indexof = function (el){

                                for(var b = 0; b < this.length; b++) if (el === this[b]) return b;

                                return -1;

                                }

                                }

                                app.alert(strArray.indexOf(num));

                                • 13. Re: How to pull data from attachment?
                                  Joel_Geraci Adobe Community Professional

                                  First... indexOf is a defined method of the String object in Acrobat JavaScript. You don't need to define it... plus there's a typo in your definition so it wouldn't work as you've coded it anyway.

                                   

                                  Second... You'll need to be more specific on exactly what you want to have happen. How did that value of "ID" get set? What exactly do you mean by "I need to figure out how to find the ID that the user clicks on in the array"?

                                  • 14. Re: How to pull data from attachment?
                                    FyreHawke Level 1

                                    ID is name name of a form field in my document.

                                    so i'm typing in one of the ids thats in my .csv file, for example 124. I would like it to search the array, find 124, and then spit out the following value, which will be a city, and put it into the form field called "city", and then go to the next value, employees, and put it into the form field called "employees"

                                    • 15. Re: How to pull data from attachment?
                                      Joel_Geraci Adobe Community Professional

                                      Just loop over the outer array, get the id from each row and compare it to the ID entered. The following value would be the index of the id +1

                                       

                                      There's a good example for looping over a multidimensional array here

                                      For loop in multidimensional javascript array - Stack Overflow

                                      • 16. Re: How to pull data from attachment?
                                        FyreHawke Level 1

                                        Yes, thats what I thought I could do (the index +1 and +2) but I can't seem to get the index part working?

                                         

                                        This code is returning -1

                                         

                                        var num = this.getField("ID").value;

                                        var stmData = this.getDataObjectContents("importdata.txt");

                                        var strData = util.stringFromStream(stmData);

                                        var strArray = strData.split("\n");

                                        for(var i=0; i<strArray.length; i++){

                                        y=strArray[i].split('\t');

                                        strArray[i]=y;

                                        }

                                        var result = strArray.indexOf(num);

                                        app.alert(result);

                                        • 17. Re: How to pull data from attachment?
                                          Joel_Geraci Adobe Community Professional

                                          But you know the Excel column that the ID is in? You don't need to use indexOf.

                                           

                                          If, for example, the ID is in column C, which would have an array index of 2...you'd get each row... lets call the current row "i" then just get the ID using it's index.

                                           

                                          var id = strArray[i][2];

                                          1 person found this helpful
                                          • 18. Re: How to pull data from attachment?
                                            FyreHawke Level 1

                                            yes, all of my IDs are in column 1, cities  are in column 2, and employees are in column 3

                                             

                                            so I need to search column 1 for the value that is entered in ID, how would I find the row number so that I could get strArray[1][0]

                                            • 19. Re: How to pull data from attachment?
                                              Joel_Geraci Adobe Community Professional

                                              Use the same kind of loop to compare the value of the field to the the value of the ID for that row. Google how to iterate over an array in JavaScript.