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

Search a pdf document using javascript

New Here ,
May 09, 2023 May 09, 2023

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?

TOPICS
How to , JavaScript , PDF forms
5.1K
Translate
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 ,
May 10, 2023 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.

@+

Translate
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 ,
May 10, 2023 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.

Translate
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
New Here ,
May 10, 2023 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.

Translate
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 ,
May 10, 2023 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.

Translate
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
New Here ,
May 10, 2023 May 10, 2023

Thanks, but our members wouldn't be able to cope with that. And they would need acrobat.  

Translate
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 ,
May 10, 2023 May 10, 2023

They are not capable of using a simple search window? In that case, your script will certainly be too much for them, too.

Translate
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 ,
May 10, 2023 May 10, 2023

@try67 What is the method you can use for searching a file with JavaScript?

@+

Translate
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 ,
May 10, 2023 May 10, 2023

See above. I use the built-in getPageNthWord method, but it's very limited. Or I use a non-JS solution for searching the file (such as the Search & Remove Text tool) and then use the results from that in my script for further processing.

Translate
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
New Here ,
May 10, 2023 May 10, 2023

Would you be able to provide an example?

Translate
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 ,
May 10, 2023 May 10, 2023

Of course, this method is for searching a word in a file.

I misunderstood, I thought you were saying that it was possible with JavaScript to search for a file (its name or path in a directory for example).

@+

Translate
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
New Here ,
May 10, 2023 May 10, 2023

I tried this method but the code only executes with certain (not helpful) actions so it actually can't process a "current file".  Other than that it searches quickly and returns the results in a panel.

Translate
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 ,
May 10, 2023 May 10, 2023

I don't understand what you're trying to do there, and certainly not why you're using the Doc Will Close event for it... How can you search the file if it just closed?

Translate
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
New Here ,
May 10, 2023 May 10, 2023

I agree, it was an example I picked up from a Community post.  At the moment I am willing to try anything.  I am pretty much starting from scratch.  I try and describe what I am trying to do at the top of this post.

Translate
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
LEGEND ,
May 10, 2023 May 10, 2023

I maybe am missing something, but can your target audience really not use Ctrl+F to search?

Translate
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
New Here ,
May 11, 2023 May 11, 2023

Yes, a simple search using CMD-F or the search in Preview (Mac) might have to be good enough, and it is fast.

This raises another question if you might help.  When I export the library database from MS Excel I end up with 75 pages as it is in a big font and in portrait mode. 

Could I use Acrobat Pro to reformat the PDF to change to landscape mode and reduce the font size?  If I could automate that process all the better

Translate
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 ,
May 12, 2023 May 12, 2023
LATEST

No. This is something that needs to be done before the file is converted to PDF.

Translate
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