Copy link to clipboard
Copied
Hi Folks,
The issue is simple if we try to delete lots of pages from a big document say 2000 pages long, the process is very slow. A simple code snippet like the following takes ages to complete. For my testing I used a blank document with 2000 pages, I am sure for a fully composed production document the time will be more
for (i=0; i<50; i++)
app.documents[0].pages[i].remove();
Here I am trying to delete the pages in sequence but a practical scenario would be deleting specific pages. This issue was reported in response to another query posted on the forum for which I posted a potential code snippet
I tried different things like
Any ideas on how to speed this up? If we try to delete these pages using the pages panel it deletes much quicker so this has something to do with the communication of DOM with the InDesign objects in my opinion. Any insights would be really helpful
-Manan
Copy link to clipboard
Copied
I would be defining pages as their own variable with getElements and iterating backward if we are doing removal ie:
var allPages = app.documents[0].pages.everyItem().getElements();
for (var i = 50; i >= 0; i--) {
allPages[i].remove();
}
This limits DOM calls and prevents potential indexing errors. Have you tried those ideas?
Copy link to clipboard
Copied
Yeah @brianp311 i tried these ideas but unfortunately its still the same.
-Manan
Copy link to clipboard
Copied
You're trying to remove the first page, which isn't possible. Does that have anything to do with it?
Another approach might be this:
app.documents[0].pages.itemByRange(1,-1).remove();
But I found that deleting pages manually in the Pages panel is slow in big documents as well.
P.
Copy link to clipboard
Copied
Hi @Peter Kahrel,
It's still the same. Deleting from the UI does take a bit of extra time but the script is taking minutes to do this operation with the computer fans getting cranked up as well.
-Manan
Copy link to clipboard
Copied
Hi Manan,
the only way where you can make it instantly is when you have to remove pages at the end of a document.
Then you simply can assign a new value for pagesPerDocument in the documentPreferences.
var removeAtEnd = 10;
var doc = app.documents[0];
var numPages = doc.documentPreferences.pagesPerDocument;
var newPageLength = numPages - removeAtEnd ;
if( newPageLength > 0 )
{
doc.documentPreferences.pagesPerDocument =
newPageLength ;
};
Using itemByRange() remains very slow.
Just tested this with an empty document with 2000 pages where I removed pages 1-10.
Regards,
Uwe Laubender
( ACP )
Copy link to clipboard
Copied
Hi @Laubender,
I was also pinning my hopes on pagesPerDocument, for that I thought exactly like you and tried using the move method to bring all the deletion candidates at the end. But, in such a case the move method becomes the bottleneck. I think we can now conclude there is no faster method using the remove method. I thought of one alternative solution, could be a lot of work though.
The other option is to go with a C++ plugin that should be pretty fast.
-Manan
Copy link to clipboard
Copied
Is duplicate/remove equally slow? Not around a computer this weekend to test.