5 Replies Latest reply on Oct 3, 2015 3:52 PM by Jagmeister123456

    javascript close bookmarks


      Forum, I need help with a javascript code.  I have a 6,000 page document with 1,200 bookmarks that all work great.  I want to identify the bookmarks that contain the word "Document" and close all the children of these "Document" bookmarks (there are about 150 bookmarks that contains the word "Bookmark".  The "Document" bookmarks are on different levels so I cannot go at it that way.  I have this as my javascript code.

      search.matchCase = true;

      search.bookmarks = true;

      search.matchWholeWord = true;

      search.docText = false;



      I am doing my code testing on a much smaller document that has "Part" in a dozen bookmarks.  What I'm asking for should be possible because of the API bookmark object has properties children and open.

      Any help would be appreciated.


        • 1. Re: javascript close bookmarks
          try67 MVP & Adobe Community Professional

          I don't understand what this code has to do with your request... Your code just searches the file. You can't use that to close any bookmarks.

          What you need to do is use a recursive function to iterate over the entire bookmarks tree, checking the name of each bookmark and setting its open property to false if it matches your criteria.

          • 2. Re: javascript close bookmarks
            Jagmeister123456 Level 1

            The code I have identifies all of the bookmarks that contain a certain word.  I am very new to javascript.  I'm probably a 3 out of 10 level.  What is a recursive function?  I will spend some time reading about recursive functions.  Thank you for you reply to my question!

            • 3. Re: javascript close bookmarks
              try67 MVP & Adobe Community Professional

              It might identify them, but you can't do anything with those results, as they're not a part of the code and can't be accessed by it.

              • 4. Re: javascript close bookmarks
                gkaiseril MVP & Adobe Community Professional

                A recursive function is function which calls itself with in itself.


                To manipulate a bookmark, you first need to access the bookmark root and then search the bookmark array for the word in the bookmark name.


                A function to open/close all bookmarks:


                function OpenBookmark(bm, bOpen){
                Purpose: to Open all the bookmarks in a PDF file

                Inputs: bookmark object
                        bookmark open property's logical value default is true
                    Optionally the bookmark open property can be passed as open or close
                    and the funciton will set bOpen parameter tot he correct logical value.

                Returns: logical for sucessful completion of function;

                Notes: This function can be placed in the folder level JavaScript folder and called through the JavaScript (Debugger) console or called from a batch process. It could also be added to a PDF as a document level function and executed on each opening to open all the bookmarks in the PDF.

                // example of usage
                OpenBookMark(this.BookmardRoot, true);

                This function is recursively calls itself to work through the bookmark tree.
                bResult = true; // assumed result for function execution;
                // check value of bOpen parameter exist if not set to defualt of true;
                if(typeof bOpen == "undefined") var bOpen = true;
                if(String(bOpen).toLowerCase().substr(0,1) == "o") bOpen = true;
                if(String(bOpen).toLowerCase().substr(0,1) == "c") bOpen = false;
                if(bOpen != true && bOpen != false) {
                app.alert("bOpen parameter must be either true or flase.\nbOpen = " + bOpen, 0, 0);
                try {
                // set bookmark open property to bOpen value for the current bookmark level;
                bm.open = bOpen;

                // loop through all the levels of the bookmarks ;
                // recursively open the bookmark’s children:
                if (bm.children != null) {

                // loop to the lowest bookmark
                for (var i = 0; i < bm.children.length; i++) {
                OpenBookmark(bm.children[i], bOpen);
                } // end for bm.children[i].length

                } // end if != null
                } catch (e) {
                bResult = false; // set return value for failure;
                app.alert("Open all bookmarks failed.\nBookmark parameter object type: " + typeof bm + "\nbOpem parameter value: " + bOpen, 0, 0);
                } finally {
                return bResult; // return process result;
                } // end try
                } // end OpenBookmark function
                } // end viewer type not reader;

                • 5. Re: javascript close bookmarks
                  Jagmeister123456 Level 1

                  GKaiseril, Thank you so much for the code.  I'm going to copy this code and use it, but I'm also going to try and learn what it does.  I will never be able to give advice like you guys do, but maybe I can learn enough so I don't feel foolish asking questions.  Thanks again.