Javascript to split top level bookmarks using bm names for file names
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;
}
}
