How to search text in PDF Viewer by using python script?

Is there anyone who know how to search text in PDF Viewer by using python script? After searching, the page can skip to the highlight postion automatically.


Both the Vision and Perspective versions have search functionality already available, either by using Ctrl-F within Perspective or the "Search Document" button within the Vision component. So creating a search function with Python is not needed unless there is further functionality you want to implement. If that is the case, would you describe what else you are trying to do within the script?

Hello Joshua,
I am also trying the same functionality which requires passing search text as a parameter in the PDF Viewer popup window. While opening the popup window i need to pass the search text in the PDF and need to get the appropriate results automatically in the PDF. Thanks.

Are you saying you want to open the side panel and enter the search term via scripting, so it looks like this when the popup opens?

Also, this thread is tagged Vision. That is the module you are developing in, correct?

Yes Justin.

I threw together a library script that will enter the search term into the text box, click the search button, and select the first found option from the tree via scripting. Just feed it the PDF viewer component and the search term:

def setPDFViewerSearchResult(pdfViewer, searchTerm):
	# Recursive scripts to get various components from the pdf viewer
	def getSearchPanel(pdfViewer):
		for component in pdfViewer.components:
			if component.__class__.__name__ == 'JTabbedPane' and component.parent.__class__.__name__ == 'JSplitPane' :
				return component.getComponentAt(component.indexOfTab('Search'))
				searchPanel = getSearchPanel(component)
				if searchPanel:
					return searchPanel

	def getSearchButton(searchPanel):
		for component in searchPanel.components:
			if component.__class__.__name__ == 'JButton' and component.text == 'Search':
				return component
				searchButton = getSearchButton(component)
				if searchButton:
					return searchButton
	def getSearchTextfield(searchPanel):
		for component in searchPanel.components:
			if component.__class__.__name__ == 'JTextField':
				return component
				searchTextfield = getSearchTextfield(component)
				if searchTextfield:
					return searchTextfield
	def getSearchTree(searchPanel):
		for component in searchPanel.components:
			if component.__class__.__name__ == 'JTree':
				return component
				searchTree = getSearchTree(component)
				if searchTree:
					return searchTree
	# Function that sets the selectedRow of the JTree
	# ...invoked later to give the JTree time to populate
	def selectFirstTreeItem():
		if searchTree.rowCount > 2:
	# Get the search components from the PDF viewer
	searchPanel = getSearchPanel(pdfViewer)
	searchButton = getSearchButton(searchPanel)
	searchTextfield = getSearchTextfield(searchPanel)
	searchTree = getSearchTree(searchPanel)

	# Set the selected index of the panel to the search tab 	
	searchPanel.parent.selectedIndex = searchPanel.parent.indexOfTab('Search')

	# Set the search term and initiate the search using the button's click
	searchTextfield.text = searchTerm
	# Give the JTree a second to get some populated results before selecting the first row
	system.util.invokeLater(selectFirstTreeItem, 1000)

Call it like this:

# Adjust this relative path as needed
pdfViewer = event.source.parent.getComponent('PDF Viewer')

##### OPTIONAL ######
pdfViewer.utilityPaneVisible = True

# Determine this somehow
searchTerm = 'Term'

# Change the script location to match the nested location within the library
# Call the library script and set the search item
libraryScript.setPDFViewerSearchResult(pdfViewer, searchTerm)

Whether or not the search menu is displayed is optional. The script will navigate to the first found result and highlight it regardless of whether or not the utility panel is open.

Edit: Added a line of code to switch the selected tab of the utility panel to the search tab after realizing that it defaults to Bookmarks and afterwards, stays on the last tab selected.


Hello Justin,
Yes, it works perfect for vision. Thanks.
How about the same functionality will be deployed using perspective pdf viewer, any idea Justin?

Hello Joshua,
As you mentioned in Perspective ctrl-F functionality will work when our session is launched in Browser, what if the session launched using perspective workstation?