Skip to main content
pedrog66606847
Participant
Oct 26, 2023 9:16 PM UTC
Question

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

This topic is closed to new replies. Start a new post to keep the conversation going.

4 replies

Bernd Alheit
Community Expert
Community Expert
Oct 27, 2023 10:02 AM UTC

Read the documentation of the getPageNumWords function.

Translate
try67
Community Expert
Community Expert
Oct 26, 2023 10:33 PM UTC

You have quite a lot of issues there. The getPageNumWords function returns a single number, the number of words on the page. There's no other information included. You're treating it as if it returns the full information of all words on the page. That's not at all the case.

If you want to access individual words you will need to use the getPageNthWord method, and even that doesn't return anything but the word's text.

There's no font size property you can access for individual words, although it is possible to calculate the area (including the height) each word occupies by accessing the quads that define it (using getPageNthWordQuads), but this is not a trivial task.

Translate
pedrog66606847
Participant
Oct 27, 2023 6:54 AM UTC

Okay I think I get it. I also thought that if I told the script what a Title or Subtitle looks like it would be easier. Like this:

 

// 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];

        // Check for titles ("1 WEIGHT")
        if (/^\d+\s[A-Z]+\s*$/.test(word.text)) {
            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;
        }
        // Check for subtitles ("1.1 Structural Limitations")
        else if (/^\d+\.\d+\s[A-Z][a-z]+\s*$/.test(word.text)) {
            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);
}

app.alert("Bookmarks added successfully!");

 Although this does not work either. Any thoughts?

Translate
try67
Community Expert
Community Expert
Oct 27, 2023 10:06 AM UTC

You didn't solve the issues I described. I don't see you using getPageNthWord anywhere in your code, and that's the only way to read the contents of the page. Until you do that, it can't work.

 

Also, for future reference, writing "it doesn't work" is not very helpful. Describe the results you're getting more clearly. Are there any error messages? If not, does it produce any output at all? etc.

Translate