Skip to main content
Participant
December 25, 2023
Question

Acrobat X Pro - closedoc method doesn't close the doc immediately in loop, why?

  • December 25, 2023
  • 2 replies
  • 552 views
// Loop to create and save 10 PDF documents
for (var i = 1; i <= 10; i++) {// Create a new PDF document
var doc = app.newDoc();

// Save the document with the appropriate file name
var fileName = "/C/temp/test" + i + ".pdf";
doc.saveAs(fileName);

// Close the document
doc.closeDoc();
}

// Inform that the process is complete
console.println("Creation of 10 PDF files completed.");

 

That is my sample code above, when you run it, you can see all 10 files created, saved and then all 10 being closed at the same time. Why is this? Tested this in both in console and as a JS action.

 

After each file is created, that doc should close. I tried reading the SDK manual but there is nothing in there except for this:

It is important to use this method carefully, because it is an abrupt change in the document state that can affect any
JavaScript executing after the close. Triggering this method from a Page event or Document event could cause the
application to behave strangely.
In versions of Acrobat earlier than 7.0, a document that closes itself by executing this.closeDoc terminates any script
that follows it. In Acrobat 7.0, the script is allowed to continue and to terminate naturally. However, if the Doc of the
closed document is referenced, an exception will be thrown.

 

Any help is much appreciated. Thank you~

 

V

This topic has been closed for replies.

2 replies

JR Boulay
Community Expert
Community Expert
January 8, 2024

It's a screen refresh problem, Acrobat has always handled windows of open documents poorly, you just have to try to resize one to be convinced. While Photoshop or InDesign react in real time even with open documents of hundreds Mb.

Acrobate du PDF, InDesigner et Photoshopographe
try67
Community Expert
Community Expert
December 25, 2023

I've also encountered this issue, and found no solution for it. In later versions it's worse, though. There the files don't close at all, and once you get to 50 files the script errors out, as it's the maximum allowed number of files that can be opened simultaneously by Acrobat.

Participant
December 25, 2023

The solution I came up with for my scripts was just to adapt by creating temp files for all new/working docs that you plan to work on in the script (to prevent them from modifying existing docs that are open that you don't want your script to modify) and then manually open each temp one you need to do your stuff on.