Question: (Javascript?) script for adding page number to end of bookmark names
Moderators: Daniel - PDF-XChange, PDF-XChange Support, Vasyl - PDF-XChange, Chris - PDF-XChange, Sean - PDF-XChange, Paul - PDF-XChange, Ivan - Tracker Software, Stefan - PDF-XChange
-
- User
- Posts: 1372
- Joined: Mon Nov 15, 2021 8:38 pm
Question: (Javascript?) script for adding page number to end of bookmark names
viewtopic.php?p=166066&hilit=bookmark+p ... er#p166066
as the title says, in reference to the post link above
Maybe anyone here would have a script for adding page number to end of bookmark names to run in the Javascript module of PDF xce? While we're waiting for optional page numbers in the bookmark titles
something that would maybe be able to extract the page number in the action list here on 100+ bookmarks and change the name of the bookmarks accordingly. Either adding p1 (for page 1) to the beginning or end of the bookmark names
as the title says, in reference to the post link above
Maybe anyone here would have a script for adding page number to end of bookmark names to run in the Javascript module of PDF xce? While we're waiting for optional page numbers in the bookmark titles
something that would maybe be able to extract the page number in the action list here on 100+ bookmarks and change the name of the bookmarks accordingly. Either adding p1 (for page 1) to the beginning or end of the bookmark names
My wishlist https://forum.pdf-xchange.com/viewtopic.php?p=187394#p187394
Disable SPACE page navigation, fix kb shortcut for highlighting advanced search tool search field, bookmarks with numbers, toolbar small icon size, AltGr/Ctrl+Alt keyboard issues
Disable SPACE page navigation, fix kb shortcut for highlighting advanced search tool search field, bookmarks with numbers, toolbar small icon size, AltGr/Ctrl+Alt keyboard issues
-
- User
- Posts: 1372
- Joined: Mon Nov 15, 2021 8:38 pm
Re: Question: (Javascript?) script for adding page number to end of bookmark names
https://groups.google.com/g/adobe.acrobat.windows/c/yPV7CxtlRig?pli=1
I found this discussion on a javascript extracting bookmarks pages, but this is for adobe acrobat, not PDF XCE
I found this discussion on a javascript extracting bookmarks pages, but this is for adobe acrobat, not PDF XCE
My wishlist https://forum.pdf-xchange.com/viewtopic.php?p=187394#p187394
Disable SPACE page navigation, fix kb shortcut for highlighting advanced search tool search field, bookmarks with numbers, toolbar small icon size, AltGr/Ctrl+Alt keyboard issues
Disable SPACE page navigation, fix kb shortcut for highlighting advanced search tool search field, bookmarks with numbers, toolbar small icon size, AltGr/Ctrl+Alt keyboard issues
- Stefan - PDF-XChange
- Site Admin
- Posts: 19806
- Joined: Mon Jan 12, 2009 8:07 am
- Contact:
Re: Question: (Javascript?) script for adding page number to end of bookmark names
Hello MedBooster,
Those scripts will likely work in our Editor as well.
However those will output a list of the bookmarks to the command line of the JS console. They will not rename the bookmarks for you in the bookmarks pane.
Kind regards,
Stefan
Those scripts will likely work in our Editor as well.
However those will output a list of the bookmarks to the command line of the JS console. They will not rename the bookmarks for you in the bookmarks pane.
Kind regards,
Stefan
-
- User
- Posts: 1372
- Joined: Mon Nov 15, 2021 8:38 pm
Re: Question: (Javascript?) script for adding page number to end of bookmark names
Edit: I got this "error" when running the script, but it does actually give the page number of bookmarks it seems. So I guess what's left is just to make it appear at the end of bookmark names.Tracker Supp-Stefan wrote: ↑Mon Jan 16, 2023 1:13 pm Hello MedBooster,
Those scripts will likely work in our Editor as well.
However those will output a list of the bookmarks to the command line of the JS console. They will not rename the bookmarks for you in the bookmarks pane.
Kind regards,
Stefan
Anyway, I think I'll leave this up to other coding geniuses on this forum. Just an idea, since page numbers in bookmarks is a thing many want. The only disadvantage to this method is that it will be difficult to change the page numbers again if you add some pages in between existing ones. So making it an intrinsic part of the bookmarks pane would still be optimal
My wishlist https://forum.pdf-xchange.com/viewtopic.php?p=187394#p187394
Disable SPACE page navigation, fix kb shortcut for highlighting advanced search tool search field, bookmarks with numbers, toolbar small icon size, AltGr/Ctrl+Alt keyboard issues
Disable SPACE page navigation, fix kb shortcut for highlighting advanced search tool search field, bookmarks with numbers, toolbar small icon size, AltGr/Ctrl+Alt keyboard issues
- Stefan - PDF-XChange
- Site Admin
- Posts: 19806
- Joined: Mon Jan 12, 2009 8:07 am
- Contact:
Re: Question: (Javascript?) script for adding page number to end of bookmark names
Hello MedBooster,
That is not an error. The code uses "App.alert" - which throws a pop-up message with the information.
So the script actually works exactly as it should - it's just an example that is quite tedious for a long file with a lot of bookmarks!
(I tested with a 310 page file ... so I had to do A LOT of clicks to go through that script - and they could have used Console.println as the other one
)
Kind regards,
Stefan
That is not an error. The code uses "App.alert" - which throws a pop-up message with the information.
So the script actually works exactly as it should - it's just an example that is quite tedious for a long file with a lot of bookmarks!
(I tested with a 310 page file ... so I had to do A LOT of clicks to go through that script - and they could have used Console.println as the other one

Kind regards,
Stefan
-
- User
- Posts: 1372
- Joined: Mon Nov 15, 2021 8:38 pm
Re: Question: (Javascript?) script for adding page number to end of bookmark names
Tracker Supp-Stefan wrote: ↑Mon Jan 16, 2023 1:24 pm Hello MedBooster,
That is not an error. The code uses "App.alert" - which throws a pop-up message with the information.
So the script actually works exactly as it should - it's just an example that is quite tedious for a long file with a lot of bookmarks!
(I tested with a 310 page file ... so I had to do A LOT of clicks to go through that script - and they could have used Console.println as the other one)
Kind regards,
Stefan
Okay, thanks for the input.

Hopefully this helps either me or someone else so that we could get a working script for renaming sooner or later....
My wishlist https://forum.pdf-xchange.com/viewtopic.php?p=187394#p187394
Disable SPACE page navigation, fix kb shortcut for highlighting advanced search tool search field, bookmarks with numbers, toolbar small icon size, AltGr/Ctrl+Alt keyboard issues
Disable SPACE page navigation, fix kb shortcut for highlighting advanced search tool search field, bookmarks with numbers, toolbar small icon size, AltGr/Ctrl+Alt keyboard issues
- Stefan - PDF-XChange
- Site Admin
- Posts: 19806
- Joined: Mon Jan 12, 2009 8:07 am
- Contact:
Question: (Javascript?) script for adding page number to end of bookmark names
Hi Med Booster,
I've taken a look at what is available as JS methods - and there aren't that many (no "Rename): And while you can remove a bookmark and create a new one - there is also no method to copy all the actions from an existing bookmark (so I can't e.g. read the current one, destroy it, and create a new one in it's place with the desired new name - as I can't set it up with the correct actions).
I've taken a look at what is available as JS methods - and there aren't that many (no "Rename): And while you can remove a bookmark and create a new one - there is also no method to copy all the actions from an existing bookmark (so I can't e.g. read the current one, destroy it, and create a new one in it's place with the desired new name - as I can't set it up with the correct actions).
Re: Question: (Javascript?) script for adding page number to end of bookmark names
It's a bit hacky, but you can use the bookmark action to find the page number. It will get tripped up if an action does something besides going to a page. In the script below change DELIM for a different string to put before the page number, and MAXDEPTH to the depth into sub-bookmarks to go.
Code: Select all
// script to add page number to bookmark names
// Note that the page number is static (ie if a page is added or deleted,
// the page number on the bookmark is just text and will be unchanged)
{
// this is the character that goes between the name and the page number
const DELIM = " p.";
// change to the number deep you want to add page numbers
const MAXDEPTH = 1;
function getPage(bkm) {
// does bookmark action and returns the page
bkm.execute();
return bkm.doc.pageNum;
}
function addPgnum(bkm, depth) {
// step through children
if (depth < MAXDEPTH && bkm.children) {
for (let b of bkm.children) {
addPgnum(b,depth+1);
}
}
// rename this bookmark
bkm.name += DELIM + getPage(bkm);
}
// save the viewstate because using action to find the page number
let vState = this.viewState;
// step through bookmarks
addPgnum(this.bookmarkRoot, 0);
// restore viewstate
this.viewState = vState;
}
- Daniel - PDF-XChange
- Site Admin
- Posts: 10910
- Joined: Wed Jan 03, 2018 6:52 pm
Re: Question: (Javascript?) script for adding page number to end of bookmark names
Hello, Mathew
Thank you very much for this, it looks like it will be plenty helpful for someone.
Kind regards,
Thank you very much for this, it looks like it will be plenty helpful for someone.
Kind regards,
Dan McIntyre - Support Technician
PDF-XChange Co. LTD
+++++++++++++++++++++++++++++++++++
Our Web site domain and email address has changed as of 26/10/2023.
https://www.pdf-xchange.com
Support@pdf-xchange.com
PDF-XChange Co. LTD
+++++++++++++++++++++++++++++++++++
Our Web site domain and email address has changed as of 26/10/2023.
https://www.pdf-xchange.com
Support@pdf-xchange.com
-
- User
- Posts: 1372
- Joined: Mon Nov 15, 2021 8:38 pm
Re: Question: (Javascript?) script for adding page number to end of bookmark names
I just realized... it's probably smarter to add the numbers to the beginning of bookmarks... that way you could maybe sort the bookmarks alphabetically? To make it at least resemble this bookmark number functionality we've discussed previously:
viewtopic.php?t=37539&hilit=page+number
2 more days till 2024
viewtopic.php?t=37539&hilit=page+number
2 more days till 2024

My wishlist https://forum.pdf-xchange.com/viewtopic.php?p=187394#p187394
Disable SPACE page navigation, fix kb shortcut for highlighting advanced search tool search field, bookmarks with numbers, toolbar small icon size, AltGr/Ctrl+Alt keyboard issues
Disable SPACE page navigation, fix kb shortcut for highlighting advanced search tool search field, bookmarks with numbers, toolbar small icon size, AltGr/Ctrl+Alt keyboard issues
- Stefan - PDF-XChange
- Site Admin
- Posts: 19806
- Joined: Mon Jan 12, 2009 8:07 am
- Contact:
Re: Question: (Javascript?) script for adding page number to end of bookmark names
Hello MedBooster,
Second day of 2024 already!
You can do that numbering at the front of the bookmark names indeed if it would be easier for you later on in your process!
Kind regards,
Stefan
Second day of 2024 already!

You can do that numbering at the front of the bookmark names indeed if it would be easier for you later on in your process!

Kind regards,
Stefan
-
- User
- Posts: 1372
- Joined: Mon Nov 15, 2021 8:38 pm
Re: Question: (Javascript?) script for adding page number to end of bookmark names
Mathew wrote: ↑Thu Dec 14, 2023 9:03 pm It's a bit hacky, but you can use the bookmark action to find the page number. It will get tripped up if an action does something besides going to a page. In the script below change DELIM for a different string to put before the page number, and MAXDEPTH to the depth into sub-bookmarks to go.
Code: Select all
// script to add page number to bookmark names // Note that the page number is static (ie if a page is added or deleted, // the page number on the bookmark is just text and will be unchanged) { // this is the character that goes between the name and the page number const DELIM = " p."; // change to the number deep you want to add page numbers const MAXDEPTH = 1; function getPage(bkm) { // does bookmark action and returns the page bkm.execute(); return bkm.doc.pageNum; } function addPgnum(bkm, depth) { // step through children if (depth < MAXDEPTH && bkm.children) { for (let b of bkm.children) { addPgnum(b,depth+1); } } // rename this bookmark bkm.name += DELIM + getPage(bkm); } // save the viewstate because using action to find the page number let vState = this.viewState; // step through bookmarks addPgnum(this.bookmarkRoot, 0); // restore viewstate this.viewState = vState; }
Doesn't seem to be working for me... Are you not supposed to paste it into the Javascript console?
My wishlist https://forum.pdf-xchange.com/viewtopic.php?p=187394#p187394
Disable SPACE page navigation, fix kb shortcut for highlighting advanced search tool search field, bookmarks with numbers, toolbar small icon size, AltGr/Ctrl+Alt keyboard issues
Disable SPACE page navigation, fix kb shortcut for highlighting advanced search tool search field, bookmarks with numbers, toolbar small icon size, AltGr/Ctrl+Alt keyboard issues
- Stefan - PDF-XChange
- Site Admin
- Posts: 19806
- Joined: Mon Jan 12, 2009 8:07 am
- Contact:
Re: Question: (Javascript?) script for adding page number to end of bookmark names
Hello MedBooster,
Definitely worked for me: Yes - all you need to do is copy-paste the JS in the JS console, and then run the script.
It is only adding the page numbers to the top level bookmarks. If you need a different level numbered - please adjust the script where indicated.
Kind regards,
Stefan
Definitely worked for me: Yes - all you need to do is copy-paste the JS in the JS console, and then run the script.
It is only adding the page numbers to the top level bookmarks. If you need a different level numbered - please adjust the script where indicated.
Kind regards,
Stefan
Re: Question: (Javascript?) script for adding page number to end of bookmark names
I'm not sure why I didn't notice this before, but there's a much better tool to do this already built-in...
Bookmarks > Add Text to Bookmark Title

Bookmarks > Add Text to Bookmark Title
- Stefan - PDF-XChange
- Site Admin
- Posts: 19806
- Joined: Mon Jan 12, 2009 8:07 am
- Contact:
Re: Question: (Javascript?) script for adding page number to end of bookmark names
Hello Mathew,
You and me both!
And I just tested the script when MedBooster said it does not work for them - and your script also works!
Kind regards,
Stefan
You and me both!
And I just tested the script when MedBooster said it does not work for them - and your script also works!

Kind regards,
Stefan
-
- User
- Posts: 1372
- Joined: Mon Nov 15, 2021 8:38 pm
Re: Question: (Javascript?) script for adding page number to end of bookmark names
Thank you for your efforts Mathew. That's great news... this will probably also be more reliable if any bookmarks do anything more than referring to a page, or "destination"/position
Stilllll.... I personally at least would still prefer an option to see automatically generated page numbers....
You see... including the page number in the bookmark name is suddenly much more of a hassle if you add and remove pages, or want to add more bookmarks with page numbers at a later date....
Nevertheless, this is useful to do with textbooks to better estimate how many pages you have left to read etc....
Stilllll.... I personally at least would still prefer an option to see automatically generated page numbers....
You see... including the page number in the bookmark name is suddenly much more of a hassle if you add and remove pages, or want to add more bookmarks with page numbers at a later date....
Nevertheless, this is useful to do with textbooks to better estimate how many pages you have left to read etc....
My wishlist https://forum.pdf-xchange.com/viewtopic.php?p=187394#p187394
Disable SPACE page navigation, fix kb shortcut for highlighting advanced search tool search field, bookmarks with numbers, toolbar small icon size, AltGr/Ctrl+Alt keyboard issues
Disable SPACE page navigation, fix kb shortcut for highlighting advanced search tool search field, bookmarks with numbers, toolbar small icon size, AltGr/Ctrl+Alt keyboard issues
- Stefan - PDF-XChange
- Site Admin
- Posts: 19806
- Joined: Mon Jan 12, 2009 8:07 am
- Contact:
Re: Question: (Javascript?) script for adding page number to end of bookmark names
Hello MedBooster,
Thanks for your comments and feedback - for the moment that is what we (and JS) can offer!
Kind regards,
Stefan
Thanks for your comments and feedback - for the moment that is what we (and JS) can offer!
Kind regards,
Stefan
-
- User
- Posts: 1372
- Joined: Mon Nov 15, 2021 8:38 pm
Re: Question: (Javascript?) script for adding page number to end of bookmark names
%[Page]/%[PagesCount]
If you add more pages, and you'd like to delete the pattern.
e.g. if you know the suffix is %[Page]/%[PagesCount]
and you'd like to use a new format , like just %[Page] instead, how would you go forth?
How would you for example remove "/%[PagesCount]" from all bookmarks
is there a way to do a reverse creation – meaning a deletion?If you add more pages, and you'd like to delete the pattern.
e.g. if you know the suffix is %[Page]/%[PagesCount]
and you'd like to use a new format , like just %[Page] instead, how would you go forth?
How would you for example remove "/%[PagesCount]" from all bookmarks
My wishlist https://forum.pdf-xchange.com/viewtopic.php?p=187394#p187394
Disable SPACE page navigation, fix kb shortcut for highlighting advanced search tool search field, bookmarks with numbers, toolbar small icon size, AltGr/Ctrl+Alt keyboard issues
Disable SPACE page navigation, fix kb shortcut for highlighting advanced search tool search field, bookmarks with numbers, toolbar small icon size, AltGr/Ctrl+Alt keyboard issues
Re: Question: (Javascript?) script for adding page number to end of bookmark names
Regular expressions are your friends for things like this. [url]https://en.wikipedia.org/wiki/Regular_expression#Basic_concepts[/url]MedBooster wrote: ↑Sun Dec 01, 2024 8:09 pm is there a way to do a reverse creation – meaning a deletion?
If you add more pages, and you'd like to delete the pattern.
e.g. if you know the suffix is %[Page]/%[PagesCount]
and you'd like to use a new format , like just %[Page] instead, how would you go forth?
How would you for example remove "/%[PagesCount]" from all bookmarks
I'd use the 'Find and Replace Bookmark Text' and check the box for "Regular Expressions". To match %[Page]/%[PagesCount] at the end of the text, with possibly a space before the page number use
Code: Select all
\s?[0-9]+/[0-9]+$
Code: Select all
(\s?[0-9]+/[0-9]+)?$
- Stefan - PDF-XChange
- Site Admin
- Posts: 19806
- Joined: Mon Jan 12, 2009 8:07 am
- Contact:
Re: Question: (Javascript?) script for adding page number to end of bookmark names
Hello Mathew,
Thanks for the RegEx! These are indeed powerful, but also have to be used with caution as they are sometimes "too" powerful, and as you said might match something that was not intended!
I hope this is of good use to MedBooster!
Kind regards,
Stefan
Thanks for the RegEx! These are indeed powerful, but also have to be used with caution as they are sometimes "too" powerful, and as you said might match something that was not intended!
I hope this is of good use to MedBooster!
Kind regards,
Stefan