Skip to main content
Legend
October 1, 2017
Answered

Javascript to split top level bookmarks using bm names for file names

  • October 1, 2017
  • 2 replies
  • 6962 views

Hello all!

Adobe Acrobat DC pro

Mac os 10.12.6

I want to create a acrobat action to split top level bookmarks using bm names for file names, to my surprise Adobe doesn't have that as an option under the "tools to add" when creating a new action. I was equally surprised to find very little discussion or code postings on the net about this subject.

What I did find doesn't work. I was hoping one of the experts on the forum could take a look at the code below and give some direction on how to get it to work.

Thanks in advance!!

BookmarkCollection bookmarks = document.Bookmarks;

for ( int index=0, fromPage=0; index<bookmarks.Count; index++ )

{

   // determine last page of next part

   int toPage = -1;

   Bookmark bookmark = null;

   if ( index == bookmarks.Count-1 )

   {

      // last bookmark - append all remaining pages

      toPage = document.Pages.Count;

   }

   else

   {

      // not the last bookmark - append up to the next bookmark

      bookmark = bookmarks[index+1];

      GoToAction action = bookmark.Actions[0] as GoToAction;

      if ( null != action )

      {

         InternalDestination destination = action.Destination as

            InternalDestination;

         if ( null != destination )

         {

            toPage = destination.Page.Index;

         }

      }

   }

   // create a new part

   if ( -1 != toPage && null != bookmark )

   {

      Document part = new Document();

      for ( int pageIndex=fromPage; pageIndex<toPage; pageIndex++ )

      {

         part.Pages.Add( document.Pages[pageIndex].Clone() );

      }

      using ( FileStream fileOut = new FileStream(

         bookmark.Title + ".pdf", FileMode.Create, FileAccess.Write ) )

      {

         part.Write( fileOut );

      }

      fromPage = toPage;

   }

}

This topic has been closed for replies.
Correct answer Thom Parker

Hello Thom!

Thank you for explaining about the assumptions and what to consider.............

In this case the bookmarks are intended only to be used for Identifying the first page in a rang of pages and for the spilt/extracted file naming.

The code you provided is splitting the pages correctly but the naming of the extracted files are not of their respective Bookmark names but of the file name and base numbering.

I've tried a few attempts to modify the code with no good results.

Thanks again for any help!!!!!

oDoc = this;

bkTrgt = []

this.bookmarkRoot.children.forEach(function(a){a.execute();bkTrgt.push(pageNum);})

bkTrgt.forEach(function(a,i,k){oDoc.extractPages(k,((i+1)<k.length)?k[i+1]-1:oDoc.numPages-1,oDoc.documentFileName.replace(/\./,i+"."));})


Opps , missed that part, well here is an update,

bkTrgt.forEach(function(a,i,k){oDoc.extractPages(k,((i+1)<k.length)?k[i+1]-1:oDoc.numPages-1,oDoc.bookmarkRoot.children.name + ".pdf"})

The bookmarks children have exactly the same indexes as the page numbers in the array, so I just changed the code the get the save file name from the bookmark name at the index that is being operated on..

2 replies

Joel Geraci
Community Expert
Community Expert
October 1, 2017

The built-in Split function will split files based on top level bookmarks and can be run against multiple files as though it were being initiated from an Action. Unless there is something you're leaving out of your question, there's no need to create an Action to do this.

Mike BroAuthor
Legend
October 1, 2017

Thanks for the reply Joel

Your'e correct as the Acrobat split function can be run against multiple files, I am working on creating an action that Creates Bookmarks-From Tags then I am running js to make them top level bookmarks, I just wanted to add the split function to make it fully automated.

Thanks

Joel Geraci
Community Expert
Community Expert
October 1, 2017

You may be over complicating this. What software is adding the tags?

try67
Community Expert
Community Expert
October 1, 2017

Any specific reason not to use the built-in command that does that? It's under the Split Document function.

try67
Community Expert
Community Expert
October 1, 2017

PS. The code above was not written for Acrobat.

PPS. I've developed a script (a long time ago) that does it, if you're interested: Custom-made Adobe Scripts: Acrobat -- Extract Chapters by Bookmarks

Mike BroAuthor
Legend
October 1, 2017

Hello,

I wanted to create a acrobat action to split top level bookmarks so I could point to a folder location full of bookmarked pdfs, so I wouldn't have to physically open each file and go through the process: Organize Pages/Split/Split by: Top level bookmarks/Output Options/Use bookmark names for file names.

Thanks......