Copy link to clipboard
Copied
Hi,
This is my first message on the board 🙂
I work with very large PDF files (i.e 2000 pages, 700 bookmarks on 5 or 6 levels!) and I'm looking for a script (Acrobat 9 Pro) allowing to expand/collapse all the bookmarks in a PDF file.
Thanks in advance for your valuable help,
Cheers
Antonella
Copy link to clipboard
Copied
Antonella,
Although I don't know of any single command that collapses all bookmarks, I did find a way to collapse all bookmarks under a single top-level bookmark. Highlight the top-level bookmark and press the "/" key. This will collapse all bookmarks multiple layers deep. Repeat for each top-level bookmark. I'm assuming you don't have 700 top-level bookmarks. If you do, this procedure may not help.
JBrush
Copy link to clipboard
Copied
It's possible to collapse/expand all bookmarks in a file with a script.
PS - this message is from January... I think the OP probably solved it by
now.
Copy link to clipboard
Copied
Just trying to help. So where can I get a copy of this script?
Copy link to clipboard
Copied
The Acrobat Javascript Scripting reference (http://partners.adobe.com/public/developer/en/acrobat/sdk/AcroJS.pdf) describes how to use scripting to access bookmarks and how to open and close them, which is what you're looking for.
I suggest you read the reference and make a function that will do the job for you.
That's the way I would do it.
I don't think you can access the keyboard shortcuts "/" and "Shift + *" via scripting.
Torben.
Copy link to clipboard
Copied
The following works to collapse all bookmarks at all levels in Acrobat 9 Pro:
The numeric-keypad part is significant; using the regular forward-slash key doesn't work.
Copy link to clipboard
Copied
Thanks for the tip. Can't figure out how to get it to work on my Mac (I'll borrow a numeric keypad and see if that helps) or with the Fn numeric keys on my Windows laptop, but it works fine with Windows using an external keyboard.
Copy link to clipboard
Copied
Anybody know how to do this on a Mac laptop?
The only way I've managed to do it is by attaching an external keyboard to my Windows laptop.
Given that this is a basic requirement for professional-quality PDFs, well, actually, no surprise that Adobe doesn't make it easy.
Copy link to clipboard
Copied
It is possible to collapse a given bookmark with JavaScript or to close all levels of bookmarks by "walking" the bookmark tree from the bottom levels to the top level.
// the bookmark open property is not supported by Reader;
if(app.viewerType != "Reader") {
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, bOpen);
} // end for bm.children.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;
if(app.viewerType != "Reader") {
// the bookmark open property is not supported by Reader;
// add menuitem for Open Bookmarks;
app.addMenuItem( { cName: "OpenBookmarks",
cUser: "Open All Bookmarks",
cParent: "View",
cExec: "OpenBookmark(this.bookmarkRoot);",
cEnable: "event.rc = (event.target != null);"
});
// the bookmark open property is not supported by Reader;
// Close Bookmarks;
app.addMenuItem( { cName: "CloseBookmarks",
cUser: "Close All Bookmarks",
cParent: "View",
cExec: "OpenBookmark(this.bookmarkRoot, 'Close');",
cEnable: "event.rc = (event.target != null);"
});
} // end viewer type not reader;
// tool bar buttons;
if(app.viewerType != "Reader") {
// removeToolButton
try{
app.removeToolButton({cName:"BookmarkOpen"});
} catch (e) {
// do nothing;
}
// add a toolbutton
app.addToolButton({
cName: "BookMarkOpen",
cExec: "OpenBookmark(this.bookmarkRoot, 'open');",
cEnable: "event.rc = (event.target != null);",
cTooltext: "Open All Bookmarks"
});
try{
app.removeToolButton({cName:"BookmarkClose"});
} catch (e) {
// do nothing;
}
// add a toolbutton
app.addToolButton({
cName: "BookMarkClose",
cExec: "OpenBookmark(this.bookmarkRoot, 'Close');",
cEnable: "event.rc = (event.target != null);",
cTooltext: "Close All Bookmarks"
});
} // end not reader
function ToggleBookmarks() {
// toggle bookmark navigation pane value
this.pane.toUpperCase() != "B"? this.pane = "B" : this.pane = "";
return true
};
// add menu item
app.addMenuItem({
cName: "ToogleBookMarks",
cUser: "Toggle Bookmarks Pane",
cParent: "View",
cExec: "ToggleBookmarks();",
cEnable: "event.rc = (event.target != null);"
});
try{
app.removeToolButton({cName:"ToggleBookmark"});
} catch (e) {
// do nothing;
}
// add a toolbutton
app.addToolButton({
cName: "ToggleBookmark",
cExec: "ToggleBookmarks();",
cEnable: "event.rc = (event.target != null);",
cTooltext: "Toggle Bookmarks"
});
I'll let you figure out how to add the menu item or toolbar button.
Copy link to clipboard
Copied
Shift-Ctrl-Option-/ But, this only works in Acrobat 9, Acrobat X, XI and DC do not seem to have a way to do this. Acrobat 9 is not supported on any recent version of Mac OS X.
Copy link to clipboard
Copied
For Acrobat/Reader DC the help file shows:
Expand the current bookmark (focus on Bookmarks panel) | Right Arrow or Shift+plus sign | Right Arrow or Shift+plus sign |
Collapse the current bookmark (focus on Bookmarks panel) | Left Arrow or minus sign | Left Arrow or minus sign |
Expand all bookmarks | Shift+* | Shift+* |
Collapse selected bookmark | Forward Slash | Forward Slash |
Center column is for Windows, right column for Mac.
Acrobat Help Shortcuts for the full list. They appear to change with the version.
Copy link to clipboard
Copied
Right, but I'm looking for the command to collapse all bookmarks.
Copy link to clipboard
Copied
I have poste the JavaScript to open/close all the bookmarks as well as the keyboard short cuts. There no single JavaScript command provided by Adobe. The code I provided can open/close all bookmarks
I provided a the single JavaScript command that can open all the bookmarks once the code has been properly installed in the application folder or the PDF.
The following JavaScript command will close all bookmarks:
OpenBookMark(this.BookmardRoot, false);
With some additional coding you can add this to Acrobat as menu item or a toolbar button and that button can be added to the quick bar.
You will still have to install the provided script.
Copy link to clipboard
Copied
A very old thread, but folk have added to it for the last 7 years ...
I wanted to use JavaScript in an Action inside the Action Wizard, Acrobat DC Pro, to collapse all bookmarks in the tree.
I started with a recursive function, similar to the script above, and similar to the example in the JavaScript™ for Acrobat® API Reference.
I then came across a PDF with in excess of 1,500 bookmarks in 5 or 6 layers, and the recursive function crashed Acrobat! I guess this is because the recursive function continually reuses the same variable multiple times, and the JavaScript engine couldn't cope.
The script also sends the name of the bookmark to the console. The console eventually added: "Cannot continue printing to the console." It had captured 1,580 bookmarks at this point.
I used the if () followed by for (), and simply nested this multiple times, and it worked!
David
/*Collapse Bookmarks*/
CollapseBookmarks()
//Function Collapse Bookmarks(CollapseBookmarks)
function CollapseBookmarks()
{
//Not entirely sure how to access docs!!
try {
d = app.activeDocs[0]
d.info.Title //Find out if d has any properties!
Msg = "activeDocs[0]"
} catch(e) {
d = this
Msg = "this"
}
//Now use d as document object...
console.println (Msg)
if (d.bookmarkRoot.children == null) {
// No bookmarks
app.alert({cMsg: "No bookmarks", cTitle: "Bookmarks"})
return
} else {
// Bookmarks
app.alert({cMsg: "Has bookmarks", cTitle: "Bookmarks"})
}
for (i = 0; i < d.bookmarkRoot.children.length; i++){
d.bookmarkRoot.children[i].open = false
console.println(d.bookmarkRoot.children[i].name)
if (d.bookmarkRoot.children[i].children != null){
for (j = 0; j < d.bookmarkRoot.children[i].children.length; j++){
d.bookmarkRoot.children[i].children[j].open = false
console.println(d.bookmarkRoot.children[i].children[j].name)
if (d.bookmarkRoot.children[i].children[j].children != null){
for (k = 0; k < d.bookmarkRoot.children[i].children[j].children.length; k++){
d.bookmarkRoot.children[i].children[j].children[k].open = false
console.println(d.bookmarkRoot.children[i].children[j].children[k].name)
if (d.bookmarkRoot.children[i].children[j].children[k].children != null){
for (l = 0; l < d.bookmarkRoot.children[i].children[j].children[k].children.length; l++){
d.bookmarkRoot.children[i].children[j].children[k].children[l].open = false
console.println(d.bookmarkRoot.children[i].children[j].children[k].children[l].name)
if (d.bookmarkRoot.children[i].children[j].children[k].children[l].children != null){
for (m = 0; m < d.bookmarkRoot.children[i].children[j].children[k].children[l].children.length; m++){
d.bookmarkRoot.children[i].children[j].children[k].children[l].children[m].open = false
console.println(d.bookmarkRoot.children[i].children[j].children[k].children[l].children[m].name)
if (d.bookmarkRoot.children[i].children[j].children[k].children[l].children[m].children != null){
for (n = 0; n < d.bookmarkRoot.children[i].children[j].children[k].children[l].children[m].children.length; n++){
d.bookmarkRoot.children[i].children[j].children[k].children[l].children[m].children[n].open = false
console.println(d.bookmarkRoot.children[i].children[j].children[k].children[l].children[m].children[n].name)
if (d.bookmarkRoot.children[i].children[j].children[k].children[l].children[m].children[n].children != null){
for (o = 0; o < d.bookmarkRoot.children[i].children[j].children[k].children[l].children[m].children[n].children.length; o++){
d.bookmarkRoot.children[i].children[j].children[k].children[l].children[m].children[n].children[o].open = false
console.println(d.bookmarkRoot.children[i].children[j].children[k].children[l].children[m].children[n].children[o].name)
if (d.bookmarkRoot.children[i].children[j].children[k].children[l].children[m].children[n].children[o].children != null){
for (p = 0; p < d.bookmarkRoot.children[i].children[j].children[k].children[l].children[m].children[n].children[o].children.length; p++){
d.bookmarkRoot.children[i].children[j].children[k].children[l].children[m].children[n].children[o].children[p].open = false
console.println(d.bookmarkRoot.children[i].children[j].children[k].children[l].children[m].children[n].children[o].children[p].name)
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
if (d.bookmarkRoot.children.length == 1){
d.bookmarkRoot.children[0].open = true
}
} //end CollapseBookmarks
Copy link to clipboard
Copied
You have filled the console display full of messages to the point it can no longer display any message. The JavaScript engine has detected this condition and realizes it cannot print any more messages so it shuts down on this error. Remove the print messages, print fewer messages, or clear the console before every print.
Copy link to clipboard
Copied
Filling the console with too many messages was not what caused the recursive function to crash. I tried to catch the error, but it crashed anyway.
The long function above works fine, but with 1,580 plus bookmarks, the console filled up. This did not cause the JavaScript engine to crash, it just wrote the polite note: "Cannot continue printing to the console.", and stopped printing to the console. The console had only two other lines before the function started to list all the bookmarks.
I mentioned this to explain why I don't know exactly how many bookmarks there are.
A recursive function is in the bookmark example in the JavaScript™ for Acrobat® API Reference. Folks need to be aware that this could crash if the PDF has a very large nest of bookmarks.
The long way above works, although it does assume there are no more than 8 levels. I suppose it would be possible to introduce a recursive function at level 6, 7 or 8, as it would probably not have much work to do beyond that level. But, it's also quite easy to cut, paste and edit an additional level, if necessary.
David
Copy link to clipboard
Copied
I modified the try .. catch at the start to the following.
I think the use of 'this' to access the PDF is fine for document JavaScripts. However, I don't think it will work with a JavaScript that is part of an Action. The following works with multiple PDFs open at once.
David
// Not entirely sure how to access docs!!
try {
// Front most seems to be last!
d = app.activeDocs[app.activeDocs.length - 1]
d.info.Title //Find out if d has any properties!
Msg = "activeDocs
} catch(e) {
d = this
Msg = "this"
}
// Now use d as document object...
console.println (Msg)
Copy link to clipboard
Copied
This worked for me, at the ROOT bookmark. I did not need to select each top level. Thank you so much for posting - I read the keyboard shortcuts before and assumed it meant the same as others do.
Thank you!!!
Copy link to clipboard
Copied
"I did not need to select each top level."
If you have a root bookmark, that is the only top-level bookmark.
Copy link to clipboard
Copied
Old thread, but adding my reply here. Apparently this thread is pretty high up in Google searches for PDF bookmarks!
For future visitors, I am leaving a useful tip I blogged at http://twtools.wordpress.com/2011/09/26/expand-or-collapse-pdf-bookmarks-and-save-that-state/.
Copy link to clipboard
Copied
Hey, Adobe, it would be nice if you would change the "Collapse all" shortcut or provide a menu command, so I would not have to copy the PDF from my MacBook to my PC laptop and attach a keyboard with a numeric keypad in order to do this one small task.
Copy link to clipboard
Copied
Hey rlauriston. This is a User 2 User forum not Acrobat or Adobe Help Desk. The posters here are individuals providing free assistance. The posted script was developed by a user and not an Adobe employee. Adobe provides a usable product that can have custom features added by users as needed either by using the simple language JavaScript or creating add-ins using a full computer language like C++.
If you want to add a suggestion or improvement idea you need to do this through the Adobe site under the contact page.
Copy link to clipboard
Copied
I know it's a user-to-user forum, but I'm pretty sure Adobe employees monitor it.
That's great that someone wrote a script, but I want Adobe to fix the UI.
Copy link to clipboard
Copied
You should do so here: Feature Request/Bug Report Form
Copy link to clipboard
Copied
I have Adobe 9, maybe I'm misunderstanding the question, but on the bookmark pane, can't you click the "gears" and click on "expand/collapse top level book marks"?