Skip to main content
Participating Frequently
May 10, 2023
Question

Search a pdf document using javascript

  • May 10, 2023
  • 2 replies
  • 6005 views

I want to write some javascript to include as an action behind a button on a form to search a PDF file and find lines in the document which include those words and then return the result to a text box in the form.

I have managed to write the code that opens the file and get the keywords to search on but can't work out how to use those parameters to search the file.

 

I will have five buttons on the form for five separate PDF docs.

 

Can anyone help, or suggest a better way?

This topic has been closed for replies.

2 replies

try67
Community Expert
Community Expert
May 10, 2023

Searching a file using JS is possible, actually, but quite complicated, and there's no easy way to display the results. It can also be very slow, if the file is long.

You can do it using the getPageNthWord method, but notice it will process one word at a time, so if your search term is more than one word then you'll need to take that into account.

Participating Frequently
May 10, 2023

Ok, not looking good then.  So any other options?  This is to be a way of putting our Guild's library system on our website.  It is currently in MS Access and that can't run on the website so I thought I could export it to a PDF file and then give our members a way of searching the PDF's to find a book or a video on various topics.

try67
Community Expert
Community Expert
May 10, 2023

Why re-invent the wheel? Acrobat (and Reader) already have a pretty powerful built-in search functionality. Especially the Advanced Search tool could work pretty well for you. It displays a list of all the matches it finds in a separate window, allowing you to click each one and go to that location in the file, and it can even search multiple files at once. If you have Acrobat you can even export the results of such a search as a CSV file. And it has boolean searching options, proximity, stemming, etc.

bebarth
Community Expert
Community Expert
May 10, 2023

Hi,
You can't search the files with JavaScript!
You can give a list of files then the script will search in this list if the words you are looking for are included in each file or not, but that's all.

@+