Javascript to change bookmark property based on search results
Moderators: PDF-XChange Support, Daniel - PDF-XChange, Chris - PDF-XChange, Sean - PDF-XChange, Paul - PDF-XChange, Vasyl - PDF-XChange, Ivan - Tracker Software, Stefan - PDF-XChange
-
- User
- Posts: 7
- Joined: Wed Sep 25, 2024 11:23 am
Javascript to change bookmark property based on search results
Hi team,
I am looking for a short(ish) script ollowing me to change the color of a bookmark based on results I can optain from my search.
I got this suggested:
var bkm = this.bookmarkRoot.children[0];
var searchResults = search.query("true and fair", "ActiveDoc");
if (searchResults && searchResults.results && searchResults.results.length > 0) {
// Iterate through search results and highlight matches (optional)
for (var i = 0; i < searchResults.results.length; i++) {
var result = searchResults.results;
// Highlight the matched text or perform other actions
}
bkm.color = "red"; // Set bookmark color to red
} else {
bkm.color = "blue"; // Set bookmark color to blue
}
But it doesn't work for me. I also spent week looking at this page but no success.
https://opensource.adobe.com/dc-acrobat-sdk-docs/library/jsdevguide/JS_Dev_SearchIndexEssentials.html
My idea is to look for a word or a sentence "my search" for example. And if it is found in the document to make a bookmark (lets call it "my search") blue if not found and red if found.
Is it possible?
Thank you
I am looking for a short(ish) script ollowing me to change the color of a bookmark based on results I can optain from my search.
I got this suggested:
var bkm = this.bookmarkRoot.children[0];
var searchResults = search.query("true and fair", "ActiveDoc");
if (searchResults && searchResults.results && searchResults.results.length > 0) {
// Iterate through search results and highlight matches (optional)
for (var i = 0; i < searchResults.results.length; i++) {
var result = searchResults.results;
// Highlight the matched text or perform other actions
}
bkm.color = "red"; // Set bookmark color to red
} else {
bkm.color = "blue"; // Set bookmark color to blue
}
But it doesn't work for me. I also spent week looking at this page but no success.
https://opensource.adobe.com/dc-acrobat-sdk-docs/library/jsdevguide/JS_Dev_SearchIndexEssentials.html
My idea is to look for a word or a sentence "my search" for example. And if it is found in the document to make a bookmark (lets call it "my search") blue if not found and red if found.
Is it possible?
Thank you
-
- Site Admin
- Posts: 11252
- Joined: Wed Jan 03, 2018 6:52 pm
Re: Javascript to change bookmark property based on search results
Hello, Vinny
My apologies, I am not fluent enough in Js to offer a solution here, but hopefully someone else can come along and help.
I am posting in the name of making this more visible. You seem to have posted in the SDK forum, however, we have very few active SDK users, and JS is not exclusively an SDK function.
Would you like me to move this post to the Editor sub-forum so that more of our JS focused end users may see it, or are you looking exclusive for an SDK centered solution here?
Kind regards,
My apologies, I am not fluent enough in Js to offer a solution here, but hopefully someone else can come along and help.
I am posting in the name of making this more visible. You seem to have posted in the SDK forum, however, we have very few active SDK users, and JS is not exclusively an SDK function.
Would you like me to move this post to the Editor sub-forum so that more of our JS focused end users may see it, or are you looking exclusive for an SDK centered solution here?
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: 7
- Joined: Wed Sep 25, 2024 11:23 am
Re: Javascript to change bookmark property based on search results
Yes if you can move it to the appropriate forum please. Thank you very much.
-
- User
- Posts: 600
- Joined: Thu Jun 19, 2014 7:30 pm
Re: Javascript to change bookmark property based on search results
A few hurdles here:
- the search object doesn't actually return a search result, it just gets PXCE to run a search. I'm not sure if one can access the results of that search through javascript. The only way I know to search a document is to search the text directly in javascript:
* For annotations, see my script here for one approach: viewtopic.php?p=177460&hilit=tool#p177460
* For document text you could use the getPageNthWord() method of the document object - I'm not clear what you want to match in the bookmarks?
* Are you matching text in the bookmark or
* matching the bookmark's destination or making a new bookmark pointing to the location of the matched text on the document? Making a new bookmark or determining where a bookmark points is tenuous in javascript because there's no way to access the actual properties of bookmarks. You can only activate the bookmark and see what happens with .execute(), and can only assign a javascript to the bookmark with .setAction().
Code: Select all
search.query("true and fair");
// list all properties of search object
for (let i in search) console.println( i + ":" +search[i]);
-
- User
- Posts: 600
- Joined: Thu Jun 19, 2014 7:30 pm
Re: Javascript to change bookmark property based on search results
Maybe I'm reading too far into it. If just searching bookmark text, something like this:
Code: Select all
/** script to highlight current search result
*
* @param bkm - the bookmark root whose child bookmarks are getting highlighted
* @param searchTx - the text to search for in bookmarks
* @param highlightColors - optional array of [color to hightlight match, optional color to highlight not match]
**/
// sample search
bkSearch( this.bookmarkRoot, "true and fair", [color.red, color.blue] );
// need a function to do recursive search through bookmarks
function bkSearch( bkm, searchTx, highlightColors=[color.red] ) {
for (const b of bkm.children) {
// see if text is in bookmark name
if (b.name.indexOf( searchTx ) > -1) {
b.color = highlightColors[0];
} else if (highlightColors[1]) {
b.color = highlightColors[1];
}
// search children if it has them
if (b.children?.length) {
bkSearch( b, searchTx, highlightColors );
}
}
}
-
- User
- Posts: 7
- Joined: Wed Sep 25, 2024 11:23 am
Re: Javascript to change bookmark property based on search results
Thanks for the reply. I now realise that I wasn't very clear so my apologies. I will now give a longer explanation:
I have to find data in at least 300 pages long documents. The most efficient way I found to do it consist of using the "import bookmark from text" function.
That way I am a set off bookmark titles (without any destination) which tells my team what data they are looking for in the documents.
I am bringing this a step further by giving an action to some bookmarks: Using the bookmark properties, we can "program" (for lack of better word) the bookmark when clicked on to inititate a Javascript action -> it will search for terms.
Here is the javascript in question:
search.wordMatching = "MatchAnyWord";
search.query("\"mysearch1\" \"mysearch2\" \"mysearch3\"","ActiveDoc");
Its very simple and efficient which is great for people like me who don't know much about JS and coding in general.
My next step is now to change the color of these bookmarks depending on the info they found/or don't find.
So lets say I want my bookmark to look for the word "Ring of power" and if it is found my bookmark will turn red, and if not found it should turn blue.
Will there be a JS action I can attach to my existing script?
I have to find data in at least 300 pages long documents. The most efficient way I found to do it consist of using the "import bookmark from text" function.
That way I am a set off bookmark titles (without any destination) which tells my team what data they are looking for in the documents.
I am bringing this a step further by giving an action to some bookmarks: Using the bookmark properties, we can "program" (for lack of better word) the bookmark when clicked on to inititate a Javascript action -> it will search for terms.
Here is the javascript in question:
search.wordMatching = "MatchAnyWord";
search.query("\"mysearch1\" \"mysearch2\" \"mysearch3\"","ActiveDoc");
Its very simple and efficient which is great for people like me who don't know much about JS and coding in general.
My next step is now to change the color of these bookmarks depending on the info they found/or don't find.
So lets say I want my bookmark to look for the word "Ring of power" and if it is found my bookmark will turn red, and if not found it should turn blue.
Will there be a JS action I can attach to my existing script?
You do not have the required permissions to view the files attached to this post.
-
- Site Admin
- Posts: 11252
- Joined: Wed Jan 03, 2018 6:52 pm
Re: Javascript to change bookmark property based on search results
Hello, Vinny
I am not certain on why this would need to be something that happens automatically in the background after opening the file. If the check is a simply "does this bookmark name exist in this document" than you should be able to apply the color coding before you provide the document to anyone else, instead of trying to make JS do a complicated check and modify colors after the fact.
In your example, I would suggest using the search bar the top of the bookmarks pane, typing in "ring of power" and then you can select every bookmark which has the term in it, and change its color as desired. You can even use this method to change the color of multiple bookmarks at once.
Making a JS for this seems unnecessary when it would need to be added to every bookmark one by one, and it would take just as much time to add this JS, as it would to do the search manually, and change the color by hand... Am I missing something?
Kind regards,
I am not certain on why this would need to be something that happens automatically in the background after opening the file. If the check is a simply "does this bookmark name exist in this document" than you should be able to apply the color coding before you provide the document to anyone else, instead of trying to make JS do a complicated check and modify colors after the fact.
In your example, I would suggest using the search bar the top of the bookmarks pane, typing in "ring of power" and then you can select every bookmark which has the term in it, and change its color as desired. You can even use this method to change the color of multiple bookmarks at once.
Making a JS for this seems unnecessary when it would need to be added to every bookmark one by one, and it would take just as much time to add this JS, as it would to do the search manually, and change the color by hand... Am I missing something?
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: 7
- Joined: Wed Sep 25, 2024 11:23 am
Re: Javascript to change bookmark property based on search results
Hi Daniel,
The search is for words in the document itself. I don't indend to search word in the bookmarks. The bookmarks are a checklist for the data to look for in the documents.
Once the word is found in the document we set the bookmark destination to the page it is located. This JS could be very useful as I will be able to tell automatically if the info I am looking for is there or not in the 300 page long document whithout having to read through even one page.
The search is for words in the document itself. I don't indend to search word in the bookmarks. The bookmarks are a checklist for the data to look for in the documents.
Once the word is found in the document we set the bookmark destination to the page it is located. This JS could be very useful as I will be able to tell automatically if the info I am looking for is there or not in the 300 page long document whithout having to read through even one page.
-
- Site Admin
- Posts: 11252
- Joined: Wed Jan 03, 2018 6:52 pm
Re: Javascript to change bookmark property based on search results
Hello, Vinny
In that case, would the "Create bookmarks from page text" tool not suffice? This allows you to enter respective criteria, such as fonts, size, or color of the text, but also a specific text pattern/string. If you created an action with this tool, you would be able to Create a new set of bookmarks which includes the items that were present in the document text, and even apply specific colors to the various matching data sets.
I am sorry to say that I cannot think of a way to offer exactly what you are looking for here, so I am attempting to understand the root of the case and find a viable alternative instead.
Kind regards,
In that case, would the "Create bookmarks from page text" tool not suffice? This allows you to enter respective criteria, such as fonts, size, or color of the text, but also a specific text pattern/string. If you created an action with this tool, you would be able to Create a new set of bookmarks which includes the items that were present in the document text, and even apply specific colors to the various matching data sets.
I am sorry to say that I cannot think of a way to offer exactly what you are looking for here, so I am attempting to understand the root of the case and find a viable alternative instead.
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: 7
- Joined: Wed Sep 25, 2024 11:23 am
Re: Javascript to change bookmark property based on search results
Hi Daniel,
I haven't looked into it in detailbut it seems like a decent back-up solution. Thanks for the advices
I haven't looked into it in detailbut it seems like a decent back-up solution. Thanks for the advices

-
- Site Admin
- Posts: 19913
- Joined: Mon Jan 12, 2009 8:07 am