7 Replies Latest reply on Jan 30, 2013 6:30 PM by Sudha K

    Read CSV in acrobat javascript

    Sudha K Level 1

      Hi,

               I am new to acrobat javascript. I want  to get the data from the CSV file and do process in active pdf file(opened current document) .Please, any one of you help , by provinding code for this.

       

       

      thanks in advance

        • 1. Re: Read CSV in acrobat javascript
          Dave Merchant MVP & Adobe Community Professional

          If the CSV file is an attachment to the PDF you can use the util object's methods in a document-level script to read it into a string, e.g.

           

          var oFile = this.getDataObjectContents("MyFile.csv");

          var cFile = util.stringFromStream(oFile, "utf-8");

           

          In Acrobat X and later, if the CSV file is external to the PDF and the script is running at folder-level you can use another method to read it:

           

          var oFile = util.readFileIntoStream("/c/MyFolder/MyFile.csv");

          var cFile = util.stringFromStream(oFile, "utf-8");

           

          You can then use JavaScript's split() method to convert the string into lines and fields, making sure you handle the contents of the file properly (e.g. quoted or empty fields, etc.). See http://stackoverflow.com/questions/1293147/javascript-code-to-parse-csv-data for some example functions that can cope with quotes and nulls.

           

          See http://www.adobe.com/content/dam/Adobe/en/devnet/acrobat/pdfs/js_api_reference.pdf for more information on the util object.

          • 2. Re: Read CSV in acrobat javascript
            try67 MVP & Adobe Community Professional

            Just my 2 cents on this matter... Even though readFileIntoStream is very handy, it has an undocumented limit and will stop reading data from a file after a certain amount of characters (I don't remember exacly how many, but it's not that much).

            The biggest problem is that it doesn't throw an error or anyhing like that when that happens, it just stops mid stream...

            This is why I have abandonded using it and have stuck with the more cumbersome, but also more reliable, method of importing the external file as a data object and then reading it using getDataObjectContents, which has no such limitations.

            • 3. Re: Read CSV in acrobat javascript
              Dave Merchant MVP & Adobe Community Professional

              There's no (realistic) limit on the readFileIntoStream function until you hit the 32-bit address space limit - In testing I've fed Acrobat X and XI some very large files without a problem and stringFromStream will convert anything that fits into a ReadStream into a single string.

               

              e.g. from the console in Acrobat X:

               

              oFile = util.readFileIntoStream("/c/55-mb-textfile.txt"); // 620,384 lines of random ASCII with 54,441,593 chars

              var c = util.stringFromStream(oFile);

              console.println(c.length);

               

              54441593

              try67 wrote:

               

              Just my 2 cents on this matter... Even though readFileIntoStream is very handy, it has an undocumented limit and will stop reading data from a file after a certain amount of characters (I don't remember exacly how many, but it's not that much).

              • 4. Re: Read CSV in acrobat javascript
                try67 MVP & Adobe Community Professional

                Well, maybe it was fixed... It definitely happened with the first versions

                of Acrobat X. I haven't tested it in a while, though, I must admit.

                Anyway, it's good to hear it's working well now!

                • 5. Re: Read CSV in acrobat javascript
                  Sudha K Level 1

                  Thanks to Dave and try67 for your reponse.

                   

                  I tried to read csv file as you said , but its showing exception and not working.

                   

                  try{

                   

                      var oFile = util.readFileIntoStream("/Users/laser/Desktop/CSVFile.csv");

                      var c = util.stringFromStream(oFile);

                      app.alert(c.length+"=test");

                  }catch(e){

                      app.alert("catch="+e);

                  }

                   

                  Screen shot 2013-01-30 at 12.00.25 PM.png

                  Note :  I am using inbuilt editor.

                   

                   

                  Please help on this, i could not read csv file. I am trying this for many days.

                  • 6. Re: Read CSV in acrobat javascript
                    George_Johnson MVP & Adobe Community Professional

                    The util.readFileIntoStream method was introduced in Acrobat 10. What version of Acrobat are you using?

                    • 7. Re: Read CSV in acrobat javascript
                      Sudha K Level 1

                      I am using Acrobat 8.0