javascriptsearchindexingacrobatacrobat-sdk

How can I create bookmarks with a JS script in Adobe Acrobat?


I have a huge PDF (2000+ pages, here is a sample of the document) that lost it's tags and bookmarks. I cannot contact the original source.

What I'm trying to do is run a JS script on Adobe Acrobat that generates bookmarks to allow easier navigation through the document.

This is what I have tried so far:

// Function to create bookmarks for titles and subtitles
function createBookmarks() {
    const numPages = this.numPages;
    const bookmarks = [];
    let currentTitleBookmark = null;

    for (let pageNum = 0; pageNum < numPages; pageNum++) {
        const pageText = this.getPageNumWords(pageNum);
        let titleMatch, subtitleMatch;

        for (let i = 0; i < pageText.length; i++) {
            const word = pageText[i];
            const text = word.text;

            // Check if the text matches the title pattern (e.g., "1 Title")
            titleMatch = text.match(/^(\d+)\s(.+)$/);
            if (titleMatch) {
                const titleLevel = titleMatch[1];
                const titleName = titleMatch[2];
                const bookmark = this.bookmarkRoot.createChild(titleName);
                bookmarks.push({ level: titleLevel, bookmark });
                currentTitleBookmark = bookmark;
            }

            // Check if the text matches the subtitle pattern (e.g., "1.1 Subtitle")
            subtitleMatch = text.match(/^(\d+\.\d+)\s(.+)$/);
            if (subtitleMatch && currentTitleBookmark) {
                const subtitleName = subtitleMatch[2];
                const subtitleBookmark = currentTitleBookmark.createChild(subtitleName);
                bookmarks.push({ level: subtitleMatch[1], bookmark: subtitleBookmark });
            }
        }
    }

    // Set the initial bookmark to open
    if (bookmarks.length > 0) {
        bookmarks[0].bookmark.dest = pageNum;
    }
}

// Call the function to create bookmarks
createBookmarks();

When I press Ctrl + Enter in the console in line with the last line of code an error shows up saying:

ReferenceError: createBookmarks is not defined 
1:Console:Exec
undefined

What am I missing?


Solution

  • As discussed at the time this LOOKS perfectly easy as a programmable task. Simply Convert the headings and subsections into Bookmarks. Possibly even by some Artificial Intelligence methodology or even basic JS programming.

    enter image description here

    The core problem is not just the bookmarks were lost (in a prior process). But the whole structure of the PDF sample is unworkable, for the task in hand. Before and after fix the content.
    enter image description here

    So in order to fix the PDF sample it was a much bigger iceberg under that tip, where all the text needs correctly encoding before it can be used for interrogation. Once done for one page it will usually be applicable to all pages.

    Even then as a Human Computer I had many problems establishing the TOC ranking as the same font types had been used at multiple levels of interest.

    Answer

    Locate an uncorrupted version, or rebuild the PDF before attempt the Outline.