• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Open and close PDF files from a CVS list

Community Beginner ,
Mar 21, 2016 Mar 21, 2016

Copy link to clipboard

Copied

I would like to perform some actions on a list of files. I have them in a CSV file. I can read it in OK in the Console but cannot open them. The paths are correct, I can open one. But in the loop it does not work. Why?

var oFile = util.readFileIntoStream("/C/test.txt");

var cFile = util.stringFromStream(oFile, "utf-8");

var n = cFile.split(",");

//second part

for (var i in n) {

  var otherDoc = app.openDoc(n);

  otherDoc.closeDoc();

}

undefinedInvalid Location "/D/test.pdf". File or folder does not exist.

NotAllowedError: Security settings prevent access to this property or method.

App.openDoc:2:Console undefined:Exec

undefined

TOPICS
Acrobat SDK and JavaScript

Views

855

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Mar 21, 2016 Mar 21, 2016

OK, it's actually not a line-break, but something called BOM, which appears as the first character of some UTF-8 text files, especially those created on a Mac.

All you have to do is remove it from the strings. You can use something like this in your loop:

var filePath = n.replace("\uFEFF", "");

var otherDoc = app.openDoc(filePath);

You're benefiting from my experience here because I recently encountered this same issue and it took me several hours of frustration to solve it...

Votes

Translate

Translate
Community Expert ,
Mar 21, 2016 Mar 21, 2016

Copy link to clipboard

Copied

Are you sure the file path is correct?

What happens when you run just this code from the console:

app.openDoc("/D/test.pdf");

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 21, 2016 Mar 21, 2016

Copy link to clipboard

Copied

If I run it in a fresh console window it works as a single line. After I try the loop it does not. I thought maybe this is some security restriction on iteration...

I am generally struggling with accessing files. The idea is to list all files in a directory, then manually remove some files on the list, then perform some operations on the remaining files. If I am missing a better way, I am all ears.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 21, 2016 Mar 21, 2016

Copy link to clipboard

Copied

Here is the evidence try67‌

var oFile = util.readFileIntoStream("/C/test/test.txt");

var cFile = util.stringFromStream(oFile, "utf-8");

var n = cFile.split("\n");

console.println(n);

/C/test/test.pdf

true

for (var i in n) {

  var otherDoc = app.openDoc(n);

  otherDoc.closeDoc();

}

Invalid Location "/C/test/test.pdf". File or folder does not exist.

NotAllowedError: Security settings prevent access to this property or method.

App.openDoc:2:Console undefined:Exec

undefined

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 21, 2016 Mar 21, 2016

Copy link to clipboard

Copied

This doesn't really prove anything... Can you open this file path directly, as I asked?

Also, change this line:

console.println(n);

To:

console.println(n.toSource());

And see what the output is... My guess is you're not removing all of the line-breaks from the input file.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 21, 2016 Mar 21, 2016

Copy link to clipboard

Copied

Thanks, try67‌

//opening directly

var otherDoc = app.openDoc("/C/test/test.pdf");

undefined

//and the file opens

//running this second

var oFile = util.readFileIntoStream("/C/test/test.txt");

var cFile = util.stringFromStream(oFile, "utf-8");

var n = cFile.split("\n");

console.println(n.toSource());["\uFEFF/C/test/test.pdf"]

true

//now running this

for (var i in n) {

  var otherDoc = app.openDoc(n);

  otherDoc.closeDoc();

}Invalid Location "/C/test/test.pdf". File or folder does not exist.

NotAllowedError: Security settings prevent access to this property or method.

App.openDoc:2:Console undefined:Exec

undefined

//i guess this looks like you are right. then how can i output a list of file and read them back into an array with those breaks removed?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 21, 2016 Mar 21, 2016

Copy link to clipboard

Copied

OK, it's actually not a line-break, but something called BOM, which appears as the first character of some UTF-8 text files, especially those created on a Mac.

All you have to do is remove it from the strings. You can use something like this in your loop:

var filePath = n.replace("\uFEFF", "");

var otherDoc = app.openDoc(filePath);

You're benefiting from my experience here because I recently encountered this same issue and it took me several hours of frustration to solve it...

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 21, 2016 Mar 21, 2016

Copy link to clipboard

Copied

LATEST

I see. Many thanks try67 !!! Here is how it looks now, as I was getting line breaks in multi-line files too

for (var i in n) {

  var otherDoc = app.openDoc(n.replace(/(\r\n|\n|\r)/gm,"").replace("\uFEFF", ""));

  otherDoc.closeDoc();

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines