Page 1 of 1

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

Posted: Mon Jan 16, 2023 1:05 pm
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

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

Posted: Mon Jan 16, 2023 1:08 pm
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

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

Posted: Mon Jan 16, 2023 1:13 pm
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

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

Posted: Mon Jan 16, 2023 1:18 pm
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

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

Posted: Mon Jan 16, 2023 1:24 pm
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

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

Posted: Mon Jan 16, 2023 1:54 pm
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....

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

Posted: Mon Jan 16, 2023 2:50 pm
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
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

Posted: Thu Dec 14, 2023 9:03 pm
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;
}

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

Posted: Thu Dec 14, 2023 10:27 pm
by Daniel - PDF-XChange
Hello, Mathew

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

Kind regards,

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

Posted: Sat Dec 30, 2023 2:26 am
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

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

Posted: Tue Jan 02, 2024 12:45 pm
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

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

Posted: Tue Jan 02, 2024 1:59 pm
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

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

Posted: Tue Jan 02, 2024 2:34 pm
by Stefan - PDF-XChange
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

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

Posted: Wed Jan 10, 2024 2:22 am
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

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

Posted: Wed Jan 10, 2024 11:20 am
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

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

Posted: Tue Jan 16, 2024 8:52 am
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....

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

Posted: Tue Jan 16, 2024 11:21 am
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

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

Posted: Sun Dec 01, 2024 8:09 pm
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

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

Posted: Mon Dec 02, 2024 4:35 pm
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.)

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

Posted: Tue Dec 03, 2024 12:15 pm
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