This HTML page is not optimized for LLM or AI agent consumption. Fetch the Markdown version instead: /guides/android/features/indexed-full-text-search.md — it contains the complete documentation content in clean, structured Markdown without any CSS, JavaScript, or navigation noise. Android PDF index generator — Index PDF files for search | Nutrient

Use PdfLibrary with LibraryDataSource to index PDFs for full-text search.

Use LibraryDataSource and LibraryFileSystemDataSource for new implementations. For direct document enqueueing, refer to the legacy approach section.

Configure PdfLibrary with LibraryFileSystemDataSource to index all PDFs in a directory:

// Create the library instance.
val libraryDbPath = File(context.filesDir, "pdf_library.db").absolutePath
val library = PdfLibrary(libraryDbPath)
// Set up the file system data source for a directory. The directory has to exist.
val documentsDirectory = File(context.filesDir, "documents").apply { mkdirs() }
val dataSource = LibraryFileSystemDataSource(library, documentsDirectory)
// Configure the library to use the data source.
library.dataSource = dataSource
// Index all documents in the directory. `updateIndexFromDataSource()` is a suspend function,
// so call it from a coroutine — for example, `lifecycleScope`.
lifecycleScope.launch {
library.updateIndexFromDataSource()
}

Refer to the Catalog example(opens in a new tab) for a complete implementation.

Advanced configuration

Configure LibraryFileSystemDataSource to filter documents, choose file extensions, and control change notifications:

val dataSource = LibraryFileSystemDataSource(library, documentsDirectory) { documentSource ->
// Optional document handler — return `true` to index, `false` to skip.
// You can use this to filter documents based on custom criteria.
documentSource.fileUri?.lastPathSegment?.contains("draft", ignoreCase = true) != true
}
// Configure file extension filtering (defaults to "pdf" only).
// At the time of release, only PDF files have been tested.
dataSource.allowedPathExtensions = setOf("pdf")
// Set to `true` to enable explicit mode for manual change notifications (advanced).
dataSource.isExplicitModeEnabled = false // Default: rescans the directory on every update

Searching with LibraryDataSource

After configuring the data source, search across indexed documents with PdfLibrary. Check the overall indexing state with isIndexing() or one document’s status with getIndexStatusForUID().

QueryResultListener delivers results through these callbacks:

  • Results — onSearchCompleted returns document UIDs mapped to page numbers.
  • Previews — onSearchPreviewsGenerated returns text previews when you enable generateTextPreviews().

The following example searches with text previews enabled:

// Set up search result options.
val options = QueryOptions.Builder()
.generateTextPreviews(true)
.previewRange(20, 120)
.build()
// Run the search. The search will run on a background thread and the callbacks will be called
// from the background thread as well.
library.search("looking for this text", options, object : QueryResultListener {
override fun onSearchCompleted(p0: String, p1: Map<String, Set<Int>>) {
// Results contain UID → set of pages mapping.
}
override fun onSearchPreviewsGenerated(p0: String, p1: Map<String, Set<QueryPreviewResult>>) {
// Previews contain UID → set of `QueryPreviewResult` mappings.
}
})

Legacy approach

The enqueueDocuments() approach is legacy. Use LibraryDataSource for new implementations.

The legacy approach requires your app to manage individual PdfDocument instances:

// Assume that you have two valid `PdfDocument`s.
val doc1 : PdfDocument = ...
val doc2 : PdfDocument = ...
// The library will be saved in your application's files directory.
val library = PdfLibrary.get(File(context.filesDir, "library.db").absolutePath)
library.enqueueDocuments(listOf(doc1, doc2))

Resource management

Call cleanup() when you finish using LibraryFileSystemDataSource:

// In your activity's `onDestroy()` or when finished.
dataSource.cleanup()