Skip to main content
New Participant
December 11, 2020
Answered

getPageNthWord returning [object Doc] when used in a text field custom calculation

  • December 11, 2020
  • 2 replies
  • 4138 views

Hello

 

Using Acrobat DC Pro.

 

When I try to the use the function this.getPageNthWord() or doc.getPageNthWord() and convert it to a string, I get the [object Doc] instead of a word. I can use the function this.getPageNthWord() by intelf on the same document on the console command and it comes up with the first word in the document, but not when I use the function in the custom calculation of a text field.

Bit of context

I have a working custom stamp that brings up an execDialog box for the user to enter stamp data into when the stamp is placed. I would like the stamp dialogbox text fields to be filled in with data from text on the document when the stamp is applied.

This topic has been closed for replies.
Correct answer try67

try67

 

I tried adding your code in the stamp code and neither the dialog box nor the alert dialog box show up at all, but the stamp image is still applied to a document page. I tried replacing all of the stamp code to only contain the two lines of code you posted and an alert dialog box appears with

(new String("Vendor"))

which is the first text word that is in the stamp pdf not the first word of the target pdf.

Doing some more reading I found that

this.getPageNthWord(0,0)

can be replaced with

event.source.source.getPageNthWord(0,0)

This seems to get me the first word of the docuemnt being stamped. I don't beleive I understand how this works though since I am trying to now use event.source.source.pageNum function and it does not work.


Yes, in the context of a dynamic stamp event.source.source returns the document to which the stamp is being applied. And I tried it with event.source.source.pageNum and it worked fine, by the way.

2 replies

ls_rbls
Adobe Expert
December 11, 2020

Since you've mentioned  about a bit of context, I think you're not using that function in the correct context.

 

I am learning JavaScript so bear with me if I say something incorrect, but you can't convert a function to string if the object itself is not defined to begin with.

 

Running the function by itself in the console is not converting anything to a string, is just reporting back. 

 

So when you say "When I try to the use the function this.getPageNthWord() or doc.getPageNthWord() and convert it to a string" is hard to make sense out of it, unless you mean that you have defined an array for example, or a for loop, with  a condition to search for a specific word. And since you've mentioned about using the function by itself, and that it finds the first word in the document, that is not a text string that was taken from a field object for example. The text string that you're referring to is taken from the free text content of your document. That doesn't necessarily mean it was found in an annotation or a comment.

 

See Example 2, in Page 232 of the Adobe Acrobat SDK JavaScript API JavaScript™ for Acrobat® API Reference "Doc methods"

 

//Example 2
//Search through the document for the word “Acrobat” and create a link around that word.

for (var p = 0; p < this.numPages; p++)
{
var numWords = this.getPageNumWords(p);
for (var i=0; i<numWords; i++)
{
var ckWord = this.getPageNthWord(p, i, true);
if ( ckWord == "Acrobat")
{
var q = this.getPageNthWordQuads(p, i);
// Convert quads in default user space to rotated
// User space used by Links.
m = (new Matrix2D).fromRotated(this,p);
mInv = m.invert()
r = mInv.transform(q)
r=r.toString()
r = r.split(",");
l = addLink(p, [r[4], r[5], r[2], r[3]]);
l.borderColor = color.red;
l.borderWidth = 1;
l.setAction("this.getURL('http://www.example.com/')");
}
}
}

 

try67
Adobe Expert
December 11, 2020

Post your full code, please.

Chris5FC7Author
New Participant
December 14, 2020
Please download the attached file to view this post
try67
try67Correct answer
Adobe Expert
December 14, 2020

try67

 

I tried adding your code in the stamp code and neither the dialog box nor the alert dialog box show up at all, but the stamp image is still applied to a document page. I tried replacing all of the stamp code to only contain the two lines of code you posted and an alert dialog box appears with

(new String("Vendor"))

which is the first text word that is in the stamp pdf not the first word of the target pdf.

Doing some more reading I found that

this.getPageNthWord(0,0)

can be replaced with

event.source.source.getPageNthWord(0,0)

This seems to get me the first word of the docuemnt being stamped. I don't beleive I understand how this works though since I am trying to now use event.source.source.pageNum function and it does not work.


Yes, in the context of a dynamic stamp event.source.source returns the document to which the stamp is being applied. And I tried it with event.source.source.pageNum and it worked fine, by the way.