Skip to main content
Monteigo99
Inspiring
June 19, 2017
Answered

Having issues with batch import javascript from txt file with Enterprise security Trust and Privilege issues?

  • June 19, 2017
  • 2 replies
  • 2490 views

Hello all,

I'm lost and have been struggling with this for a couple weeks now. I am a decent beginner level who can understand js, copy it and past, then modify to get it working. I am much more limited with regards to creating new scripts from scratch.

I have a js 'Action' that performs a batch import: locates txt file, imports data for first record, saves the file with one of the field names, then automatically, pulls the next record and repeats.

I'm using Acrobat Pro XI.

The problem is that while it populates the data, it will not save the new file to my folder, and thus, will not proceed to the next file.

I have performed the research and have traced the problem to the security issues associated with saving files on my company's drives. I believe this is a Trust function and Privilege scripting. I don't quite seem to understand

1) how to create a .js file that I save in the javacript folder,

2) the javascript folder location is different from where I have seen in blogs that it is supposed to be (it is here: C:\Program Files (x86)\Adobe\Acrobat 11.0\Acrobat\Javascripts), and

3) the difference between writing the trust script into the Action js and simply creating a js to go and retrieve the trusted script from the js file.

I have tried all sorts of variations to no avail. Any assistance would be really appreciated.

Current script:

/* Autofill OPPE Records */
// specify the filename of the data file
var fileName = "/c/Users/XXXXX/Desktop/Temp Files/NeuroTestDatafile.txt";
var outputDir = "/c/Users/XXXXX/Desktop/Temp Files/OPPEAutoPopTest/";

var err = 0;
var idx = 0;
while (err == 0){
err = this.importTextData(fileName, idx); // imports the next record
if (err == -1)
app.alert("Error: Cannot Open File");

else if (err == -2)
app.alert("Error: Cannot Load Data");

else if (err == 1)
app.alert("Warning: Missing Data");

else if (err == 2)
app.alert("Warning: User Cancelled Row Select");

else if (err == 3)
app.alert("Warning: Missing Data");   

else if (err == 0){
     this.saveAs(outputDir + this.getField("Provider").value + ".pdf")

}
}

This topic has been closed for replies.
Correct answer Monteigo99

There is a line missing after your call to saveAs() (see my original article with the code here: Batch-Import Excel Data into PDF Forms - KHKonsulting LLC​):

idx++;

This is how the counter gets incremented, and the next time around, the second, third and so on line will be processed.


Karl,

this.saveAs(outputDir + this.getField("Text1").value + "_" + this.getField("Text2").value + ".pdf"); // saves the file

  idx++;

This is from your blog that I gathered the script from. It looks like the idx++; is missing after the ".pdf"); which it is not but it doesn't show the 'enter' that separates it from the 'saves the file.

This is my new script and it is important to point out a couple things that us novices need to be careful of. 1) that the this.resetForm needs to go after the idx++; in order for it to populate the form first. I tried it in 3 separate locations and it broke the js.

2) when copying and pasting your script, the idx++; actually flows with the "saves the file" unless there is an enter after "file".

This script just saved my office about 150 hrs per year. Thank you so much, Karl. (I'll post this on your consulting blog too.)

Thank you too, Try67; you've been a significant help in the past with my js education.

Here is the full code.

/* Autofill OPPE Records */
// specify the filename of the data file
var fileName = "/c/Users/XXX/Desktop/Temp Files/NeuroTestDatafile.txt";
var outputDir = "/c/Users/XXX/Desktop/Temp Files/OPPEAutoPopTest/";

var err = 0;
var idx = 0;
while (err == 0){
err = this.importTextData(fileName, idx); // imports the next record

if (err == -1)
app.alert("Error: Cannot Open File");

else if (err == -2)
app.alert("Error: Cannot Load Data");

else if (err == 1)
app.alert("Warning: User Cancelled File Select");

else if (err == 2)
app.alert("Warning: User Cancelled Row Select");

else if (err == 3)
app.alert("Warning: Missing Data");   

else if (err == 0){
this.saveAs(outputDir + this.getField("Provider").value + ".pdf");idx++; // saves the file idx++;
this.resetForm();
}
}

2 replies

try67
Community Expert
Community Expert
June 19, 2017

You're not explaining what the issue is... In your code you're not saving to a network drive, but to a local one.

So, is there an error message when you run the code above? If so, what does it say? Also, from where are you running this code?

Karl Heinz  Kremer
Community Expert
Community Expert
June 19, 2017

One of your error cases is wrong, this is the correct one:

else if (err == 1)

  app.alert("Warning: User Cancelled File Select");

This does however not explain why your save operating never gets called or never finishes. The usual case is that you run into an error when importing the record, and that should display an error message (via the "app.alert()" method). In case something else goes wrong, you should see an error written to the Javascript console. I've never seen a case where no error was produced, but the file was also not saved. Unless you are running into something completely new or different, there should be an error reported somewhere. That's why we are asking for what's going on in the JavaScript console.

If there truly is no error message anywhere, you need to debug the code by either stepping though it line by line, or by adding console.print() statements to see how far your code actually gets.

Monteigo99
Inspiring
June 19, 2017

Hey Karl, this is actually your script. I've communicated with you several times on your consulting website, but after reviewing 2-3 years worth of comments, I came up with it being the security issues with saving to a Privilege folder with Trust Functions. I don't quite understand how to do this though.

Bernd Alheit
Community Expert
Community Expert
June 19, 2017

You can use an action in the Action Wizard for this.

Monteigo99
Inspiring
June 19, 2017

Thank you for responding, but that is what I'm doing. The action requires a script and I am trying to write the script per above.

Bernd Alheit
Community Expert
Community Expert
June 19, 2017

Any error message in the JavaScript console?