How do I create a path that util.readFileIntoStream will recognize?

I want to have a javascript that will run on a page open that checks for an xml data file. If found it will load data from that file. The PDF file and xml data file get created and put into a folder together. The xml data file is named the same as the PDF except the ".pdf" is replaced with "_Data.xml".

When I try to create the path to the data file the readFileIntoStream always fails.

If I don't include a path and let readFileIntoStream present an open file dialog, when I choose the file it works.

How do I get my constructed path to be recognized by readFileIntoStream?

Here is some of the code:

var fileName = this.documentFileName;
var filePath = this.path;
var dataFileName = "";
var dataFilePath = "";

dataFileName = fileName.replace(".pdf","_Data.xml");
dataFilePath = filePath.replace(fileName,dataFileName);

dataStream = util.readFileIntoStream(dataFilePath);
app.alert("Not found");

Peter Mitchell

3 Answers

Voted Best Answer

Unless this document is used in a closed environment where you can install folder level scripts on everyone's system, you won't be able to use the util.readFileIntoStream() function. A better method is to attach the XML file to the PDF, then use the Doc.getDataObjectContents() fucntion to read the data. The data travels with the PDF and script will work anywhere, including on Reader.

Thom Parker
The source for PDF Scripting Info

The Acrobat JavaScript Reference, Use it Early and Often

The most important JavaScript Development tool in Acrobat
The Console Window (Video tutorial)
The Console Window(article)

Having trouble, Why Doesn't my Script Work

By Thom Parker   

The issue is not the path, but the fact that when you use it the method has to run from a privileged context.
Read the reference for that parameter once more.

Edit: I see now that there's actually also a problem with your path. Try printing dataFilePath to the console just before calling RFIS and you'll see the problem.

Visit my custom-made PDF scripts website:
Contact me personally:

Gilad D (try67)   

I think the path is okay, I displayed it in an alert and it looked correct. (There may be a typo in my example since I couldn't paste it in.) I missed the detail of "privileged context", I'm new to javascript and scripting pdfs.

I added my form "working" folder to the security section as a privileged location and the readFileIntoStream command worked. Means we would need to get each user to do this.

The idea of attaching the xml to the pdf sounds good, but I don't think I could do that from an outside application. Each time one of these forms will be used it is copied from a template and then the xml is generated to pre fill the form. This is not done in Acrobat but in our own application and done on each individuals computer, not centrally.

Thanks for the suggestions.

Peter Mitchell   

Please specify a reason: