Batch Sequence - Renaming Bookmarks
Copy link to clipboard
Copied
Hi,
Complete JS noob here. Whenever I have an issue I just google scripts and copy and paste them and adjust them accordingly - I have never written one on my own.
So if anyone is feeling nice enough to write an entire script that I can just copy and paste (and tweak) for this, THANK YOU!
So what I want to do is to automate the sequential renaming of bookmarks. I have created a PDF consisting of other PDFs and have used the built-in Acrobat features to automatically create bookmarks. Instead of having to manually rename all these bookmarks, is there a script out there that can do this? I need to make the bookmarks sequential, for example:
TAB 1
TAB 2
TAB 3
TAB 4
The bookmarks are already in the right spots, all I need to do is rename them similarly to the naming convention used in this example.
Copy link to clipboard
Copied
When I have combined PDFs I always get an additional bookmark with the source PDF's name. How do you want to handle this bookmark?
It is possible to remove these bookmarks with JavaScript.
Removing filename bookmarks created by Acrobat​
The script puts all the bookmarks into an array and only adds bookmarks that do not have a file name ending in ".pdf". You could rename the bookmark as part of the process adding the bookmark to the array.
Copy link to clipboard
Copied
This is such a good question. Have you found the answer ?
Copy link to clipboard
Copied
Hi,
It is pretty simple to do this, the documentation pretty much gives you all the code, but here you go
var myBookMarks = this.bookmarkRoot;
for ( var i = 0; i < myBookMarks.children.length; i++)
{
myBookMarks.children[i].name = "Tab" + i;
}
Just add this to a button and you are good to go, you could even make it a application level script but you would need to change line 1 as the this object wouldn't point to a document.
Hope this helps
Malcolm
Copy link to clipboard
Copied
I know this is an old question, but I have a similar issue. I want to strip out parts of all the bookmarks. See the attached file....I want to remove the "22115-Safeway Manning CFS SHOP PLANS-" from every bookmark and just keep the S101, S201, etc.
Copy link to clipboard
Copied
Hi,
similar to above:
var myBookMarks = this.bookmarkRoot;
for ( var i = 0; i < myBookMarks.children.length; i++)
{
var lastDash = myBookMarks.children[i].name.lastIndexOf("-");
myBookMarks.children[i].name = myBookMarks.children[i].name.substring(++lastDash, myBookMarks.children[i].name.length);
}
Run it from a button of the JavaScript console or an action.
To run from the console:
Open the console
Paste the code in
Select all the code
Press the keypad enter
Copy link to clipboard
Copied
So I tried creating a New Action using Acrobat Pro DC. Pasted in the code you gave me under "Execute Javascript" and it didn't do anything. It could be I don't know what I'm doing. Can you give me step-by-step instructions on how to add this?
Copy link to clipboard
Copied
Ok I think I found the problem but I don't know enough about Javascript to know how to fix it. When AutoCAD creates the bookmarks in the PDF, it creates a subheading called "Sheets and Views"
The code doesn't seem to be able to go into child bookmarks. When I create a top level bookmark withouth the "Sheets and Views" subheading, the code works perfectly. How do I get it to go into the subheading bookmark?
Copy link to clipboard
Copied
Change the first line of code to this:
var myBookMarks = this.bookmarkRoot.children[0];
This code change makes the root bookmark for interation to the first of the top level bookmarks.
A more generic solution could be implemented by searching the top level bookmarks for the "Sheets and Views" title.
Use the Acrobat JavaScript Reference early and often
Copy link to clipboard
Copied
That worked perfect! Thanks so much.
Last question...I've put the Action into a button on the toolbar. When I click it, a prompt opens on the right to select files. Is there a way to have the action just run on the current open file or will it always prompt me for the file?
Copy link to clipboard
Copied
How exactly did you put this code into a toolbar button? Did you use the "Action Wizard"?
Use the Acrobat JavaScript Reference early and often
Copy link to clipboard
Copied
Yeah I used the Action Wizard to create a "New Action" then added that action to a toolbar button by Customising the Quick Tools.
Copy link to clipboard
Copied
The "Actions Wizard" is used to create batch processes that apply the same "Action" to several documents. If you want a tool that just operates on the current document then you have two choices.
1) Create an automation tool that adds a toolbar button or menu item to the Acrobat UI
https://www.pdfscripting.com/public/Automating-Acrobat.cfm
2) Put your code in a "Custom Command", which you'll also find on the "Action Wizard" toolbar.
Use the Acrobat JavaScript Reference early and often
Copy link to clipboard
Copied
The Custom Command worked perfectly! Thank you so much for all your help!
Chris
Copy link to clipboard
Copied
Ok I'm back 🙂
The custom command has been working perfectly, but I've noticed one other little quirk in Acrobat. The page labels that you see when you look at the thumbnails don't match the bookmarks. Is there also a way to programmatically have the page labels match the bookmarks that are changed as well?
Copy link to clipboard
Copied
Those are crazy looking page labels. Page labels should be short because they are alwasy shown in a small context.
But anyway, you can get the page label for any page with the "doc.getPageLabel" function.
Here's the reference entry:
https://opensource.adobe.com/dc-acrobat-sdk-docs/library/jsapiref/doc.html#getpagelabel
Use the Acrobat JavaScript Reference early and often
Copy link to clipboard
Copied
Yeah AutoCAD creates those page labels when it produces the PDF.
That reference worked perfect though, the SetPageLabels function example on that webpage was able to remove the long one and replace it with just the page number. So yay!
Thanks again for all the help 🙂
Copy link to clipboard
Copied
Hi Malcolm,
I realize this is an old thread but I'm hoping to get help on a related bookmark issue that I can't find an answer to. Can you help? I'm trying to figure out how to strip a fixed number of leading characters from all bookmarks in a pdf.
For example in the attached screenshot, all of the bookmarks have an identical prefix of "22429_17073172_" that I would like to delete. That is, I want to change the first bookmark from "22429_17073172_11-18-2020_R3.73" to "11-18-2020_R3.73" etc.
Is there a way to delete the first n characters (here, 15 characters) from all the bookmarks?
Alternatively, is there a way to search/replace text in all the bookmarks, e.g., s/r "22429_17073172_" with nothing, to delete the "22429_17073172_" prefix? (I've seen code that will replace all instances of a predefined character with something else, but that doesn't seem to be the answer for this issue.)
Thank you,
-Jay
Copy link to clipboard
Copied
Yes, both things are possible.
// remove the first 15 chars of each (top-level) bookmark name
for (var i=0; i<this.bookmarkRoot.children.length; i++) {
var bkm = this.bookmarkRoot.children[i];
bkm.name = bkm.name.substring(15);
}
// remove a specific prefix from the name of each (top-level) bookmark
for (var i=0; i<this.bookmarkRoot.children.length; i++) {
var bkm = this.bookmarkRoot.children[i];
bkm.name = bkm.name.replace(/^22429_17073172_/, "");
}
Copy link to clipboard
Copied
@try67
This is wonderful, thank you so much! The script to prune off the first 15 characters works as intended, and is just what I was hoping for. You made my day! 🙂
Best,
-Jay
Copy link to clipboard
Copied
By the way, I'll use this to prune USPTO image file wrapper bookmarks - the Patent Office's IFW downloads for patent applications are formatted with a long bookmark string that has all of the significant content at the end, and the leading characters just waste screen real estate.