Skip to main content
Participant
August 9, 2017
Question

Prompt user to select an existing .txt file on their computer

  • August 9, 2017
  • 3 replies
  • 621 views

I am working on a script that is trying to read files (in .txt format) and manipulate them. I am trying to prompt the user to find and select an existing file on their computer. How can I write the code in such a way that triggers a pop up file explorer window (compatible with Adobe Acrobat DC)? I want the user to be able to find any document instead of dictating the file name or file location within the script. Once this is achieved, I would set the file name to a string variable called fileName. I am unsure where to begin with this process, so any help would be greatly appreciated. If there is an easier or more efficient method of achieving this goal please feel free to share. Thank you.

This topic has been closed for replies.

3 replies

jpags98Author
Participant
August 14, 2017

Thank you both for your help!

After this, I also need to add to the file and rewrite (or "update") its contents before saving it again. How would I go about converting the string back into a stream for the file?

try67
Community Expert
Community Expert
August 14, 2017

If you use the importDataObject method the file selected will become attached to the PDF. You can then edit its contents using the setDataObjectContents method, and then export it using the exportDataObject method. However, the user will have to select where to save it. You can't do it silently without their approval.

Karl Heinz  Kremer
Community Expert
Community Expert
August 9, 2017

You can do this using the util.readFileIntoStream() method (Acrobat DC SDK Documentation ) - this will not give you the filename, but instead will actually read the file, and you can then access it (see the example in the documentation) e.g. as string and process it.

try67
Community Expert
Community Expert
August 9, 2017

From my experience this method only reads a specific maximum amount of characters (4096, I believe) and then stops without warning or an error, so be careful when using it.

Karl Heinz  Kremer
Community Expert
Community Expert
August 9, 2017

Hmmm... I've been using this for a while with files in the 20K range without a problem. I actually just marked up a file with some specific information in the last line so that I would be able to see if the whole file got imported, and again, no problems. Is it possible that Adobe fixed what was wrong with it?

try67
Community Expert
Community Expert
August 9, 2017

Is this script supposed to work in Reader, or just in Acrobat?

jpags98Author
Participant
August 14, 2017

For now, I am working in just Acrobat. However, by the end of this project I need it to work in Reader.

try67
Community Expert
Community Expert
August 14, 2017

In Acrobat you can use the importDataObject method to do it. With Reader I think the only option is the readFileIntoStream method mentioned by KHK above.