0

I have a PDF document with anywhere from 80 to 150 pages. We use Javascript in the admin console to extract each page and save the file based on some text at the top of each page. The code below works except for when the file has more than 49 pages. At first, I thought it was because the loop causes a race condition where closeDoc doesn't fire because the loop is still looping. But no matter what type of iterator I use, the script crashes on file 50 with the following error:

RaiseError: The maximum number of files are already open. No other files can be opened or printed until some are closed.

I even tried a 1-25 loop, then on to a 26 - 50 loop, etc. but the same error message always happens. I also tried app.setTimeout thinking that it would eliminate the race issue. I've also tried calling it as a function and passing in the page number. And unfortunately Acrobat DC does not include support for AWAIT.

Here is the code:

for(var i=0; i<this.numPages; i++) {

    var oNewDoc = this.extractPages({nStart: i, nEnd: i});
    
    //folderPath and filename logic goes here

    oNewDoc.saveAs(folderPath+filename);
    oNewDoc.closeDoc(true); 
}

I need this to be a synchronous process. Anyone have any solutions?

2 Answers2

0

One thing you could try out is after the closeDoc() command to add a loop which checks the open docs, and wait until the single page document is really closed.

Max Wyss
  • 3,549
  • 2
  • 20
  • 26
  • Thanks Max for the suggestion, however, Acrobat doesn't have a way to tell if a document is closed. You could iterate over the open docs and do some if-then there but it's the outside loop that's causing the problem. – Matt Allison Oct 28 '20 at 11:40
0

After some exhaustive searching, I came up with a reference that helped. It seems like it's the same kind of iteration (maybe more while-ish) but this does work.

makeBlankFile = app.trustedFunction(function() {

    var oNewDoc;
    app.beginPriv();
    oNewDoc = this.extractPages({nStart: fileCount, nEnd: fileCount});
    oNewDoc.saveAs(folderPath+filename);
    oNewDoc.closeDoc(true);
    app.endPriv();
    fileCount++;

    if (fileCount < pageInfo.length)
        t = app.setTimeOut("makeBlankFile();", 10);

});

t = app.setTimeOut("makeBlankFile();", 10);