[tool] Name bookmarks based on text in area of page  SOLVED

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

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

Mathew
User
Posts: 602
Joined: Thu Jun 19, 2014 7:30 pm

[tool] Name bookmarks based on text in area of page  SOLVED

Post by Mathew »

This tool will name bookmarks based on text in specific locations on each page. I made it for working with technical drawings that have sheet number and name in set locations (ie in a titleblock). I think something like this built-in would be a fantastic addition to PXE. I modeled it on the "Generate bookmarks from page text" tool - including copying the icon.

Installation
To use the tool, extract the zip and save files in the Javascripts folder either in the application folder, or in %APPDATA%\Tracker Software\PDFXEditor\3.0\Javascripts
bkmNameRegions v1.3.zip

Restart PDF-XChange and it will add a tool to the Bookmarks ribbon or menu:
image(1).png

Usage
Click in the tool to open an extensive dialog:
image.png
Choose whether to rename current bookmarks or add bookmarks
At the top, select whether to rename current bookmarks or add bookmarks for a page range.
Page range format is the same as the print dialog: Dash is inclusive range (10-20), comma separates ranges (1,5,10-12).
For PDF XChange build 393 or later, you can select which bookmarks to change using the button.

Adding rename steps
On the left top, there are buttons to Add, move or delete individual naming steps. For PDF XChange versions before 387, there's also an 'Edit' button. Below that it lists the steps. Each 'step' can be a separate selected area, or piece of text, that is combined to make the new bookmark name.

Editing rename steps
Select a step from the list (and for versions before 387 then press 'Edit…'. Because PDF XChange versions before 387 didn't allow a script to know when you've selected a step, you must press the Edit button to update the fields on the right side.)
On the right side of the dialog are the options for this step.
image(2).png
From the top:
  • At the top is a preview of the text found in the currently selected region on the page.
    Below that is the editable name for this step and a button to Select Region: Press this button to pick an area on the page. It should activate the rectangle tool for you to draw a rectangle, and shortly after you draw it, it should come back to this dialog, with the rectangle shown in green.
    100% inside: To the right of this button is an option whether to only include text that is completely inside the picked area, or allow words to hang outside the rectangle slightly.
    Anchor: The region selected can be anchored to the page corners, or edge midpoints, or center. This is so that if page sizes change slightly, the picked area still captures text in the same relative point to the anchor point.
    White space: Select whether to strip out white space (spaces, tab, newline, etc) or white space and punctuation from the found text.
    Word order: Because the arrangement of words in title blocks sometimes doesn't match the word order that is stored in the pdf, there's an option to sort the word order top to bottom of page, and/or right and left.
    PV: To PreView what text is selected on different pages, use the < or > buttons to cycle through the currently selected bookmarks or page range.
    Text: Optionally add text before or after the text that was found on the page. I included an implementation some of PXE's macros (sequence number, page, number of pages, page label, filename, date, and I added one that adds the previous bookmark name).
    Transformations: The case can be changed to UPPER, lower, Initial Capitals, Title Case. Title Case capitalizes all words except a, an, the, for, and, nor, but, or, yet, so, as, at, by, for, in, of, on, per, pro, sub, thru, to, up, re, ca., vs., v., w/, w/o. It will do find and replace on the new name and can use a macro in the replacement text.
(and for versions before 387 Don't forget to press "Edit" before making any changes to a step!)

When all changes are ready, press Rename to run the tool. It can be a bit slow; there's a progress dialog in the lower right corner.

As of v0.5 the tool is translatable: If you want it in a language other than English, edit the translationData.bkmNameReg.json file to add translation for the English phrases. Post the translation file here and I'll include it in future revisions. For instructions on using the tool I made to help editing the translation files see viewtopic.php?p=186101#p186101 or for more information about 1ang.js see the post viewtopic.php?t=43253

Limitations
  • Fixed v1.0 The tool does not generate bookmarks. It renames the current bookmarks.
  • Fixed PDF XChange build 393: The tool renames all of the current bookmarks!
  • Because the only way for javascript to know where a bookmark points to is to activate that bookmark, the renaming can be a bit slow. I added a progress dialog.
  • Fixed in build 387: The dialog doesn't know when you picked a different step, so you must press "edit" after you pick a step or you'll be editing the previously selected step. This can be confusing.
There are two things that would make this tool more useful IMO. If I get time, I'll add them, but without the ability to add bookmarks and links that change with added/moved sheets, it's of limited use to me:
  1. Done. If the tool added bookmarks, and those bookmarks could be the same as those generated by PXE internally (so they update with added pages, etc)
  2. If the tool could search the page for text in the bookmark (ie sheet number) and also add a link over this text.
The part of this tool that was hardest to figure out was how to interactively select an area on the page. It does that by setting up an interval that checks for new annotations on the page, gets it and passes it to the script. It's a bit fragile (ie if something causes the interval to delay, nothing seems to happen after drawing the rectangle.) This tool is still a bit rough around the edges, but let me know if you have suggestions.

For v1.0 I found a workaround to generating bookmarks: The script goes to the required location, then runs the built-in menu item app.execMenuItem('cmd.addBookmark', doc). It then has to figure out which bookmark was added, and change its name. It now just assembles a list of all the bookmarks before the bookmark is added, then compares by object to the list to find the new one. To work if the language is not English, it needs to know what the "untitled" bookmark name is, so the translation tool is essential for this.

Because there are so many different pieces in this tool, I've split out some of the utility functions to "xutils" that are in a separate file. I'll post info about that separately.
You do not have the required permissions to view the files attached to this post.
Last edited by Mathew on Mon Apr 28, 2025 5:06 pm, edited 31 times in total.
Mathew
User
Posts: 602
Joined: Thu Jun 19, 2014 7:30 pm

Re: [tool] Rename bookmarks based on text in area of page

Post by Mathew »

Slight revision to cache the page words so it speeds up the dialog slightly. Also deals better with page rotations, and if pages are offset from [0,0]. Added an alert if there are no bookmarks. Also updated in post above.
bkmNameRegions v0.3.2.zip
You do not have the required permissions to view the files attached to this post.
Last edited by Mathew on Tue May 14, 2024 7:28 pm, edited 4 times in total.
Mathew
User
Posts: 602
Joined: Thu Jun 19, 2014 7:30 pm

Re: [tool] Rename bookmarks based on text in area of page

Post by Mathew »

Example usage
When working with technical drawings in pdf format, it really helps to have the sheet names and numbers as bookmarks. Some software creates this automatically when it outputs to pdf, but frequently it's missing. I've tried using the 'Generate bookmarks from page text' tool, but because of the way sheet titles can be laid out on pages, it's difficult to get that tool to do it right. viewtopic.php?t=41182

For this example, I have a 100+ page set of drawings from another consultant, but none of the pages are bookmarked. As of v1.0 you can skip the first step.
  1. (optional)First go to Bookmarks > Bookmark Every Nth Page… and add a bookmark to every page. It doesn't matter what's in the bookmark text because it will be replaced.
    image.png
  2. Go to the BkName by Area tool. Select whether to rename bookmarks, or to add them for all or specific pages.
    image(6).png
  3. Press 'Add…' and then 'Select Region':
    image(1).png
  4. It will close the dialog and the rectangle tool will be active. Select the area for the sheet number:
    image(2).png
  5. The dialog will open again, and if text is found, it will show the preview. Select "Strip Whitespace" to clear out any spaces or newline characters that the pdf may have around the page. I'm leaving the anchor at the lower right corner, because that's where the sheet number is:
    image(3).png
  6. Now add the sheet name to the bookmark. Press the 'Add…' button to add another area and then 'Select Region'. This time select the sheet name:
    image(4).png
  7. Now add a dash with spaces around it to Text Before and set Transformations to Title Case:
    image(5).png
  8. Press Rename and it will rename all the bookmarks:
    image(7).png
You do not have the required permissions to view the files attached to this post.
Last edited by Mathew on Sat Apr 05, 2025 6:46 pm, edited 4 times in total.
User avatar
Daniel - PDF-XChange
Site Admin
Posts: 11277
Joined: Wed Jan 03, 2018 6:52 pm

Re: [tool] Name bookmarks based on text in area of page

Post by Daniel - PDF-XChange »

Hello, Mathew

This looks like a very complicated piece of functionality. Thank you very much, I hope this can help someone!

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
Mathew
User
Posts: 602
Joined: Thu Jun 19, 2014 7:30 pm

Re: [tool] Name bookmarks based on text in area of page

Post by Mathew »

A minor change to make it work properly in build 387. This version removes the "Edit" button in the dialog for build 387+ so using the list box is a bit more intuitive.
bkmNameRegions v0.4.zip
You do not have the required permissions to view the files attached to this post.
Mathew
User
Posts: 602
Joined: Thu Jun 19, 2014 7:30 pm

Re: [tool] Name bookmarks based on text in area of page

Post by Mathew »

A small update to add the tool to the Bookmarks toolbar in the ribbon UI, and the Bookmarks menu in Classic UI. Also I made it translatable. I've put two versions below, one with the translation files, and one without. There are currently no translations.
bkmNameRegions v0.5.zip
bkmNameRegions v0.5_en.zip
You do not have the required permissions to view the files attached to this post.
Mathew
User
Posts: 602
Joined: Thu Jun 19, 2014 7:30 pm

Re: [tool] Name bookmarks based on text in area of page

Post by Mathew »

This is a big improvement for the tool. I've added the option to add bookmarks, or select which bookmarks to change (build 393+ only).
bkmNameRegions v1.0.zip
  • I realized that I can work around the inability for javascript to directly add proper bookmarks (yet) by selecting the page and then running the menu item to add a bookmark and finding that new bookmark to rename. A bit roundabout, but now it can add bookmarks.
  • Also, as of PXCE build 393 improvements to the hierarchical list box, I can more easily add an option to select which bookmarks to change.
You do not have the required permissions to view the files attached to this post.
User avatar
Stefan - PDF-XChange
Site Admin
Posts: 19913
Joined: Mon Jan 12, 2009 8:07 am

Re: [tool] Name bookmarks based on text in area of page

Post by Stefan - PDF-XChange »

Hello Mathew,

Many thanks for this significant update to the tool!

Kind regards,
Stefan
Mathew
User
Posts: 602
Joined: Thu Jun 19, 2014 7:30 pm

Re: [tool] Name bookmarks based on text in area of page

Post by Mathew »

[Edit - I saw one download of v1.1, but only a minor change here so I'm not making a new post]
Update v1.2 posted above. Mostly usability issues (added buttons to change which page is active so you can see how the selected region is affected on different pages).
bkmNameRegions v1.2.zip


Changes since v1.1
  • add Initial Capitals to distinguish from Title Case (not capitalized list: a, an, the, for, and, nor, but, or, yet, so, as, at, by, for, in, of, on, per, pro, sub, thru, to, up, re, ca., vs., v., w/, w/o);
  • increase find history to 20 in drop down;
  • indicate if "all bookmarks" are picked
Changes since v1.0
  • change menu button name,
  • move addBookmark v3 to xutils (no longer requires translation of "Untitled"),
  • update selected text preview,
  • added a tiny little section 'PV' (page view) buttons to move current page. The less-than and greater-than symbols move through the currently selected bookmarks or page range
    image.png
  • minify (jsTool). Source posted below:
    bkmNameRegions.source v1.2.zip
You do not have the required permissions to view the files attached to this post.
User avatar
Daniel - PDF-XChange
Site Admin
Posts: 11277
Joined: Wed Jan 03, 2018 6:52 pm

[tool] Name bookmarks based on text in area of page

Post by Daniel - PDF-XChange »

:)
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
Mathew
User
Posts: 602
Joined: Thu Jun 19, 2014 7:30 pm

Re: [tool] Name bookmarks based on text in area of page

Post by Mathew »

I ran into some bugs. Fixed in v1.3 uploaded above and here.
bkmNameRegions v1.3.zip

Changes since v1.2
  • fix bug when pdf has no bookmarks,
  • fix bug with title case,
  • scroll page to show selected region,
  • moved tool to end of bp.rbar.create and removed from add-on toolbar,
  • slight change in menu name (shortened to "From Page Areas…") so translation strings updated
You do not have the required permissions to view the files attached to this post.
User avatar
Stefan - PDF-XChange
Site Admin
Posts: 19913
Joined: Mon Jan 12, 2009 8:07 am

[tool] Name bookmarks based on text in area of page

Post by Stefan - PDF-XChange »

:)