Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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.
@+
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Thanks, but our members wouldn't be able to cope with that. And they would need acrobat.
Copy link to clipboard
Copied
They are not capable of using a simple search window? In that case, your script will certainly be too much for them, too.
Copy link to clipboard
Copied
@try67 What is the method you can use for searching a file with JavaScript?
@+
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Would you be able to provide an example?
Copy link to clipboard
Copied
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).
@+
Copy link to clipboard
Copied
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
I maybe am missing something, but can your target audience really not use Ctrl+F to search?
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
No. This is something that needs to be done before the file is converted to PDF.