How to read lines from a text file with Javascript?

Explorer ,
Jan 24, 2019 Jan 24, 2019

Copy link to clipboard

Copied

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

Views

3.2K

Likes

translate

Translate

Translate

Report

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

Most Valuable Participant , Jan 24, 2019 Jan 24, 2019
Use the readFileIntoStream method of the util object.

Likes

translate

Translate

Translate
Most Valuable Participant ,
Jan 24, 2019 Jan 24, 2019

Copy link to clipboard

Copied

Use the readFileIntoStream method of the util object.

Likes

translate

Translate

Translate

Report

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

Copy link to clipboard

Copied

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

Likes

translate

Translate

Translate

Report

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
Most Valuable Participant ,
Jan 24, 2019 Jan 24, 2019

Copy link to clipboard

Copied

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.

Likes

translate

Translate

Translate

Report

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

Copy link to clipboard

Copied

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.

Likes

translate

Translate

Translate

Report

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
Most Valuable Participant ,
Jan 24, 2019 Jan 24, 2019

Copy link to clipboard

Copied

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.

Likes

translate

Translate

Translate

Report

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

Copy link to clipboard

Copied

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.

Likes

translate

Translate

Translate

Report

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
Most Valuable Participant ,
Jan 24, 2019 Jan 24, 2019

Copy link to clipboard

Copied

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.

Likes

translate

Translate

Translate

Report

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

Copy link to clipboard

Copied

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.

Likes

translate

Translate

Translate

Report

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
Adobe Community Professional ,
Jan 25, 2019 Jan 25, 2019

Copy link to clipboard

Copied

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

Likes

translate

Translate

Translate

Report

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

Copy link to clipboard

Copied

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.

Likes

translate

Translate

Translate

Report

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
Adobe Community Professional ,
Jan 25, 2019 Jan 25, 2019

Copy link to clipboard

Copied

Use Med.value, not only Med.

Likes

translate

Translate

Translate

Report

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

Copy link to clipboard

Copied

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.

Likes

translate

Translate

Translate

Report

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

Copy link to clipboard

Copied

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

Likes

translate

Translate

Translate

Report

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
Most Valuable Participant ,
Jan 24, 2019 Jan 24, 2019

Copy link to clipboard

Copied

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.

Likes

translate

Translate

Translate

Report

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