Question: (Javascript?) script for adding page number to end of bookmark names

Forum for the PDF-XChange Editor - Free and Licensed Versions

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

Post Reply
MedBooster
User
Posts: 1372
Joined: Mon Nov 15, 2021 8:38 pm

Question: (Javascript?) script for adding page number to end of bookmark names

Post by MedBooster »

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
image.png
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
MedBooster
User
Posts: 1372
Joined: Mon Nov 15, 2021 8:38 pm

Re: Question: (Javascript?) script for adding page number to end of bookmark names

Post by MedBooster »

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
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
User avatar
Stefan - PDF-XChange
Site Admin
Posts: 19794
Joined: Mon Jan 12, 2009 8:07 am
Contact:

Re: Question: (Javascript?) script for adding page number to end of bookmark names

Post by Stefan - PDF-XChange »

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
MedBooster
User
Posts: 1372
Joined: Mon Nov 15, 2021 8:38 pm

Re: Question: (Javascript?) script for adding page number to end of bookmark names

Post by MedBooster »

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
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.

image.png
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
User avatar
Stefan - PDF-XChange
Site Admin
Posts: 19794
Joined: Mon Jan 12, 2009 8:07 am
Contact:

Re: Question: (Javascript?) script for adding page number to end of bookmark names

Post by Stefan - PDF-XChange »

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
MedBooster
User
Posts: 1372
Joined: Mon Nov 15, 2021 8:38 pm

Re: Question: (Javascript?) script for adding page number to end of bookmark names

Post by MedBooster »

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. :D
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
User avatar
Stefan - PDF-XChange
Site Admin
Posts: 19794
Joined: Mon Jan 12, 2009 8:07 am
Contact:

Question: (Javascript?) script for adding page number to end of bookmark names

Post by Stefan - PDF-XChange »

Hi Med Booster,

I've taken a look at what is available as JS methods - and there aren't that many (no "Rename):
image.png
image.png (3.92 KiB) Viewed 6237 times
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).
Mathew
User
Posts: 563
Joined: Thu Jun 19, 2014 7:30 pm

Re: Question: (Javascript?) script for adding page number to end of bookmark names

Post by Mathew »

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;
}
User avatar
Daniel - PDF-XChange
Site Admin
Posts: 10897
Joined: Wed Jan 03, 2018 6:52 pm

Re: Question: (Javascript?) script for adding page number to end of bookmark names

Post by Daniel - PDF-XChange »

Hello, Mathew

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
MedBooster
User
Posts: 1372
Joined: Mon Nov 15, 2021 8:38 pm

Re: Question: (Javascript?) script for adding page number to end of bookmark names

Post by MedBooster »

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 :o
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
User avatar
Stefan - PDF-XChange
Site Admin
Posts: 19794
Joined: Mon Jan 12, 2009 8:07 am
Contact:

Re: Question: (Javascript?) script for adding page number to end of bookmark names

Post by Stefan - PDF-XChange »

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
MedBooster
User
Posts: 1372
Joined: Mon Nov 15, 2021 8:38 pm

Re: Question: (Javascript?) script for adding page number to end of bookmark names

Post by MedBooster »

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?
image.png
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
User avatar
Stefan - PDF-XChange
Site Admin
Posts: 19794
Joined: Mon Jan 12, 2009 8:07 am
Contact:

Re: Question: (Javascript?) script for adding page number to end of bookmark names

Post by Stefan - PDF-XChange »

Hello MedBooster,

Definitely worked for me:
image.png
image.png (11.43 KiB) Viewed 3198 times
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
Mathew
User
Posts: 563
Joined: Thu Jun 19, 2014 7:30 pm

Re: Question: (Javascript?) script for adding page number to end of bookmark names

Post by Mathew »

I'm not sure why I didn't notice this before, but there's a much better tool to do this already built-in... :roll:

Bookmarks > Add Text to Bookmark Title
image.png
image(1).png
User avatar
Stefan - PDF-XChange
Site Admin
Posts: 19794
Joined: Mon Jan 12, 2009 8:07 am
Contact:

Re: Question: (Javascript?) script for adding page number to end of bookmark names

Post by Stefan - PDF-XChange »

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
MedBooster
User
Posts: 1372
Joined: Mon Nov 15, 2021 8:38 pm

Re: Question: (Javascript?) script for adding page number to end of bookmark names

Post by MedBooster »

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....
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
User avatar
Stefan - PDF-XChange
Site Admin
Posts: 19794
Joined: Mon Jan 12, 2009 8:07 am
Contact:

Re: Question: (Javascript?) script for adding page number to end of bookmark names

Post by Stefan - PDF-XChange »

Hello MedBooster,

Thanks for your comments and feedback - for the moment that is what we (and JS) can offer!

Kind regards,
Stefan
MedBooster
User
Posts: 1372
Joined: Mon Nov 15, 2021 8:38 pm

Re: Question: (Javascript?) script for adding page number to end of bookmark names

Post by MedBooster »

%[Page]/%[PagesCount]
image.png
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
Mathew
User
Posts: 563
Joined: Thu Jun 19, 2014 7:30 pm

Re: Question: (Javascript?) script for adding page number to end of bookmark names

Post by Mathew »

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
Regular expressions are your friends for things like this. [url]https://en.wikipedia.org/wiki/Regular_expression#Basic_concepts[/url]

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]+$
Thus:
image.png
If you want to update the pages at the same time, and add to pages that maybe don't currently have the page number, something like this:

Code: Select all

(\s?[0-9]+/[0-9]+)?$
image(1).png
The main gotcha in this approach is that if a bookmark name ends in number/number, the regular expression may match that and replace it. If that's a possibility, I'd work around that by always making the page numbering uniquely identifiable (ie p.%[Page] or enclosing the page number in brackets, etc.)
User avatar
Stefan - PDF-XChange
Site Admin
Posts: 19794
Joined: Mon Jan 12, 2009 8:07 am
Contact:

Re: Question: (Javascript?) script for adding page number to end of bookmark names

Post by Stefan - PDF-XChange »

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
Post Reply