Method HighlightAsync
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
inputPathstringPath to the input document (PDF or image).
querystringThe search text or pattern. For Text, an exact substring. For Regex, a .NET regular expression pattern. For Fuzzy, the approximate text to locate.
optionsSearchHighlightOptionsSearch and highlight options. When
null, default options are used (text mode, case-insensitive, semi-transparent yellow highlight).cancellationTokenCancellationTokenCancellation 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
inputPathisnull.- FileNotFoundException
The specified file does not exist.
- ArgumentException
queryisnullor 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
attachmentAttachmentThe source document (PDF or image). Must not be
null.querystringThe search text or pattern. For Text, an exact substring. For Regex, a .NET regular expression pattern. For Fuzzy, the approximate text to locate.
optionsSearchHighlightOptionsSearch and highlight options. When
null, default options are used.pageElementsIReadOnlyList<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.
cancellationTokenCancellationTokenCancellation 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
attachmentisnull.- ArgumentException
queryisnullor whitespace.