Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

How to read lines from a text file with Javascript?

Explorer ,
Jan 24, 2019 Jan 24, 2019

Good evening, I am trying to find a way to read lines from a text file within Acrobat Javascript, more specifically, to search for a line among hundreds of lines.

I have looked over the Acrobat JS API reference, and I found that import/export text may not be the best idea since we don't use hundreds of text fields in our documents.

Any help would be wonderful, thank you!

TOPICS
Acrobat SDK and JavaScript , Windows
8.8K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Jan 24, 2019 Jan 24, 2019

Use the readFileIntoStream method of the util object.

Translate
Community Expert ,
Jan 24, 2019 Jan 24, 2019

Use the readFileIntoStream method of the util object.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 24, 2019 Jan 24, 2019

I looked into the function you recommended to me, and I have built my code off of that, along with other functions.

However, I am running into the problem that my string searches are returning number values, and not the string line itself.

Even if I use the, "String()" method, it still returns the same number value.

Here is my code:

this.importDataObject("MEDDIAGNOSISICD-10.txt", "C:\Users\dell\Documents\tab excel\MEDDIAGNOSISICD-10.txt");

var oFile = this.getDataObjectContents("MEDDIAGNOSISICD-10.txt");

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

var Med1 = cFile.search("ADVIL");

var Text1 = String(Med1);

console.println(String(Text1));

the return value is: 9607

I repeated the String() method several times here, to see what worked... which all of my tests did not work, sadly.

Is there any way to fix this issue, and return the string line itself? try67​

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 24, 2019 Jan 24, 2019

You need to read the documentation of this method: JavaScript String search() Method

And you're not using the method I mentioned, but your way should work, too.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 24, 2019 Jan 24, 2019

Sorry, I couldn't seem to find the readFileIntoStream method in the JS API. 

As of right now, I've yet to find a way to return the full line in which the string is included, sadly.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 24, 2019 Jan 24, 2019

I would do it differently. I would split the full string into lines and then search them one by one (using a for-loop) until a match is found.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 24, 2019 Jan 24, 2019

How would one do this?  I have so far only been able to retrieve the full line, but not able to search fo specific values at all.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 24, 2019 Jan 24, 2019

Read about the split method and then use it to split the data into line (by specifying a line-break as the delimiter).

Then you'll just need to go over all the lines in the array and see which one contains the string you're looking for.

You can use the search command, or indexOf, to do that.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 25, 2019 Jan 25, 2019

I fixed the file path, and I found that separating the file by tabs works well in getting rid of the spaces, with each med and their code in the same line.

The Array is set up like this in string form:

   Xanax,Brand,Anxiety,Code

This set of code can call a specific string, set it to a variable, and print it into the console now:

this.importDataObject("MEDDIAGNOSISICD-10.txt", "C:/Users/dell/Documents/tab excel/MEDDIAGNOSISICD-10.txt");

var oFile = this.getDataObjectContents("MEDDIAGNOSISICD-10.txt");

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

var fileArray = cFile.split('\t');

var Med = this.getField("Medications 1");

var Index = fileArray.indexOf(Med.value);

var Call = fileArray[Index];

console.println(Call);

However, now I'm stuck with calling the only the Medication, and not the full line, sadly.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 25, 2019 Jan 25, 2019

You must check every line in the array for the medication.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 25, 2019 Jan 25, 2019

How would one write a code for that?  I have this,

var i, Index;

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

  Index = fileArray.indexOf(Med);

  if(Index > -1) console.println(fileArray);

}

Sadly it doesn't want to print out to the console.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 25, 2019 Jan 25, 2019

Use Med.value, not only Med.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 25, 2019 Jan 25, 2019

I changed the value from med, to med.value, and now it only prints to the console the Med.value twice, but not the line, sadly.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 25, 2019 Jan 25, 2019
LATEST

EDIT:  I changed the split from tabs to lines, and it now grabs the entire line!  Wundershon.  Thank you!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 24, 2019 Jan 24, 2019

PS. This line of code is incorrect and should not work at all:

this.importDataObject("MEDDIAGNOSISICD-10.txt", "C:\Users\dell\Documents\tab excel\MEDDIAGNOSISICD-10.txt");

You're not specifying the file-path correctly in the second parameter.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines