5 Replies Latest reply on May 9, 2013 11:46 PM by HeulwenM

    How to delete unused bookmarks from a PDF?

    HeulwenM Level 1

      I am trying to find a way to automatically delete bookmarks from a PDF if they attached "named destination" no longer exists in the document.

       

      I thought I could loop through the bookmarks, execute them, and of they threw an error then the bookmark could be deleted but this didn't work ie. no exception was thrown. Here is my code:

       

      var myBookmarks = this.bookmarkRoot.children;

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

      try

      {

      myBookmarks[i] = myBookmarks[i].execute();

      }

      catch (myError)

      {

      myBookmarks[i] = myBookmarks[i].remove();

      }

       

       

      Does anyone have any ideas how to do this??? Many thanks. HJ

        • 1. Re: How to delete unused bookmarks from a PDF?
          George_Johnson MVP & Adobe Community Professional

          At the beginning of the loop, you could set the current view to something you know is not the destination of a bookmark (e.g., page = 0, zoom = 1234), execute the bookmark, and if the view is different, that would be an indication that the bookmark is valid.

          • 2. Re: How to delete unused bookmarks from a PDF?
            try67 MVP & Adobe Community Professional

            Of course, you'll have to reset the view to that initial point at the top

            of each iteration, and hope that there's no bookmark pointing to that exact

            location...

            1 person found this helpful
            • 3. Re: How to delete unused bookmarks from a PDF?
              HeulwenM Level 1

              Thanks George and Gilad,

               

              Good idea! I'll try this out and let you know how I go.

               

              HJ

              • 4. Re: How to delete unused bookmarks from a PDF?
                HeulwenM Level 1

                This solved my problem - thanks George.

                 

                I just have to solve a problem where there are a couple of links that are set to 'inherit zoom' so there zoom doesn't change even though the bookmark does work (so they get deleted when they shouldn't). Otherwise the script is working well. Thanks. HJ

                 

                PS. Script included below

                 

                 

                 

                var myBookmarks = this.bookmarkRoot.children

                var myDeletedBookmarks = 0

                if(myBookmarks != null)

                {

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

                {

                var mySingleBookmark=myBookmarks[i];

                 

                 

                var myBookmarks2 = mySingleBookmark.children

                if(myBookmarks2 != null)

                {

                for (var a=0; a<myBookmarks2.length; a++)

                {

                var mySingleBookmark2=myBookmarks2[a];

                 

                 

                var myBookmarks3 = mySingleBookmark2.children

                if(myBookmarks3 != null)

                {

                for (var e=0; e<myBookmarks3.length; e++)

                {

                var mySingleBookmark3=myBookmarks3[e];

                this.zoom=15;

                myBookmarks3[e]=myBookmarks3[e].execute();

                var myZoom=this.zoom

                if(myZoom<16)

                {

                var myDeletedBookmarks = myDeletedBookmarks+1

                mySingleBookmark3.remove();

                }

                }

                }

                //Finish actions on level 2 bookmarks

                this.zoom=15;

                myBookmarks2[a]=myBookmarks2[a].execute();

                var myZoom=this.zoom

                if(myZoom<16)

                {

                var myDeletedBookmarks = myDeletedBookmarks+1

                mySingleBookmark2.remove();

                }

                }

                }

                 

                 

                //Finish actions on level 1 bookmarks

                this.zoom=15;

                myBookmarks[i]=myBookmarks[i].execute();

                var myZoom=this.zoom

                if(myZoom<16)

                {

                var myDeletedBookmarks = myDeletedBookmarks+1

                mySingleBookmark.remove();

                }

                }

                }

                app.alert (myDeletedBookmarks+" bookmarks have been deleted");

                • 5. Re: How to delete unused bookmarks from a PDF?
                  HeulwenM Level 1

                  I should say I have written the script to go up to 3 levels of bookmarks. There's probably simplier way to do this but it seems to work ok : ) HJ