Table of Contents

Method HighlightAsync

Namespace
LMKit.Document.Search
Assembly
LM-Kit.NET.dll

HighlightAsync(string, string, SearchHighlightOptions, CancellationToken)

Asynchronously searches text in a document file and returns a highlighted copy.

public static Task<SearchHighlightResult> HighlightAsync(string inputPath, string query, SearchHighlightOptions options = null, CancellationToken cancellationToken = default)

Parameters

inputPath string

Path to the input document (PDF or image).

query string

The search text or pattern. For Text, an exact substring. For Regex, a .NET regular expression pattern. For Fuzzy, the approximate text to locate.

options SearchHighlightOptions

Search and highlight options. When null, default options are used (text mode, case-insensitive, semi-transparent yellow highlight).

cancellationToken CancellationToken

Cancellation token.

Returns

Task<SearchHighlightResult>

A task containing a SearchHighlightResult with the highlighted document and match metadata.

Examples

using LMKit.Document.Search;

// Asynchronously search a PDF for a regex pattern and save the result.
var options = new SearchHighlightOptions
{
    SearchMode = SearchMode.Regex,
    PageRange = "1-5"
};

SearchHighlightResult result = await SearchHighlightEngine.HighlightAsync(
    "contract.pdf",
    @"\b\d{1,2}/\d{1,2}/\d{4}\b",
    options);

Console.WriteLine($"Found {result.TotalMatches} date(s) in pages 1-5.");
await File.WriteAllBytesAsync("contract_dates_highlighted.pdf", result.OutputData);

Exceptions

ArgumentNullException

inputPath is null.

FileNotFoundException

The specified file does not exist.

ArgumentException

query is null or whitespace.

HighlightAsync(Attachment, string, SearchHighlightOptions, IReadOnlyList<PageElement>, CancellationToken)

Asynchronously searches text in an Attachment and returns a highlighted copy.

public static Task<SearchHighlightResult> HighlightAsync(Attachment attachment, string query, SearchHighlightOptions options = null, IReadOnlyList<PageElement> pageElements = null, CancellationToken cancellationToken = default)

Parameters

attachment Attachment

The source document (PDF or image). Must not be null.

query string

The search text or pattern. For Text, an exact substring. For Regex, a .NET regular expression pattern. For Fuzzy, the approximate text to locate.

options SearchHighlightOptions

Search and highlight options. When null, default options are used.

pageElements IReadOnlyList<PageElement>

Optional pre-computed page elements indexed by zero-based page index. When a non-null entry exists for a given page, it is used for search instead of the attachment's internal text extraction. This enables highlighting on raster PDFs or images whose text was obtained via prior OCR or layout analysis.

cancellationToken CancellationToken

Cancellation token.

Returns

Task<SearchHighlightResult>

A task containing a SearchHighlightResult with the highlighted document and match metadata.

Examples

using LMKit.Data;
using LMKit.Document.Search;

using var attachment = new Attachment("handbook.pdf");

var options = new SearchHighlightOptions
{
    SearchMode = SearchMode.Fuzzy,
    MaxEditDistance = 1,
    PageRange = "1-10"
};

SearchHighlightResult result = await SearchHighlightEngine.HighlightAsync(
    attachment,
    "compliance",
    options);

Console.WriteLine($"Found {result.TotalMatches} match(es).");
await File.WriteAllBytesAsync("handbook_highlighted.pdf", result.OutputData);

Exceptions

ArgumentNullException

attachment is null.

ArgumentException

query is null or whitespace.

Share