Skip to main content
Participant
February 10, 2023
Question

Trying to write a code to create bookmarked on all highlighted values in a document

  • February 10, 2023
  • 1 reply
  • 382 views

I've gone through and highlighted a lot of values in a pdf using the "Find, Highlight, and Extract" tool, and now I'm trying to make a bookmark at each highlighted value.  I'm trying to work something out but I keep getting errors.  Any help would be greatly appreciated!

 

var valuesToFind = ["1", "2"];

for (var i = 0; i < valuesToFind.length; i++) {
  this.syncAnnotScan();
  var results = this.search(valuesToFind[i], true, true);

  for (var j = 0; j < results.length; j++) {
    var result = results[j];

    var bookmark = this.addAnnot({
      type: "Bookmark",
      page: result.page,
      rect: result.rect,
      name: valuesToFind[i]
    });
  }
}

 

This topic has been closed for replies.

1 reply

Thom Parker
Community Expert
Community Expert
February 11, 2023

I don't know where you've been getting your information from, but you need to look through the Acrobat JavaScript reference to find out what functions and properties are available. There's also lots of sample code in the reference.

 

For example, there is no document "search" function. Doesn't exist. Here's Doc object reference entry

https://opensource.adobe.com/dc-acrobat-sdk-docs/library/jsapiref/doc.html#doc

 

 

There is no such thing as a bookmark annotation. Here is the Acrobat JavaScript Reference entry for the annotation types:

https://opensource.adobe.com/dc-acrobat-sdk-docs/library/jsapiref/JS_API_AcroJS.html#annotation-types

 

What you want to do is create a bookmark, here's the reference entry for the bookmark object.

https://opensource.adobe.com/dc-acrobat-sdk-docs/library/jsapiref/JS_API_AcroJS.html#bookmark

 

To seach for a particular type of annotation, use the "doc.getAnnots" function to get an array of all annotations on the PDF. The sort for the type:

 

var aAnnots = this.getAnnots();
if(aAnnots){
  var aHiLites = aAnnots.filter(a=>a.type == "Highlight");
  for(var i=0;i<aHiLites.length;i++){
     ... Add bookmark code ...
   }
}

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often