Skip to main content
Participating Frequently
August 20, 2018
Answered

.toUpperCase() method working only once with getPageNthWord

  • August 20, 2018
  • 3 replies
  • 1800 views

Hello,

I'm beginner to JavaScript and Acrobat JS api. I'm trying to write a script which makes annotations on phrases instead of single words, and I wanted it to be case insensitive. Therefore logically, I thought of using .toUpperCase() method on both this.getPageNthWord(p, w) and the words searched. To simplify it's use for multiple sentences, every phrases entered are pushed in an array and the annotation function is called for every items in this array.

But the problem is that when I launch the script, the annotation is made only for the first phrase in the array and I get a "TypeError: this.getPageNthWord(p, w) has no properties" in the console.

Here is a basic case which reproduces the exact same problem.

In this example this is only an array of words which is searched for annotations, but as in my case, only the first word is highlighted and we get the same "TypeError: this.getPageNthWord(p, w) has no properties"

function DoHighlight(word)

{

for (var p = 0; p < this.numPages; p++)

{

  var cnt = this.getPageNumWords(p);

  for (var w = 0; w < cnt; w++)

  {

   if (this.getPageNthWord(p, w).toUpperCase() == word.toUpperCase())

   {

    this.addAnnot({

     page: p,

     type: "Highlight",

     strokeColor: color.red,

     quads: this.getPageNthWordQuads(p, w),

    });

   }

  }

}

}

var words = ["word1","word2"];

var t = words.forEach(function Highlight(word){ DoHighlight(word)  });

I know that there is already actions for phrase & word annotation here https://acrobatusers.com/actions-exchange​ , but as I'm  a beginner I would really like to understand why it only works on the first element, and how I can fix it. And also I would like to create custom actions with this one later.

Any help / explanation would be very appreciated!

This topic has been closed for replies.
Correct answer try67

Thanks again, for now I'm only trying to make this basic example work to implement case insensitive later in other scripts. As said in upper replies, this function works perfectly when I'm not using .toUpperCase(), that's only when I add it that the script stops working after the first page last word is reached. I'm sure that the script stops working because when I add other actions after the loop, like a simple console.println, it is not showing in the console

function DoHighlight(word)

{

for (var p = 0; p < this.numPages; p++)

{

  var cnt = this.getPageNumWords(p);

  for (var w = 0; w < cnt; w++)

  {

   if (this.getPageNthWord(p, w).toUpperCase() == word.toUpperCase())

   {

    this.addAnnot({

     page: p,

     type: "Highlight",

     strokeColor: color.red,

     quads: this.getPageNthWordQuads(p, w),

    });

   }

  }

}

}

var words = ["word1","word2"];

var t = words.forEach(function Highlight(word){ DoHighlight(word)  });


It's possible that getPageNthWord is returning null, so you need to check for that.

Change this line:

if (this.getPageNthWord(p, w).toUpperCase() == word.toUpperCase())

To:

if (this.getPageNthWord(p, w)!=null && this.getPageNthWord(p, w).toUpperCase() == word.toUpperCase())

3 replies

try67
Community Expert
August 20, 2018

The error message has nothing to do with toUpperCase.

I would recommend you pass a reference to the document object as a parameter to the function, instead of using the "this" keyword.

DNasAuthor
Participating Frequently
August 20, 2018

Thank you Try67, yes I mis explained I know that the .toUpperCase() method is not specifically & directly causing this error. What I mean is that when I use any method in my if statement I get a "TypeError: this.getPageNthWord(p, w) has no properties"

For the other part, sorry I don't really understand what you mean by "pass a reference to the document object as a parameter to the function". Do you mean something like this :

var myDoc = event.target;

function myFunction(word, myDoc) {

    // The function actions here... and use like:

    myDoc.getPageNthWord(p, w)

}

I would be really grateful if you could give a little example to help me understand

try67
Community Expert
August 20, 2018

What I mean is that when you call the DoHighlight function, add another parameter to it, the Document object itself, like this:

function DoHighlight(doc, word) {

...

}

And then when you call it, change it to this:

DoHighlight(this, word)

Then within that function itself change all references to "this" to "doc".

Bernd Alheit
Community Expert
August 20, 2018

Before the line:

  if (this.getPageNthWord(p, w).toUpperCase() == word.toUpperCase())

add following line:

  console.println(" " + p + " " + w + " " + this.getPageNthWord(p, w));

DNasAuthor
Participating Frequently
August 20, 2018

Thank you for your help! So after adding this line, it seems that when I'm applying any method (like .toUpperCase() in this example) on getPageNthWord, it stops getting the words after the first page (and of course only 1st page words are highlighted).

But when no methods are applied, all pages word's are returned & highlighted.

So it looks like the script stops working after the first page, it explains why only the first word is highlighted. But I have no idea why applying a method can cause this. I would be really grateful if you could help me understand / fix this

try67
Community Expert
August 20, 2018

I don't think that's the issue. Post your full code, please, and maybe share the file you're running it with us.

Lukas Engqvist
Community Expert
August 20, 2018

This may be a problem that you have:
getPageNumWords

getPageNthWord

Then you have "getPageNthWordQuads"

That you use camel case is not a problem, though I know many that would name a variable with a "myPageNumWords" etc

I would avoid using "get" in a name or anything that would be like code.
Is there other code also? Where do you get your quads from?

Bernd Alheit
Community Expert
August 20, 2018

https://forums.adobe.com/people/Lukas+Engqvist  wrote

This may be a problem that you have:
getPageNumWords

getPageNthWord

Then you have "getPageNthWordQuads"

That you use camel case is not a problem, though I know many that would name a variable with a "myPageNumWords" etc

I would avoid using "get" in a name or anything that would be like code.
Is there other code also? Where do you get your quads from?

This are functions of Adobe Acrobat and Acrobat Reader. This are not variables.

Lukas Engqvist
Community Expert
August 20, 2018

@Bernd I must have had my brain on vacation yesterday. I should be more focused when being on the forum.
Sorry for confusing the issue.