Skip to Content
NexPDF

Search

Find text in a PDF with NexPDF for .NET 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:

using System.Text.Json.Nodes;
using Nexdevit.NexPdf;

using var doc = NexPdfDocument.Open("contract.pdf");
foreach (var hit in JsonNode.Parse(doc.SearchJson("termination"))!.AsArray())
    Console.WriteLine($"page {hit!["page"]!.GetValue<int>() + 1}: {hit["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 deserialize into your own type instead, use JsonSerializer.Deserialize with JsonNamingPolicy.SnakeCaseLower or [JsonPropertyName] attributes.

Highlight every match

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

foreach (var hit in JsonNode.Parse(doc.SearchJson("termination"))!.AsArray())
{
    double N(string k) => hit![k]!.GetValue<double>();
    doc.AddAnnotation(hit!["page"]!.GetValue<int>(), "Highlight", "#FFD400", 1.0, "",
        N("llx"), N("lly"), N("urx"), N("ury"));
}
doc.Save("highlighted.pdf");

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.

On this page