Copy link to clipboard
Copied
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.
The index for the bookmark end array is always one behind, because it is set at the same as the start. So index (1) is really the end for the start 0 (after removal of the root). Also, the last page of the document is the end page for the last bookmark. The solution is to remove the first entry of the end array and push the last page number on to the end of the end array.
Copy link to clipboard
Copied
The index for the bookmark end array is always one behind, because it is set at the same as the start. So index (1) is really the end for the start 0 (after removal of the root). Also, the last page of the document is the end page for the last bookmark. The solution is to remove the first entry of the end array and push the last page number on to the end of the end array.