Shawn
Shawn

Reputation: 11391

How can I create a new document out of a subset of another document's pages (in InDesign (CS6) using ExtendScript)?

I need to offer a feature which allows InDesign users to select a page range in an InDesign document and create a new document out of those pages. This sounds simple, but it isn't...

I have tried many different ways of doing this but they have all failed to some degree. Some methods put all pages in a single spread (which sometimes makes InDesign crash). The best I've been able to do (see code below) still has problems at the beginning and the end (see screenshots below):

The original document:

Overview of the original document. The middle is removed (red lines).

The new document:

Overview of the new document. The middle is removed (red lines).

The question: How can I create a new document out of a subset of another document's pages (in InDesign using ExtendScript) without having the problems shown in the screenshots?

note: The behavior of the script is quite different in CS5.5 and CS6. My question concerns CS6.


The second screenshot was obtained by applying the following code to the document shown in the first screenshot:

CODE

var firstPageName = { editContents: "117" };  // This page number is actually entered by the user in an integerEditbox
var lastPageName = { editContents: "136" };  // This page number is actually entered by the user in an integerEditbox
var sourceDocument = app.activeDocument;
var destDocument = app.documents.add();
destDocument.importStyles(ImportFormat.paragraphStylesFormat, new File(sourceDocument.filePath + "/" + sourceDocument.name), GlobalClashResolutionStrategy.LOAD_ALL_WITH_OVERWRITE);
destDocument.importStyles(ImportFormat.characterStylesFormat, new File(sourceDocument.filePath + "/" + sourceDocument.name), GlobalClashResolutionStrategy.LOAD_ALL_WITH_OVERWRITE);
destDocument.viewPreferences.horizontalMeasurementUnits = sourceDocument.viewPreferences.horizontalMeasurementUnits;
destDocument.viewPreferences.verticalMeasurementUnits = sourceDocument.viewPreferences.verticalMeasurementUnits;
destDocument.documentPreferences.facingPages = sourceDocument.documentPreferences.facingPages;
destDocument.documentPreferences.pageHeight = sourceDocument.documentPreferences.pageHeight;
destDocument.documentPreferences.pageWidth = sourceDocument.documentPreferences.pageWidth;
destDocument.documentPreferences.pageSize = sourceDocument.documentPreferences.pageSize;
var sourceSpreads = sourceDocument.spreads;
var nbSourceSpreads = sourceSpreads.length;
var firstPageFound = false;
var lastPageFound = false;
var i;
var newSpreadNeeded;
var currentDestSpread;

for (i = 0; !lastPageFound, i < nbSourceSpreads; ++i) {
  newSpreadNeeded = true;
  var sourcePages = sourceSpreads[i].pages;
  var nbSourcePages = sourcePages.length;
  var j;
  for (j = 0; !lastPageFound, j < nbSourcePages; ++j) {
    if (sourcePages[j].name === firstPageName.editContents) {
      firstPageFound = true;
      destDocument.documentPreferences.startPageNumber = parseInt(firstPageName.editContents);  // We want to preserve page numbers
    }
    if (firstPageFound) {
      // Copy this page over to the new document.
      var firstInNewSpread = false;
      if (newSpreadNeeded) {
        currentDestSpread = destDocument.spreads.add();
        newSpreadNeeded = false;
        firstInNewSpread = true;
      }
      var newPage = sourcePages[j].duplicate(LocationOptions.AT_END, currentDestSpread);
      var k;
      for (k = 0; k < newPage.index; ++k) {
        currentDestSpread.pages[k].remove();
      }
    }
    if (sourcePages[j].name === lastPageName.editContents) {
      lastPageFound = true;
    }
  }
}
destDocument.spreads[0].remove();

Upvotes: 2

Views: 2421

Answers (1)

Josh Voigts
Josh Voigts

Reputation: 4132

I was hacking around and came up with this little script. Although it approaches the problem from the opposite direction, it seems to work fine here. Also, I'm still running in InDesign CS5, but maybe it will work for you. Hopefully I got the gist of your question?

This will extract pages 3 through 5 into a separate document:

var doc = app.activeDocument;

var newFilePath = doc.filePath + "/subset_" + doc.name;
var newFile = File(newFilePath); // Create a new file path

doc.saveACopy(newFile); // Save a copy of the doc
var newDoc = app.open(newFile); // Open the copy

var firstPageNum = 3; // First page number in the range
var lastPageNum = 5;  // Last page number in the range

var firstPage = newDoc.pages[firstPageNum-1];
var lastPage = newDoc.pages[lastPageNum-1];

// Remove all text from the last page in the range to the end of the document
var lastPageFrames = lastPage.textFrames.everyItem().getElements();
for (var i=0; i < lastPageFrames.length; i++) {
   var frame = lastPageFrames[i];
   var parentStory = frame.parentStory;
   var lastFrameInsert = frame.insertionPoints.lastItem();
   var lastStoryInsert = parentStory.insertionPoints.lastItem();
   var textAfter = parentStory.insertionPoints.itemByRange(lastFrameInsert,lastStoryInsert);
   textAfter.remove();
};

// Remove all text from the beginning of the document to the first page in the range
var firstPageFrames = firstPage.textFrames.everyItem().getElements();
for (var i=0; i < firstPageFrames.length; i++) {
   var frame = firstPageFrames[i];
   var parentStory = frame.parentStory;
   var firstFrameInsert = frame.insertionPoints.firstItem();
   var textBefore = parentStory.insertionPoints.itemByRange(0,firstFrameInsert.index);
   textBefore.remove();
};

// Remove the pages that aren't in the range
var allPages = newDoc.pages.everyItem().getElements();
for (var i=0; i < allPages.length; i++) {
   var page = allPages[i];
   if (i < firstPageNum || i > lastPageNum) {
      page.remove();
   }
};

Upvotes: 1

Related Questions