Search
Find text in a PDF with NexPDF for Go and get the page and rectangle of every match.
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:
type Hit struct {
Page int `json:"page"`
Context string `json:"context"`
Llx float64 `json:"llx"`
Lly float64 `json:"lly"`
Urx float64 `json:"urx"`
Ury float64 `json:"ury"`
}
doc, err := nexpdf.Open("contract.pdf")
if err != nil {
log.Fatal(err)
}
defer doc.Close()
s, err := doc.SearchJSON("termination")
if err != nil {
log.Fatal(err)
}
var hits []Hit
json.Unmarshal([]byte(s), &hits)
for _, h := range hits {
fmt.Printf("page %d: %s\n", h.Page+1, h.Context)
}Each hit has:
| Field | Meaning |
|---|---|
page | 0-based page index |
context | The text around the match |
llx, lly, urx, ury | The rectangle of the match in PDF points |
Highlight every match
With the annotations module you can turn the hits into highlight annotations:
for _, h := range hits {
if _, err := doc.AddAnnotation(h.Page, "Highlight", "#FFD400", 1.0, "", h.Llx, h.Lly, h.Urx, h.Ury); err != nil {
log.Fatal(err)
}
}
doc.Save("highlighted.pdf", false)Redact every match
To remove the matches permanently instead, use RedactMatches. See
Redaction.
Scanned documents
Search finds only real text. Run OCR on scanned pages first.