Feature Request - Options for Generating Bookmarks

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

Post Reply
Puffolino
User
Posts: 317
Joined: Wed Feb 09, 2011 1:06 pm

Feature Request - Options for Generating Bookmarks

Post by Puffolino »

I'd like to have more options to control bookmarks when creating them from text.
1. Very often I get leading and trailing spaces which could be automatically trimmed
2. It also happens, that multiple spaces are seen within the text, so another option would be fine to reduce these to a single space
3. sometimes, two headings are combined to a single bookmark entry, like "1.0 Headline 1.1 Sub headline ". Not sure, if such effects could be eliminated as well

I made a workaround for cases 1 and 2 by writing a javascript code. It's not perfect as I saw I need to start it multiple times because it reduces only one occurence of multiple spaces each time. Another point is, that "Undefined" appears at the end of the console output - don't know, why this will be displayed.

Code: Select all

function OptimizeBookmark(bkm,depth)
{	
	if (depth>0 && bkm.name.length>0)
	{
		bkm.name=bkm.name.trim().replace(/ {2,}/," ");
		var z=bkm.name.substring(0,1);
		console.println(z+": "+bkm.name);
	}

	if (bkm.children != null)
		for (var i = 0; i < bkm.children.length; i++) 
			OptimizeBookmark(bkm.children[i],depth+1); 
} 

console.clear(); console.show();
console.println("Optimizing bookmarks...");

OptimizeBookmark(this.bookmarkRoot,0);
console.println("All done.");
User avatar
TrackerSupp-Daniel
Site Admin
Posts: 8436
Joined: Wed Jan 03, 2018 6:52 pm

Re: Feature Request - Options for Generating Bookmarks

Post by TrackerSupp-Daniel »

Hello Puffolino,

Hello, I believe these options already exist, take a look in the bookmarks from page text menu:
Image

Beyond that I'll answer by point:
1. The leading and trailing spaces are likely caused by the font type/size also being applied to said spaces, if you can ensure that there are no spaces in the text itself, this should not occur.
2. I am not an expert, but I believe as many PDF softwares do not have a true "Space" character, there is no way of determining how many "spaces" exist between point A and B, In turn causing it to read out exactly what has been interpreted on our end from the document in question. Again this will be solved by removing unnecessary characters.
3. Please follow this KB for detailed instructions for how to avoid this kind of problem. https://www.pdf-xchange.com/knowle ... the-Editor

Regarding the Undefined issue in JS, this is a common printout when there is not other output for the JS console, If you setup the code to record and print information from the console you should find this issue disappears. Nonetheless, it is also easily ignored.
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
User avatar
Ovg
User
Posts: 461
Joined: Tue Sep 05, 2017 4:56 pm

Re: Feature Request - Options for Generating Bookmarks

Post by Ovg »

Puffolino wrote:
"it reduces only one occurence of multiple spaces each time."

Change

Code: Select all

bkm.name=bkm.name.trim().replace(/ {2,}/," ");

to

Code: Select all

bkm.name=bkm.name.trim().replace(/ {2,}/g," ");
It's impossible to lead us astray for we don't care even to choose the way.
PDF-XChange PRO, 10.1.1 (Build 381) / W7 SP1 x64
User avatar
TrackerSupp-Daniel
Site Admin
Posts: 8436
Joined: Wed Jan 03, 2018 6:52 pm

Re: Feature Request - Options for Generating Bookmarks

Post by TrackerSupp-Daniel »

Thanks for sharing your knowledge Ovg!
What would I do without you?
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
User avatar
Ovg
User
Posts: 461
Joined: Tue Sep 05, 2017 4:56 pm

Re: Feature Request - Options for Generating Bookmarks

Post by Ovg »

:oops:
It's impossible to lead us astray for we don't care even to choose the way.
PDF-XChange PRO, 10.1.1 (Build 381) / W7 SP1 x64
User avatar
TrackerSupp-Daniel
Site Admin
Posts: 8436
Joined: Wed Jan 03, 2018 6:52 pm

Re: Feature Request - Options for Generating Bookmarks

Post by TrackerSupp-Daniel »

:)
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
Puffolino
User
Posts: 317
Joined: Wed Feb 09, 2011 1:06 pm

Re: Feature Request - Options for Generating Bookmarks

Post by Puffolino »

Thanks for the quick responses!

I did find the formatting dialog and that's the place where the additional functions (trimming, space eliminating, evtl. regular expressions in search/replace) could be added.

I didn't know about the 'g' thing - just downloaded the javascript pdf api but didn't find enough information how to include regular expressions into the scripts. Anyhow, the javascript interface looks very promising, I like to have such a feature!

Cheers,
Michael
User avatar
Ovg
User
Posts: 461
Joined: Tue Sep 05, 2017 4:56 pm

Re: Feature Request - Options for Generating Bookmarks

Post by Ovg »

2Puffolino

For Regex look at:
It's impossible to lead us astray for we don't care even to choose the way.
PDF-XChange PRO, 10.1.1 (Build 381) / W7 SP1 x64
Puffolino
User
Posts: 317
Joined: Wed Feb 09, 2011 1:06 pm

Re: Feature Request - Options for Generating Bookmarks

Post by Puffolino »

Thanks, I knew regular expressions before but had no idea about javascript nor their flags '/i' '/g' etc.
Puffolino
User
Posts: 317
Joined: Wed Feb 09, 2011 1:06 pm

Re: Feature Request - Options for Generating Bookmarks

Post by Puffolino »

My code works nearly perfect now, it removes line feeds as well as heading, trailing and multiple spaces and detects combined bookmarks ("1.2 Title 1.2.1 Subtitle") and try to split them to seperate bookmarks ("1.2 Title" and "1.2.1 Subtitle").

I wasn't able to copy the link reference from the "1.2 Title" bookmark to the new child, so clicking on this bookmark fails.

Because these are my first steps using javascript together with PDF's, I'm still a novice. So, I was wondering why a change of the value in the button definition (cExec: "OptimizeAllBookmarks(0);") did not work until restarting the PDF-XChange Editor program and I would be happy if a script file could be added to the plugin folder and assigned to a button for global usage.

Code: Select all

app.addToolButton({   
 cName: "ToolOptBookmarks",   
 oIcon: oIcon,   
 cExec: "OptimizeAllBookmarks(0);",
 cTooltext: "Optimize Bookmarks",   
 cLabel: " ",
 nPos: 2
});

function OptimizeBookmark(bkm,depth)
{	
	if (depth>0 && bkm.name.length>0)
	{
		bkm.name=bkm.name.trim().replace(/\n/g," ").replace(/ {2,}/g," ");
		//var z=bkm.name.substring(0,1);
		//if (z.length>0 && "0123456789.".search(z)>-1) console.println("Zahl");
		var z=bkm.name.search(/ \d+\.\d+/);
		//console.println(z+": "+bkm.name);
		if (z>1)
		{	var s=bkm.name.substring(z);
			console.println(z+": "+bkm.name);
			bkm.name=bkm.name.substring(0,z);
			bkm.createChild(s);
		}
	}

	if (bkm.children != null)
		for (var i = 0; i < bkm.children.length; i++) 
			OptimizeBookmark(bkm.children[i],depth+1); 
} 

function OptimizeAllBookmarks(view)
{
	console.clear();
	if (view) console.show();
	console.println("Optimizing bookmarks...");
	
	OptimizeBookmark(this.bookmarkRoot,0);
	
	console.println("All done.");
}
User avatar
TrackerSupp-Daniel
Site Admin
Posts: 8436
Joined: Wed Jan 03, 2018 6:52 pm

Re: Feature Request - Options for Generating Bookmarks

Post by TrackerSupp-Daniel »

Hello puffolino,
This article might help, it allows you to create a button in the Editor UI from JS
https://www.pdf-xchange.com/knowle ... the-Editor
Sadly I don't know enough JS myself to help troubleshoot beyond this...
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
User avatar
Ovg
User
Posts: 461
Joined: Tue Sep 05, 2017 4:56 pm

Re: Feature Request - Options for Generating Bookmarks

Post by Ovg »

change

Code: Select all

oIcon: oIcon,
with something like

Code: Select all

oIcon: util.iconStreamFromImage("/C/My Documents/My nice icon.ico"),
It's impossible to lead us astray for we don't care even to choose the way.
PDF-XChange PRO, 10.1.1 (Build 381) / W7 SP1 x64
User avatar
TrackerSupp-Daniel
Site Admin
Posts: 8436
Joined: Wed Jan 03, 2018 6:52 pm

Re: Feature Request - Options for Generating Bookmarks

Post by TrackerSupp-Daniel »

8)
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
Puffolino
User
Posts: 317
Joined: Wed Feb 09, 2011 1:06 pm

Re: Feature Request - Options for Generating Bookmarks

Post by Puffolino »

Many thanks, have no problems to create the toolbar icon (found here), I was to lazy to add the inline icon definition, sorry that I've confused everyone :roll:

My problem is to copy a link location from one bookmark to a newly created one (bkm.createChild) . So clicking on the newly created bookmark does nothing. I think, I found something here, which could help me to solve my problem, but actually I wasn't able to read the expression from the actual bookmark to "transfer" it to the children.
User avatar
TrackerSupp-Daniel
Site Admin
Posts: 8436
Joined: Wed Jan 03, 2018 6:52 pm

Re: Feature Request - Options for Generating Bookmarks

Post by TrackerSupp-Daniel »

Hi puffolino,
Glad that's one thing solved,
as for copying and creating a child, Could you post your JS so far for that command in here so everyone is able to take a look at it and work on a resolution together for you?
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
Puffolino
User
Posts: 317
Joined: Wed Feb 09, 2011 1:06 pm

Re: Feature Request - Options for Generating Bookmarks

Post by Puffolino »

Here's my actual code:

Code: Select all


// Button and it's parameter ('0') is kept in cache - to change the console output for debuggig, the editor has to be restarted before the modified script (parameter '1') is executed
app.addToolButton({   
 cName: "ToolOptBookmarks",   
 oIcon: oIcon,   
 cExec: "OptimizeAllBookmarks(0);",
 cTooltext: "Optimize Bookmarks",   
 cLabel: " ",
 nPos: 1
});

function OptimizeBookmark(bkm,depth)
{	
	if (depth>0 && bkm.name.length>0)
	{
		bkm.name=bkm.name.trim().replace(/\n/g," ").replace(/ {2,}/g," ");
		//var z=bkm.name.substring(0,1);
		//if (z.length>0 && "0123456789.".search(z)>-1) console.println("number");
		var z=bkm.name.search(/ \d+\.\d+/);
		//console.println(z+": "+bkm.name);
		if (z>1)
		{	var s=bkm.name.substring(z);
			console.println(z+": "+bkm.name);
			bkm.name=bkm.name.substring(0,z);
			bkm.createChild(s);
		}
	}

	if (bkm.children != null)
		for (var i = 0; i < bkm.children.length; i++) 
			OptimizeBookmark(bkm.children[i],depth+1); 
} 

function OptimizeAllBookmarks(view)
{
	console.clear();
	if (view) console.show();
	console.println("Optimizing bookmarks...");
	
	OptimizeBookmark(this.bookmarkRoot,0);
	
	console.println("All done.");
}

User avatar
TrackerSupp-Daniel
Site Admin
Posts: 8436
Joined: Wed Jan 03, 2018 6:52 pm

Re: Feature Request - Options for Generating Bookmarks

Post by TrackerSupp-Daniel »

it has just occurred to me that I've neglected to share the JS API reference with you, it may help out a bit:

Looking at the "children" command as detailed in here, I don't know for sure but believe you may want to try using nLevel instead of depth for that specific instance of it.

Hth
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
Puffolino
User
Posts: 317
Joined: Wed Feb 09, 2011 1:06 pm

Re: Feature Request - Options for Generating Bookmarks

Post by Puffolino »

Thanks, the API documentation is fine.
But it seems there's no option to read the "action" of a bookmark (there are only properties like "color" and "name"). Maybe there's this is a given restriction but I can't image why this should be.

BTW "depth" or "nLevel" are just variable names, so no problem here...
User avatar
Tracker Supp-Stefan
Site Admin
Posts: 17810
Joined: Mon Jan 12, 2009 8:07 am
Location: London
Contact:

Re: Feature Request - Options for Generating Bookmarks

Post by Tracker Supp-Stefan »

Hello Puffolino,

As you have probably noticed - those are all the properties and methods available through JS for Bookmarks:
JS_Bookmark.png
JS_Bookmark.png (6.86 KiB) Viewed 3554 times
I'd assume the main reason for only those being available are security related.

Regards,
Stefan
Puffolino
User
Posts: 317
Joined: Wed Feb 09, 2011 1:06 pm

Re: Feature Request - Options for Generating Bookmarks

Post by Puffolino »

Thanks, could improve my scripts using your hints.

Could you confirm, that moving a bookmark entry can't be done using JavaScript?

Sometimes the hierarchy of automatically created bookmarks doesn't work because similar fonts are used for different outline levels.
When a numbering scheme is present, the result may look like this (all bookmarks are children of the root):

+ 1 Chapter One
+ 1.1 Subtitle
+ 1.2 Subtitle
+ 2 Chapter 2
+ 2.1 Subtitle
+ 2.1.1 Sub-Sub...
+ 2.2 Subtitle

I'd like to resort that using a script to a correct tree by changing the child/parent links within the bookmarks...
+ 1 Chapter One
| - 1.1 Subtitle
| - 1.2 Subtitle
+ 2 Chapter 2
| - 2.1 Subtitle
| | - 2.1.1 Sub-Sub...
| - 2.2 Subtitle
User avatar
Tracker Supp-Stefan
Site Admin
Posts: 17810
Joined: Mon Jan 12, 2009 8:07 am
Location: London
Contact:

Re: Feature Request - Options for Generating Bookmarks

Post by Tracker Supp-Stefan »

Hello Puffolino,

The insertChild is the method you need:
Inserts the specified bookmark as a child of this bookmark. If the bookmark already exists in the bookmark tree, it is unlinked before inserting it back into the tree.



Regards,
Stefan
Post Reply