[Suggestion] JavaScript to convert annotations or base content (with specific text formatting) into bookmarks

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

[Suggestion] JavaScript to convert annotations or base content (with specific text formatting) into bookmarks

Post by MedBooster »

I am hereby summoning Javascript expert Mathew

even though my example here is for annotations ... it would be cool to do the same based on base content as well! Maybe it would have to be two separate scripts? Or there could be a way to toggle between the two, or both, with checkboxes in the GUI
Let's say you have a very specific type of typewriter/textbox font type.

e.g. Arial, pt 14, #0078D7

Would it be possible to have a JavaScript script / tool, which would create bookmarks, with the text being the destination, of ONLY annotations with these SPECIFIC annotation text format/font combination?

Meaning that other annotations which in this case would have another size, or be of another color or font, wouldn't be converted into bookmarks?

Any ideas?

Something like this?

Code: Select all

var annotations = this.getAnnots();
if (annotations) {
    for (var i = 0; i < annotations.length; i++) {
        var annot = annotations[i];
        // Check specific properties
        if (annot.font === "Arial" && annot.fontSize === 14 && annot.color === color.blue) {
            // Create a bookmark for the annotation
            var bkmk = this.bookmarkRoot.createChild(annot.contents);
            bkmk.setAction("this.pageNum = " + annot.page + ";");
        }
    }
}
It would have been cool to have a graphic user interface with for example
Font type
Font size
Color
extra: bold/italic/underscore checkboxes?

maybe with some ability to save presets as well, or to copy over the format from highlighted text if the text you have selected has only 1 type of the aforementioned formatting.

Thus, if you were lucky enough to have a document where headings are formatted differently from the default body text... you would be able to create bookmarks only for the headings...
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: [Suggestion] JavaScript to convert annotations or base content (with specific text formatting) into bookmarks

Post by MedBooster »

If we don't want a GUI, maybe something like this is fine, to just paste into the JavaScript console
HeX color code would be better

Example for base content which doesn't work yet ↓
Btw. is this the correct way to give an error message if no base content is found?

Code: Select all

// Customizable variables for font type, color, and size
var targetFont = "GoudyOldStyleT-Bold";
var targetColor = "0,0,0"; // Black in RGB format
var targetFontSize = 10;

// Iterate through all pages in the document
for (var i = 0; i < this.PageCount; i++) {
    var pageText = this.getPageText(i); // Extract all text from the page
    if (pageText) {
        // Check for specific font properties (pseudo-check based on known patterns)
        if (pageText.includes(targetFont)) {
            // Create a bookmark for the content
            var bkmk = this.BookmarkRoot.CreateChild("Page " + (i + 1) + ": " + pageText.substring(0, 50));
            bkmk.ActionGoTo(i, 0); // Set destination to the page
        }
    } else {
        // Log an error message if no text is found on the page
        console.log("Error: No base text content found on page " + (i + 1));
    }
}

Last edited by MedBooster on Sun Dec 01, 2024 11:52 am, edited 1 time in total.
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: [Suggestion] JavaScript to convert annotations or base content (with specific text formatting) into bookmarks

Post by MedBooster »

As far as I could tell there isn't a script like this on the forum yet.

Seemingly the single most relevant thread with a working script:

Feature Request - Options for Generating Bookmarks
viewtopic.php?p=122953&hilit=javascript ... gs#p122953

Other JavaScripts for bookmarks
Javascript to change bookmark property based on search results
viewtopic.php?p=187130&hilit=javascript ... rk#p187130

Re: Problems with creating bookmarks from content list
viewtopic.php?p=186565&hilit=javascript ... rk#p186565

JavaScript - Select children bookmarks if I selected parent
viewtopic.php?p=184813&hilit=javascript ... rk#p184813

Delete all children bookmarks
viewtopic.php?p=184596&hilit=javascript ... rk#p184596

Javascript command to find setences spread over multiple lines
viewtopic.php?p=184576&hilit=javascript ... rk#p184576

t[Bookmarking] Mass/BuIk Destination Setting
viewtopic.php?p=184538&hilit=javascript ... rk#p184538

Search term:
search.php?keywords=javascript+bookmark



Mathew's PDF-XCEJavaScripts
viewtopic.php?p=178663&hilit=javascript ... rk#p178663
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: 564
Joined: Thu Jun 19, 2014 7:30 pm

Re: [Suggestion] JavaScript to convert annotations or base content (with specific text formatting) into bookmarks

Post by Mathew »

MedBooster wrote: Sun Dec 01, 2024 11:27 am Arial, pt 14, #0078D7

Would it be possible to have a JavaScript script / tool, which would create bookmarks, with the text being the destination, of ONLY annotations with these SPECIFIC annotation text format/font combination?

Meaning that other annotations which in this case would have another size, or be of another color or font, wouldn't be converted into bookmarks?

Any ideas?
Hah, I just happened on this post :) I guess there's no way to tag someone in a post on this forum.

For base content: Isn't this functionality built-in with the Generate Bookmarks from Page Text menu item? You select the text you want to search for, and then in the dialog you can get the style from selected text:
image.png
Am I missing something here?

In your script, I don't think there's a document method .getPageText() but you can get page text with .getPageNumWords() and .getPageNthWord()

For annotations, I'm not sure if the built-in tool works. Maybe the tool I posted here provides a starting point: viewtopic.php?t=42180

If you're scripting it, you'd also need to search the .richContents property documented here: [url]https://opensource.adobe.com/dc-acrobat-sdk-docs/library/jsapiref/JS_API_AcroJS.html#richcontents[/url]
MedBooster
User
Posts: 1372
Joined: Mon Nov 15, 2021 8:38 pm

Re: [Suggestion] JavaScript to convert annotations or base content (with specific text formatting) into bookmarks

Post by MedBooster »

AWESOME THANK YOU

Page 1002 in the manual
https://downloads.pdf-xchange.com/PDFXE10_MAN.pdf

for annotations I would convert the font to properties only the bookmarks would have... then convert the annotations to base content

and then run the bookmarks generation tool you show in the screenshot...
Last edited by MedBooster on Tue Dec 03, 2024 12:26 pm, edited 1 time in total.
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: [Suggestion] JavaScript to convert annotations or base content (with specific text formatting) into bookmarks

Post by MedBooster »

Mathew wrote: Mon Dec 02, 2024 7:58 pm Hah, I just happened on this post :) I guess there's no way to tag someone in a post on this forum.
The only way to "tag" someone in the sense that they receive a notification is to mention a post of theirs I guess...

so maybe just use

Code: Select all

[quote=Mathew post_id=187488 time=1733169484 user_id=94963]
[/quote]

Code: Select all

[quote=Mathew user_id=94963]
[/quote]
Mathew wrote:
[mention=Mathew user_id=94963]
[/mention]

Maybe Tracker support could implement a "mention" system. Usually forums would use @ (at-sign)
(@username)

BUT as the saying, don't fix what isn't broken and there probably is a reason things are as they are.
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
Daniel - PDF-XChange
Site Admin
Posts: 10910
Joined: Wed Jan 03, 2018 6:52 pm

Re: [Suggestion] JavaScript to convert annotations or base content (with specific text formatting) into bookmarks

Post by Daniel - PDF-XChange »

Hello, MedBooster

Sorry for the delay, We usually see about 20-45 new topics in a week, and this past week that has somehow ballooned to over 100 new topics... all with active discussions ongoing. Some items, like those with JS which I am not very fluent in, were left by the wayside. Having a chance to read over this new, I would like to give Mathew a big thank you for stepping in to share that method, I am glad to see it helps!

As for your most recent post, We don't have a standard mention system somewhat intentionally, and as you say, I don't think it is likely to change. If users are looking for constant updates on a topic, they can configure notifications of new posts in their user profile area here on the forums:
image.png
and have the option to "subscribe" to a discussion personally without posting, if they are interested:
image(1).png
image(1).png (11.86 KiB) Viewed 792 times
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: [Suggestion] JavaScript to convert annotations or base content (with specific text formatting) into bookmarks

Post by MedBooster »

yo yo happy new year

I don't really see how I can generate bookmarks from all underlined text or text in bold / italic....
(preferably it would still include all text on the line that isn't underlined as well)
Is this possible without highlighting the text?

in this case the text came up as "calibri bold" , but that is not specific enough, I want only the underlined bold text, not all bold text with this style/color/font.
image.png

How could I add a criteria to only bookmarkify text that has a number in the beginning as well?
with a dot "." or parenthesis ")"
either 1-9.
or 10-100.
image(1).png
Any other way than choosing "first character is a digit"? What if you use a format like T01-T99
Topic 1 to 99 — then it doesn't fit "first character is a digit"
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
Daniel - PDF-XChange
Site Admin
Posts: 10910
Joined: Wed Jan 03, 2018 6:52 pm

Re: [Suggestion] JavaScript to convert annotations or base content (with specific text formatting) into bookmarks

Post by Daniel - PDF-XChange »

Hello, MedBooster

#1, Italic, Bold, and BoldItalic, would appear as font subsets for selection:
image.png
Technically speaking "Underline" is not a font type/style, and so does not appear in these areas, it cannot be used in this way.
You can minimize this by further defining aspects, like font size, and color instead.

#2, You would likely need to use a Regular expression for such a case. There are unfortunately too many possible variables for us to offer an option for all of the possibilities out there.
image(1).png
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
Post Reply