How can I create bookmarks for document's parent titles and children?
I'm trying to run a JS script that creates bookmarks for titles and so on.
The script has to know what a parent title and children title is. Right now the only way I've found is specifying what font-size and kind of format (bold) the parent title or children is.
// Get the current document
var doc = app.activeDocs[0];
// Initialize an array to store bookmarks
var bookmarks = [];
// Define a function to add bookmarks recursively
function addBookmarks(page, parentBookmark, level) {
var lastBookmark = null;
for (var i = 0; i < page.length; i++) {
var word = page[i];
if (word.fontSize === 15.99) { // Titles (Font size 15.99)
var title = word.text;
var bookmark = doc.bookmarkRoot.createChild(title, page[0].pageNum);
if (parentBookmark) {
parentBookmark.insertChild(bookmark);
} else {
doc.bookmarkRoot.addChild(bookmark);
}
bookmarks.push(bookmark);
lastBookmark = bookmark;
} else if (word.fontSize === 13.99) { // Subtitles (Font size 13.99)
var subtitle = word.text;
if (lastBookmark) {
var bookmark = doc.bookmarkRoot.createChild(subtitle, page[0].pageNum);
lastBookmark.insertChild(bookmark);
bookmarks.push(bookmark);
}
}
}
}
// Loop through each page in the document
for (var pageNum = 0; pageNum < doc.numPages; pageNum++) {
var page = doc.getPageNumWords(pageNum);
addBookmarks(page, null, 0);
}
// Print the created bookmarks to the console
for (var i = 0; i < bookmarks.length; i++) {
console.println("Bookmark: " + bookmarks[i].name);
}
When I run this in the console no bookmark is created.
I have run out of ideas. If you want to try this code yourself here is the sample of the document I'm working with: sample
