Skip to main content
Ryan_Oliver_02445
Known Participant
January 24, 2019
Answered

How to read lines from a text file with Javascript?

  • January 24, 2019
  • 1 reply
  • 9674 views

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!

This topic has been closed for replies.
Correct answer try67

Use the readFileIntoStream method of the util object.

1 reply

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
January 24, 2019

Use the readFileIntoStream method of the util object.

Ryan_Oliver_02445
Known Participant
January 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

try67
Community Expert
Community Expert
January 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.