Skip to main content
Participant
June 27, 2020
Answered

JavaScript - Searching PDF and displaying an output for a Text Field

  • June 27, 2020
  • 2 replies
  • 4705 views

Hello, I was wondering if anyone could help me with a script for a text field. I'm new to javascript so it's somewhat is a challenge to me. I was wondering if there is a function that can search my entire pdf for a string and if the string is there then change the value of the text box to another string, for example "Found." To be more specific, I wanted to check off buttons if three certain words are found through out my document. I believe I can handle the rest of the script if I can get some guidance on how to scan my doccument for a certrain string. Any help would be appreciated, thanks!

 

This topic has been closed for replies.
Correct answer try67

That won't work because ckWord will only be a single word, so it can't match to "Not Acrobat" or anything else that's more than one word. Also, I don't understand the need to do it for static contents. Either it's there or it's not there. You can just search for it using the Find command and fill the field manually...

2 replies

ls_rbls
Community Expert
Community Expert
June 28, 2020

I borrowed the following idea from Example 2 of the Adobe Acrobat SDK JavaScript API Reference, Doc methods, page 232

 

Running it as a button with  a mouse up action:

 

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") {this.getField("Text18").value = "Found"; }

else if (ckWord =="Not Acrobat") {this.getField("Text18").value = "Found"; }

else if (ckWord =="Maybe is Acrobat")  {this.getField("Text18").value = "Found"; }

  }  
 } 
try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
June 28, 2020

That won't work because ckWord will only be a single word, so it can't match to "Not Acrobat" or anything else that's more than one word. Also, I don't understand the need to do it for static contents. Either it's there or it's not there. You can just search for it using the Find command and fill the field manually...

ls_rbls
Community Expert
Community Expert
June 28, 2020

Got it. But that script actually worked. I typed in  "Not Acrobat" in my field hit the button and it worked, it worked   with every worI used in the example ; single or more than one word.

 

It actually found  it and changed the field value to "Found".  I would say  Beginner's Luck?   🙂

 

try67
Community Expert
Community Expert
June 28, 2020

Do you want to search static text, or the contents of fields, or comments? If static text then you can use the getPageNthWord method to iterate over all the words of a page. If form fields, then you would need to iterate over all the (text) fields in the file (using getNthFieldName), examining their value property. If comments, then you would also need to iterate over all of them (using getAnnots), looking at their contents property.

Walter L.Author
Participant
June 28, 2020

It is to static text, thank you for that. I didn't realize that there was functions to even search the comment section of a pdf. The more I learn about the the different functions, the more intrigued I get. Thank you for your help!