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

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

Moderators: TrackerSupp-Daniel, Tracker Support, Paul - Tracker Supp, Vasyl-Tracker Dev Team, Chris - Tracker Supp, Sean - Tracker, Ivan - Tracker Software, Tracker Supp-Stefan

MedBooster
User
Posts: 1070
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
You do not have the required permissions to view the files attached to this post.
Wishlist
Bookmarks with page numbers
Optional fixed small icon size in the toolbar
Shift to UNLOCK aspect ratio/i]
Allow more "toolbars" to the title bar
AltGr issues with character input and keyboard shortcuts
MedBooster
User
Posts: 1070
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
Wishlist
Bookmarks with page numbers
Optional fixed small icon size in the toolbar
Shift to UNLOCK aspect ratio/i]
Allow more "toolbars" to the title bar
AltGr issues with character input and keyboard shortcuts
User avatar
Tracker Supp-Stefan
Site Admin
Posts: 17960
Joined: Mon Jan 12, 2009 8:07 am
Location: London

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

Post by Tracker Supp-Stefan »

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: 1070
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
You do not have the required permissions to view the files attached to this post.
Wishlist
Bookmarks with page numbers
Optional fixed small icon size in the toolbar
Shift to UNLOCK aspect ratio/i]
Allow more "toolbars" to the title bar
AltGr issues with character input and keyboard shortcuts
User avatar
Tracker Supp-Stefan
Site Admin
Posts: 17960
Joined: Mon Jan 12, 2009 8:07 am
Location: London

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

Post by Tracker Supp-Stefan »

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: 1070
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....
Wishlist
Bookmarks with page numbers
Optional fixed small icon size in the toolbar
Shift to UNLOCK aspect ratio/i]
Allow more "toolbars" to the title bar
AltGr issues with character input and keyboard shortcuts
User avatar
Tracker Supp-Stefan
Site Admin
Posts: 17960
Joined: Mon Jan 12, 2009 8:07 am
Location: London

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

Post by Tracker Supp-Stefan »

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
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).
You do not have the required permissions to view the files attached to this post.
Mathew
User
Posts: 239
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
TrackerSupp-Daniel
Site Admin
Posts: 8624
Joined: Wed Jan 03, 2018 6:52 pm

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

Post by TrackerSupp-Daniel »

Hello, Mathew

Thank you very much for this, it looks like it will be plenty helpful for someone.

Kind regards,
Dan McIntyre - Support Technician
Tracker Software Products (Canada) 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: 1070
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
Wishlist
Bookmarks with page numbers
Optional fixed small icon size in the toolbar
Shift to UNLOCK aspect ratio/i]
Allow more "toolbars" to the title bar
AltGr issues with character input and keyboard shortcuts
User avatar
Tracker Supp-Stefan
Site Admin
Posts: 17960
Joined: Mon Jan 12, 2009 8:07 am
Location: London

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

Post by Tracker Supp-Stefan »

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: 1070
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
You do not have the required permissions to view the files attached to this post.
Wishlist
Bookmarks with page numbers
Optional fixed small icon size in the toolbar
Shift to UNLOCK aspect ratio/i]
Allow more "toolbars" to the title bar
AltGr issues with character input and keyboard shortcuts
User avatar
Tracker Supp-Stefan
Site Admin
Posts: 17960
Joined: Mon Jan 12, 2009 8:07 am
Location: London

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

Post by Tracker Supp-Stefan »

Hello MedBooster,

Definitely worked for me:
image.png
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
You do not have the required permissions to view the files attached to this post.
Mathew
User
Posts: 239
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
You do not have the required permissions to view the files attached to this post.
User avatar
Tracker Supp-Stefan
Site Admin
Posts: 17960
Joined: Mon Jan 12, 2009 8:07 am
Location: London

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

Post by Tracker Supp-Stefan »

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: 1070
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....
Wishlist
Bookmarks with page numbers
Optional fixed small icon size in the toolbar
Shift to UNLOCK aspect ratio/i]
Allow more "toolbars" to the title bar
AltGr issues with character input and keyboard shortcuts
User avatar
Tracker Supp-Stefan
Site Admin
Posts: 17960
Joined: Mon Jan 12, 2009 8:07 am
Location: London

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

Post by Tracker Supp-Stefan »

Hello MedBooster,

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

Kind regards,
Stefan