title: How can I create a new document out of a subset of another document's pages (in InDesign (CS6) using ExtendScript)?
body: <p>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...</p>
  
  <p>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):</p>
  
  <p><strong>The original document:</strong></p>
  
  <p><img src="https://i.stack.imgur.com/tiIWO.jpg" alt="Overview of the original document. The middle is removed (red lines)."></p>
  
  <p><strong>The new document:</strong></p>
  
  <p><img src="https://i.stack.imgur.com/t0YgP.jpg" alt="Overview of the new document. The middle is removed (red lines)."></p>
  
  <p><strong>The question:</strong> 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?</p>
  
  <p><strong>note:</strong> The behavior of the script is quite different in CS5.5 and CS6. My question concerns CS6.</p>
  
  <hr>
  
  <p>The second screenshot was obtained by applying the following code to the document shown in the first screenshot:</p>
  
  <p><strong>CODE</strong></p>
  
  <pre><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 &lt; nbSourceSpreads; ++i) {
    newSpreadNeeded = true;
    var sourcePages = sourceSpreads[i].pages;
    var nbSourcePages = sourcePages.length;
    var j;
    for (j = 0; !lastPageFound, j &lt; 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 &lt; newPage.index; ++k) {
          currentDestSpread.pages[k].remove();
        }
      }
      if (sourcePages[j].name === lastPageName.editContents) {
        lastPageFound = true;
      }
    }
  }
  destDocument.spreads[0].remove();
  </code></pre>