Skip to main content
subieguy2
Inspiring
October 16, 2018
Answered

Function on a bookmark

  • October 16, 2018
  • 2 replies
  • 1263 views

How can I create these bookmarks into a structure that is Tools > No Color, Black (Ground), 032 (Red) (Battery Power) (Bookmark names under the Parent bookmark Tools). Then I also need to have it add a javascript to the bookmarks (ie   No Color bookmark would run a function noColor();)

I found a tutorial I believe Thom Parker created on creating bookmarks....

but being fairly new to javascript in Acrobat I don't think I really understand what his function is doing.

var checkedOne = 0;

var checkedTwo = 0;

var checkedThree = 0;

var foundLayers = [];

var ocgArray = this.getOCGs();

for (var i = 0; i < ocgArray.length; i++) {

    if (ocgArray.name == "No Color" && checkedOne == "0") {

        foundLayers.push(ocgArray.name);

        checkedOne++;

    }

    if (ocgArray.name == "Black (Ground)" && checkedTwo == "0") {

        foundLayers.push(ocgArray.name);

        checkedTwo++;

    }

    if (ocgArray.name == "032 (Red) (Battery Power)" && checkedThree == "0") {

        foundLayers.push(ocgArray.name);

        checkedThree++;

    }

}

function MakeBkMks(oBkMkParent, aBkMks)

{

   for(var i=0;i<aBkMks.length;i++)

   {

      if(typeof(aBkMks) == "string")

          oBkMkParent.createChild({cName:aBkMks, nIndex:i});

      else

      {// Assume this is a sub Array

         oBkMkParent.createChild({cName:aBkMks[0], nIndex:i});

         MakeBkMks(oBkMkParent.children, aBkMks.slice(1) );

      }

   }

}

// Create the Bookmarks

MakeBkMks(this.bookmarkRoot, foundLayers)

This topic has been closed for replies.
Correct answer George_Johnson

The MakeBkMks function uses recursion, which can be a bit difficult to understand if you haven't used it in programming before.

To add a JavaScript action to a bookmark, you'd use the bookmark.setAction method, as documented here: Acrobat DC SDK Documentation

You didn't mention what the JavaScript should be for each bookmark, but it will probably be best to modify the code that adds the OCG names to the foundLayers array to include the code you want to add to the corresponding bookmark. So instead of:

        foundLayers.push(ocgArray.name); 

You could do:

        foundLayers.push([ocgArray.name, "//JavaScript code as a string goes here;"]);

And get the bookmark name and code text from the array values.

To create the parent bookmark, you'd use the createChild method like that code shows, using the doc.bookmarkRoot as the parent, and then use that as the parent in that last line of the code you posted.

2 replies

George_JohnsonCorrect answer
Inspiring
October 16, 2018

The MakeBkMks function uses recursion, which can be a bit difficult to understand if you haven't used it in programming before.

To add a JavaScript action to a bookmark, you'd use the bookmark.setAction method, as documented here: Acrobat DC SDK Documentation

You didn't mention what the JavaScript should be for each bookmark, but it will probably be best to modify the code that adds the OCG names to the foundLayers array to include the code you want to add to the corresponding bookmark. So instead of:

        foundLayers.push(ocgArray.name); 

You could do:

        foundLayers.push([ocgArray.name, "//JavaScript code as a string goes here;"]);

And get the bookmark name and code text from the array values.

To create the parent bookmark, you'd use the createChild method like that code shows, using the doc.bookmarkRoot as the parent, and then use that as the parent in that last line of the code you posted.

subieguy2
subieguy2Author
Inspiring
October 16, 2018

Thank you so much for the excellent explanation George_Johnson​. That worked exactly how I needed it to. I'm still learning and sincerely appreciate the time others take to share their knowledge on a subject/project I am working on!

Legend
October 16, 2018

This script creates bookmarks based on OCG (known as "layers"). The bookmark only appears if a layer by the same name exists. Does it in your case? If you want them every time you may have chosen a much too complicated model.

subieguy2
subieguy2Author
Inspiring
October 16, 2018

Yes I need it to be based on layers that exist. That is it's intent. I just don't know how to get it to also create a parent bookmark called Tools and then create the bookmarks under that parent. Then also assign a javascript function to the created bookmarks.