7 Replies Latest reply: Aug 24, 2011 12:52 AM by try67 RSS

    Reading a text file until eof and adding data to new template pages... Totally lost

    mcevoym1

      Hi Everyone,

          I'm new to java scripting and using it in Acrobat the following code is from what I found doing some googling. 

       

      importTextData();

      var testtemp = getTemplate("Test-template");

      testtemp.spawn(numPages,true,false);

       

         What I'm not sure is how to create the loop for the EOF and how to populate the newly created template pages.  Any direction or help will be appreciated.

       

      Thanks,

      Mitch

        • 1. Re: Reading a text file until eof and adding data to new template pages... Totally lost
          GKaiseril CommunityMVP

          Have you looked at the Acrobat JavaScirpt API Reference?

           

          The 'importTextData()' has optional input parameters and a return value that may help you with your problem.

           

          You can check the return value of -3 from the method or test to see if the values returned remain the same as the previous record to detect the EOF of a file. You can then use an 'until' loop to pull each row until you reach the End Of File condition.

           

          // full file path & name
          var cFilePath = "/c/data/myData.txt"
          // row to retrieve
          var nRow = 0;
          // return value from importTextData
          var nReturn = 0;
          // previous record values
          var cFirst, cMiddle, cLast;
          // template for creating new pages
          var oTestTemp = this.getTemplate("Test-template");
          // continue while return value from importTextData is No Error
          while (nReturn == 0) {
          //
          nReturn = this.importTextData({cPath: cFilePath, nRow: nRow})
          switch(nReturn) {
             case -3:
                if(cFirst == this.getField('First').value & cMiddle == this.getField('Middle').value & cLast == this.getField('Last').value) {
                   // EOF condition
                   app.alert("EOF encountered. Row: " + nRow, 3, 0);
                   } else {
                   app.alert("Warning: Missing Data. Row: " + nRow, 1, 0);
                   } // End EOF test
                break;
             case -2:
                app.alert("Warning: User Canceled Row Select. Row: " + nRow, 1, 0);
                break;
             case -1:
                app.alert("Warning: User Canceled File Select. Row: " + nRow, 1, 0);
                break;
             case 0:
                // No Error
                // save values to test for EOF
                cFirst = this.getField('First').value;
                cMiddle = this.getField('Middle').value;
                cLast = this.getField('Last').value;
                // processing for retrieved record
                oTestTemp.spawn({nPage: numPages, bRename: true, bOverlay: false});
                      break;
             case 1:
                app.alert("Error: Can not open file. Path: " + cFilePath, 0, 0);
                break;
             case 2:
                app.alert("Error: Can not load data. Row: " + nRow, 0, 0);
                break;
             case 3:
                app.alert("Error: Can invalid row. " + cFilePath, 0, 0);
                break;
             default:
                app.alert("Unknown condition. Return: " + nReturn + " Row: " + nRow + " File path: " + cFilePath, 0, 0);
                break;
             } // end switch for nReturn
             //
          nRow++; // increment row to retrieve
          } // end while

          There are also some security restrictions on running this code that may limit the usability of this approach.

          • 2. Re: Reading a text file until eof and adding data to new template pages... Totally lost
            mcevoym1 Community Member

            Thanks GKaiseril I'll look at implmenting that and get back to you!! 

            • 3. Re: Reading a text file until eof and adding data to new template pages... Totally lost
              mcevoym1 Community Member

              Thanks again for the code I made a few changes so it matched my test document a little better and was having some issues but I changed the red text below to cFilePath: instead of cPath and it seems to run now!!!  I have a few follow up questions if you have the time?

               

               

              // full file path & name

              var cFilePath = "/c/test.txt"

              // row to retrieve

              var nRow = 0;

              // return value from

              // importTextData

              var nReturn = 0;

              // previous record values

              var cName, cOEN, cDOB, cGender;

              // template for creating new

              //pages

              var oTestTemp = this.getTemplate("Test-template");

              // continue while return value

              //from importTextData is No

              // Error

              while (nReturn == 0) {

              //

              nReturn = this.importTextData({cPath: cFilePath, nRow: nRow})

              switch(nReturn) {

                 case -3:

                    if(cName == this.getField('Name').value & cOEN == this.getField('OEN').value & cDOB == this.getField('DOB').value & cGender == this.getField('Gender').value) {

                       // EOF condition

                       app.alert("EOF encountered. Row: " + nRow, 3, 0);

                       } else {

                       app.alert("Warning: Missing Data. Row: " + nRow, 1, 0);

                       } // End EOF test

                    break;

                 case -2:

                    app.alert("Warning: User Canceled Row Select. Row: " + nRow, 1, 0);

                    break;

                 case -1:

                    app.alert("Warning: User Canceled File Select. Row: " + nRow, 1, 0);

                    break;

                 case 0:

                    // No Error

                    // save values to test for EOF

                    cName = this.getField('Name').value;

                    cOEN = this.getField('OEN').value;

                    cDOB = this.getField('DOB').value;

                    cGender = this.getField('Gender').value;

                    // processing for retrieved record

                    oTestTemp.spawn({nPage: numPages, bRename: true, bOverlay: false});

                          break;

                 case 1:

                    app.alert("Error: Can not open file. Path: " + cFilePath, 0, 0);

                    break;

                 case 2:

                    app.alert("Error: Can not load data. Row: " + nRow, 0, 0);

                    break;

                 case 3:

                    app.alert("Error: Can invalid row. " + cFilePath, 0, 0);

                    break;

                 default:

                    app.alert("Unknown condition. Return: " + nReturn + " Row: " + nRow + " File path: " + cFilePath, 0, 0);

                    break;

                 }

              // end switch for nReturn

              //

              nRow++;

              // increment row to retrieve

              }

              // end while

               

              1) For every line of the data file it opens the "Select File Containing Form Data" window.  From what I've read this might be the security issue you were talking about if it helps I'll only ever be running this script on my system if that helps?

               

              2) The last line of data in the file populates to a new page as well as the first page. i.e.

              page 1: Wayne

              page 2: Mitch (first in the file)

              page 3: Nancy (second)

              page 4: Wayne (third)

               

              Is there a way to avoid this?

               

              • 4. Re: Reading a text file until eof and adding data to new template pages... Totally lost
                GKaiseril CommunityMVP

                Yes, so unless you run this code from the JS console or you need to write a trusted function to perform the read or the entire code.

                 

                If you have field name with the same name as field on your template, then that field will display the value that appears on the template and if you change that field's value the value on the template will also change.

                • 5. Re: Reading a text file until eof and adding data to new template pages... Totally lost
                  try67 CommunityMVP

                  If you're using Acrobat X, I suggest considering switching to util.readFileIntoStream(), which will allow you to read the entire contents of a text file, and then process it as you wish.

                  That way you won't have to create a trusted function or select the file over and over again.

                  • 6. Re: Reading a text file until eof and adding data to new template pages... Totally lost
                    mcevoym1 Community Member

                    I have been using Pro 9 but I can get a license for X today I like the sound of the new util.readFileIntoStream(), but again I'm at a loss on how to change the existing code to using this new function.  Any pointers?

                    • 7. Re: Reading a text file until eof and adding data to new template pages... Totally lost
                      try67 CommunityMVP

                      Basically, I would do it like this:

                      1. Read the contents of the file and split them by line break.

                      You will end up with an array containing your values.

                      2. You then loop over this array, applying the values to your fields and

                      spawning new pages.