Skip navigation
Currently Being Moderated

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

Aug 18, 2011 6:14 AM

Hi Everyone,

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



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



   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.




  • Currently Being Moderated
    Aug 18, 2011 10:13 AM   in reply to mcevoym1

    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
       case -2:
          app.alert("Warning: User Canceled Row Select. Row: " + nRow, 1, 0);
       case -1:
          app.alert("Warning: User Canceled File Select. Row: " + nRow, 1, 0);
       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});
       case 1:
          app.alert("Error: Can not open file. Path: " + cFilePath, 0, 0);
       case 2:
          app.alert("Error: Can not load data. Row: " + nRow, 0, 0);
       case 3:
          app.alert("Error: Can invalid row. " + cFilePath, 0, 0);
          app.alert("Unknown condition. Return: " + nReturn + " Row: " + nRow + " File path: " + cFilePath, 0, 0);
       } // 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.

    Mark as:
  • Currently Being Moderated
    Aug 22, 2011 11:25 AM   in reply to mcevoym1

    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.

    Mark as:
  • Currently Being Moderated
    Aug 23, 2011 5:30 AM   in reply to mcevoym1

    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.

    Mark as:
  • Currently Being Moderated
    Aug 24, 2011 12:52 AM   in reply to mcevoym1

    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.

    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points