• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

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

Community Beginner ,
Jun 27, 2020 Jun 27, 2020

Copy link to clipboard

Copied

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!

 

TOPICS
Acrobat SDK and JavaScript

Views

3.7K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 2 Correct answers

Community Expert , Jun 28, 2020 Jun 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 =="May
...

Votes

Translate

Translate
Community Expert , Jun 28, 2020 Jun 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...

Votes

Translate

Translate
Community Expert ,
Jun 28, 2020 Jun 28, 2020

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jun 28, 2020 Jun 28, 2020

Copy link to clipboard

Copied

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!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 28, 2020 Jun 28, 2020

Copy link to clipboard

Copied

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"; }

  }  
 } 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 28, 2020 Jun 28, 2020

Copy link to clipboard

Copied

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...

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 28, 2020 Jun 28, 2020

Copy link to clipboard

Copied

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?   🙂

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 28, 2020 Jun 28, 2020

Copy link to clipboard

Copied

No. It's not. What happened is it matched on "Acrobat", which then populated the field.

Also, you didn't include a command to set the field to be empty or "Not Found", or something like that, so once a single hit is made (on "Acrobat", for example), it won't revert back, even if no matches are found.

Resest the from, remove the first condition (for "Acrobat") and re-run it. You'll see it stays empty.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jun 28, 2020 Jun 28, 2020

Copy link to clipboard

Copied

and you are right about it not being empty. I don't have to run it since I came across this problem yesterday.

It should have an else statement at the end that looks something like this. For my situation the ckWord will work. For future knowledge what would you use instead of ckWord for a string with mulitple words?

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"; }

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

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 28, 2020 Jun 28, 2020

Copy link to clipboard

Copied

No, that won't work, either, as you're resetting the field to "Not Found" after each word... And you're still searching for multiple words, which can't work using this approach.

If your search term is one or two words then this method could work (after some adjustments), but if it's longer, more complex or involves multiple search terms I would compose a single string from each page (or even from the entire document) and then search that, instead.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jun 28, 2020 Jun 28, 2020

Copy link to clipboard

Copied

LATEST

I was stuck on this for the past 30 minutes and that makes a sense because it was a for loop.  Thank you very much!

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jun 28, 2020 Jun 28, 2020

Copy link to clipboard

Copied

The reason why I am searching static content is for error prevention. I want the buttons to fill themself in manually based on the word being found in the documents so there wouldn't be any input error. This page I'm working on is something that is changing based on docments attached. Once I figure out fully on how to change that, I will also be adding to the script on how to attach a certain document, if possible, to the end of my script. I'm assuming there is a function on adding a certain file to the pdf based on a document path in the computer. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines