Skip to Content
NexPDF

Search

Find text in a PDF with NexPDF for Android and get the page and rectangle of every match.

English

License module

Search is part of the viewer module and works without a key.

searchJson(query) searches every page, case-insensitively, and returns a JSON array of hits:

import com.nexdevit.nexpdf.NexPdfDocument
import org.json.JSONArray

NexPdfDocument.openBytes(bytes).use { doc ->
    val hits = JSONArray(doc.searchJson("termination"))
    for (i in 0 until hits.length()) {
        val hit = hits.getJSONObject(i)
        Log.i("NexPDF", "page ${hit.getInt("page") + 1}: ${hit.getString("context")}")
    }
}

Each hit has:

FieldMeaning
page0-based page index
contextThe text around the match
llx, lly, urx, uryThe rectangle of the match in PDF points

To draw a hit on a rendered page, convert its rectangle with doc.pageView(page).rectToView(k, ...). See Rendering.

Highlight every match

With the annotations module you can turn the hits into highlight annotations:

for (i in 0 until hits.length()) {
    val h = hits.getJSONObject(i)
    NexPdfJni.addAnnotation(doc.handle(), h.getInt("page"), "Highlight", "#FFD400", 1.0, "",
        h.getDouble("llx"), h.getDouble("lly"), h.getDouble("urx"), h.getDouble("ury"))
}
val highlighted = doc.saveBytes()

Redact every match

To remove the matches permanently instead, use NexPdfJni.redactMatches. See Redaction.

Scanned documents

Search finds only real text. Run OCR on scanned pages first.

On this page