Change bookmarks value with javascript

I have many PDF files with bookmarks value with this sequence:
File1.pdf
302_bookmark1
303_bookmark2
304_bookmark3
File2.pdf
324_bookmark1
326_bookmark2
328_bookmark3
330_bookmark4
File3.pdf
543_bookmark1
544_bookmark2
567_bookmark3
568_bookmark4
...
I would change the bookmarks value, only numeric value, with a counter that resets to 1 for each file.
Thanks!


Pasquale Vairo


15 Answers

Voted Best Answer

Create an Action that executes this JavaScript code and run it on your files:

for (var i=0; i<this.bookmarkRoot.children.length; i++) {
this.bookmarkRoot.children[i].name = (i+1);
}

-------------------------------------
Visit my custom-made PDF scripts website: http://try67.blogspot.com
Contact me personally: try6767@gmail.com


By Gilad D (try67)   

Do you have the Acrobat SDK with the JavaScript API documentation? The "name" property of a bookmark object can be changed via JavaScript:

http://livedocs.adobe.com/acrobat_sdk/10/Acrobat10_HTMLHelp/JS_API_AcroJS.88.21
0.html

You need to iterate over all bookmarks and then set the name approprately. The documentation for the "children" property has sample code that shows how to iterate over all bookmarks:

http://livedocs.adobe.com/acrobat_sdk/10/Acrobat10_HTMLHelp/JS_API_AcroJS.88.20
7.html


Karl Heinz Kremer

PDF Acrobatics Without a Net

PDF Software Development, Training and More...

http://www.khkonsulting.com


Karl Heinz Kremer   

Thanks Karl Heinz!

I absolutly need the Acrobat SDK or I can insert the java code into the JavaScript Editor integrated in the Acrobat X Pro?


Pasquale Vairo   

The Acrobat SDK is mainly the documentation you need to work with Acrobat, plus sample programs. You can then either insert the JavaScript program into your PDF using Acobat, or (as you will probably do in this case) write a folder level script that would add a menu item or a tool in the Tools panel to in any text editor and save it in one of Acrobat's JavaScript directories.

You can also execute JavaScript in the JS console, but again, the documentation in the SDK will talk about all this. You can find the SDK here: http://www.adobe.com/devnet/acrobat.html


Karl Heinz Kremer

PDF Acrobatics Without a Net

PDF Software Development, Training and More...

http://www.khkonsulting.com


Karl Heinz Kremer   

Do you mean that you want all the bookmarks in your files to be:
1
2
3
4
etc.
?

Do you only have a single level of bookmarks, or are there nested ones as well?
Which version of Acrobat do you have?

-------------------------------------
Visit my custom-made PDF scripts website: http://try67.blogspot.com
Contact me personally: try6767@gmail.com


Gilad D (try67)   

Yes, I want this sequence

1

2

3

4

...

for each file.

For each file, an autonumber that starts by 1 and increases the value of 1 for each barcode.


Pasquale Vairo   

Do you have Acrobat Pro?


Gilad D (try67)   

I have only one level of bookmarks.

I have Actobat X Pro.


Pasquale Vairo   

your code is good!
But I want to keep the text after the number value:

1_bookmark1
2_bookmark2
3_bookmark3
4_bookmark4

thanks again fo your help!


Pasquale Vairo   

That's not what you said why I asked you if you wanted it to be just 1,2,3, etc.
You can read up on the various String methods in JS to see how you can achieve that. The split() method will be useful in this situation.


Gilad D (try67)   

excuse me...
Can you say me the JavaScript code for to keep the bookmarks text?


Pasquale Vairo   

Use the following script in your action instead of the first one. It uses a regular expression to extract the number and the remainder after the number.

for (var i=0; i<this.bookmarkRoot.children.length; i++)
{
var theName = this.bookmarkRoot.children[i].name;
// get the remainder after the numeric part at the beginning of the bookmark
var pattern = /(\d+)(_.+)/;
var match = pattern.exec(theName);
if (match.length >= 2)
this.bookmarkRoot.children[i].name = String(i+1) + match[2];
}


Karl Heinz Kremer

PDF Acrobatics Without a Net

PDF Software Development, Training and More...

http://www.khkonsulting.com


Karl Heinz Kremer   

The autonumber not works...


Pasquale Vairo   

Then something on your system is different than on mine. What exactly do you mean by it does not work?


Karl Heinz Kremer   

Excuse me Karl Heinz, your code is perfect!!!

I use the separator - instead of _

Excuse me again...

Thanks a lot, Karl Heinz and Gilad D!


Pasquale Vairo   


Please specify a reason: