Split Document by Bookmarks
Hello,
I'm trying to create a script that splits a document at each bookmark, including any nested bookmarks, and naming the resulting files after that bookmark name. I've got the name and first page right, but the end page of the document is proving to be an issue for me. My code is as follows:
var bkmN=[];
var bkmS=[];
var bkmE=[];
function getBkmInfo(bkm, nLevel)
{ var s="";
for(var i=1; i<nLevel; i++)
{ s+="-";
}
bkmN.push(s+bkm.name);
// START PAGE ==============
// =========================
bkm.execute();
bkmS.push(this.pageNum);
// END PAGE ================
// =========================
bkmE.push(this.pageNum-1);
// =========================
if (bkm.children != null)
{ for(var i=0; i<bkm.children.length; i++)
{ getBkmInfo(bkm.children, nLevel+1);
}
}
}
getBkmInfo(this.bookmarkRoot, 0);
bkmN.splice(0, 1); // Removes bookmarkRoot info
bkmS.splice(0, 1); // Removes bookmarkRoot info
bkmE.splice(0, 1); // Removes bookmarkRoot info
console.clear();
for(var i=0; i<bkmN.length; i++)
{ console.println((i<10 ? "0"+i : i) + ") " + bkmN +
" | " + (bkmS+1) +
" - " + (bkmE+1)
);
}
this.pageNum=0;
The end page for a document should be the start page of the next bookmark, minus one, but that leaves problems with the last page of the last bookmark. The code I have gives me a correct end page for every file/bookmark except for the first bookmark of each nested section, and the very last bookmark of the entire document (regardless of if it's nested or not).
Any help would be appreciated. I'm a little rusty on my javascript, so I'm sure there is an easy way to do this.
