Download a free trial of the new Acrobat.
useful to make the search results directly available to the script, but alas, this is not the case. The script has no access to the search results. The search object was designed on the premise it would be used to provide simple but powerful search interfaces to users, so we are limited to using it in only this way, which isn’t necessarily a bad thing.
search.matchCase = false; search.wordMatching = "MatchAnyWord"; search.bookmarks = true; search.query("menu list","ActiveDoc");
One possible use for the above script would be for implementing a dynamic index. For example, rather than a word in the index providing explicit page numbers, the word itself would have a link over it. When the user presses the link, it runs a script similar to the script above. This technique allows information to be added and removed from the index without the document developer ever having to remake the index.
The most common usage scenario for the search object is to provide specialized search tools for a library of documents, such as a set of documents on CD. For example, let’s say all the forum posts for this month at www.acrobatusers.com were converted into a set of PDFs and distributed to users on a CD, one PDF for each forum category. The developers of the CD want to provide an easy-to-use search mechanism customized to this set of documents.
The first thing the developers need to do is create an index catalog file. This is a special type of file Acrobat uses to make searches more efficient, i.e., Acrobat looks through the index file rather than searching the PDF. In Acrobat 9, this feature is accessed from the "Advanced > Document Processing > Full Text Index with Catalog…" menu item. This menu item displays a dialog that builds an index file for an entire folder of PDFs. In our example, we’ll need both individual index files for each forum category, i.e., each separate PDF, and for all the PDFs together. To do this, the individual PDFs associated with a category will need to be placed in separate folders. This gives us the ability to narrow a search to one specific forum category, or to broaden it to all the documents, and to do it in an efficient manner.
Using an index for a search
The last input parameter is the full, device-independent path to the catalog file. In this case, "G" is the CD Drive, but for most situations the full path will not be readily available. We’ll need to derive it from the current file’s path. When working with index files, the relative path structure between the index and the PDF must remain the same. So if we know the location of the PDF we know the location of the index file. The following code demonstrates a general method for finding the path to our index file from the current document:
// Split the file path into an array var aPath = this.path.split("/"); // Remove the last element, which is the file name aPath.pop(); // Join the path elements back together and add the index file var cIdxPath = aPath.join("/") + "/Catalogs/JSforum.pdx"; search.query("menu list", "Index", cIdxPath);
This code can be used from a button or link on a PDF file, given of course that the path modification is correct for the PDF and index locations.
The other, more general method for searching an index is to use the Active Index List. The search object keeps the list of the active indexes in the search.indexes property. Unfortunately, this property is privileged. It cannot be used from a document script, so its use will not be covered in this article.
Building the user interface
The next step in this process is to set up a search interface for the user. One of the easiest ways to do this is to create a PDF with all the controls the user needs to do searches specific to the documents on the CD. This could be a separate PDF or part of a larger introduction/index document on the CD, something that pops up automatically when the CD starts up.
To create the interface, we need three elements (Figure 3)-
Figure 3 – User interface for a custom search. A few of the search object options provided.
In this interface, a few search object options, such as case sensitivity and word matching, are included as extras, but they aren’t necessary for running the basic search functionality. Many more options can be included, depending on what the developer wants to expose to the users.
All of the functionality is in the search button, which runs the script shown below. Notice that the first thing the script does is to test the search text. The search will only be run when the search text is not empty. The next part of the script uses the location of the current PDF to determine the full path to the index files, exactly the same setup as shown in the previous script, except that the combo-box selection is used to determine the specific index path. Then, the search options exposed in the user interface are set, and finally the actual search query is called.
This sample is available for download in the Custom Search Page for Doc Library example. Remember what I said earlier about the folder structure. This example contains several files and will only work if the folder structure is extracted from the zip file with all the files, verbatim.
|Edit PDF create PDF Action Wizard|
Try Acrobat DC
Get started >
Post, discuss and be part of the Acrobat community.
Join now >