OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

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

  • Thread starter Thread starter Peter La Anguila
  • Start date Start date
P

Peter La Anguila

Guest
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:


Code:
// 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:

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

What am I missing?

<p>I have a <strong>huge PDF</strong> (2000+ pages, here is <a href="https://drive.google.com/file/d/14BBXv7FkxHW_0xgccHzik4j9D3RJuj0X/view?usp=sharing" rel="nofollow noreferrer">a sample of the document</a>) <strong>that lost it's tags and bookmarks</strong>. I <strong>cannot contact</strong> the original <strong>source</strong>.</p>
<p>What I'm trying to do is <strong>run a JS script on Adobe Acrobat</strong> that <strong>generates bookmarks</strong> to allow <strong>easier navigation</strong> through the document.</p>
<p>This is what I have tried so far:</p>
<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-js lang-js prettyprint-override"><code>// 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;
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();</code></pre>
</div>
</div>
</p>
<p>When I press <code>Ctrl + Enter</code> in the console <strong>in line with the last line of code</strong> an error shows up saying:</p>
<pre><code>ReferenceError: createBookmarks is not defined
1:Console:Exec
undefined
</code></pre>
<p>What am I missing?</p>
 

Online statistics

Members online
0
Guests online
4
Total visitors
4
Top