Skip to main content
Participating Frequently
December 30, 2016
Answered

Duplicating a large number of pages script (special)

  • December 30, 2016
  • 1 reply
  • 3075 views

A fairly common problem for us is having a large file, say ~100 pages, where we have to duplicate each page X number times, so that each duped page is added after the original.

For example: I have to duplicate each page of a 100 page file 5x so that in the end I have a file with pages in this order: 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3...

Normally when you duplicate 100 pages, it puts the pages at the end of the file. And while I know you can OPT-Drag pages to duplicate them where you want (in fact, I have a script with a shortcut to do this), many of our jobs are very large and it's not feasible to dupe 100 pages 5x each... 1 at a time. It's both too time consuming and introduces a large chance you'll make a mistake along the way.

Is there a script out there where you can quickly duplicate a large file in the order above? Or perhaps some trick to achieve the same result?

Thanks!

This topic has been closed for replies.
Correct answer Ronald63

Hi,

You can try this snippet ...

var doc = app.documents[0];

var nbPage = doc.pages.length; // count doc pages

var n = 5; // duplicate number

for (i = 0; i < nbPage; i++) {

    var sourcePage = doc.pages.item(i + (i*n));

    for (j = 0; j < n; j++) {

        sourcePage.duplicate(LocationOptions.AFTER, doc.pages.item(i + (i*n)));

    }

}

1 reply

Ronald63Correct answer
Legend
December 30, 2016

Hi,

You can try this snippet ...

var doc = app.documents[0];

var nbPage = doc.pages.length; // count doc pages

var n = 5; // duplicate number

for (i = 0; i < nbPage; i++) {

    var sourcePage = doc.pages.item(i + (i*n));

    for (j = 0; j < n; j++) {

        sourcePage.duplicate(LocationOptions.AFTER, doc.pages.item(i + (i*n)));

    }

}

TGMikeAuthor
Participating Frequently
December 30, 2016

That's great - thank you!

While that is completely usable as is, is there any way to add a little popup so you can enter the number of duplicates you wanted?

Thanks again!

Legend
December 31, 2016

Like this ?

var n = prompt('Enter a number between 1 and 99.', '', 'Name of dialog');

var reg = /^\d{2}$/;

if (reg.test(n)){

    var doc = app.documents[0]; 

    var nbPage = doc.pages.length; // count doc pages 

     

    for (i = 0; i < nbPage; i++) { 

        var sourcePage = doc.pages.item(i + (i*n)); 

        for (j = 0; j < n; j++) { 

            sourcePage.duplicate(LocationOptions.AFTER, doc.pages.item(i + (i*n))); 

        } 

    } 

}else{

    alert('Error \r Must be a number between 1 to 99');

}