Search code examples
javascriptexcelvbarelative-pathacrobat

Acrobat Reader doesn't recognize relative path


I exported an Excel sheet to a tab delimited file (.txt) and now try to batch-import it into a PDF-form via Acrobat Reader Pro DC. This works perfectly with an action, that imports said .txt, as long, as the path is absolute. Because I want to ship the file and have other people run in as well, I need to have the path relative. My current approach is to change

var fileName = "/C/Users/somefancyname/Desktop/export.txt";
this.importTextData(fileName, somerowcounter)

to

var fileName = "export.txt";
this.importTextData(fileName, somerowcounter)

This, unfortunately throws an "Cannot Open File" error. The file I run this script on is on the very same level, as the text file. My only guess would be, that the path is relative to the acrobat app, instead of the document, but I can't help it anymore, as this is my first approach with Acrobat JS, ever.

Thanks for any suggestions!


Solution

  • I found a hacky way to create a pseudo-relative path. Tested, works!

    You simply use the length of the documents filename and substract it from the documents path. This awards you with the basepath of the document.

    var filenameLength = this.documentFileName.length * -1;
    var basePath = this.path.slice(0, filenameLength);
    

    You then proceed to use this basepath to create an absoulte path to your files in this place, as if these were relative paths.

    var fileName = basePath + "export.txt";
    var outputFodler = basePath + "/OutputFolder/";
    ...