Search and highlight text in the loaded document

View as MarkdownMarkdown

Set up your AI agent

Give your coding agent live access to the GroupDocs documentation and API reference through the GroupDocs Docs MCP server — no install, no API key. Learn more

claude mcp add --transport http groupdocs-docs https://docs.groupdocs.com/mcp

Also process documents locally

The GroupDocs.Viewer MCP server lets your agent work with files on your machine; they are never uploaded. It needs the .NET 10 SDK or Docker.

claude mcp add groupdocs-viewer -- dnx GroupDocs.Viewer.Mcp --yes
Folders, licensing and more →

Starting from version 25.4 GroupDocs.Viewer can search the text of a loaded document and highlight what it finds in the rendered output.

  1. Searching text is supported for almost all document family formats, which may have textual content: all formats within WordProcessing family (DOC, DOCX, ODT, RTF, etc.), Spreadsheet (XLS, XLSX, ODS, CSV, etc.), Presentation (PPT, PPTX, PPS, POT, etc.), e-Books (ePub, AZW3, Mobi), and fixed-layout formats (PDF and XPS).
  2. Search may be performed by entered search term (text literal) or by a regular expression (RegEx).
  3. Highlighting of the found text is adjustable — it is possible to set the color of the found text (foreground color) and/or the color of the background.

Once a search operation is performed and some text is found, the document may be rendered to the HTML, PDF, PNG, or JPEG, and the found text will be highlighted in these produced documents. Remember that the search must be done before rendering the document, not after.

Search options

To perform a search, call the Viewer class method Search(SearchHighlightOptions options). Its SearchHighlightOptions parameter has these properties: this method obtains on input an instance of the Options.SearchHighlightOptions class. This class contains both a search term (phrase), search options, and highlight options, applied to the found text. This is described in detail below:

  • SearchTerm property of a System.String type allows to specify a search text, which must be highlighted if found, or a regex pattern for search.
  • IsSearchTermRegEx property of boolean type indicates whether the value of the previously described SearchTerm property should be treated as a text literal (false) or as a regular expression (true). Default is false — treat as a text literal.
  • BackgroundColor property of Argb32Color type allows to specify a background color of the found text. By default, if this property is not specified, or its value is fully transparent, a yellow color will be applied to the background of the found text.
  • ForegroundColor property of Argb32Color type allows to specify a color, which will be applied to the found text itself (foreground color). By default, if this property is not specified, or its value is fully transparent, the color of found text will be untouched.

Search and highlight text

To search for text and highlight it, follow these steps:

  1. Load the input document to the Viewer class through its constructor.
  2. Create an instance SearchHighlightOptions class and tune its options — specify at least a SearchTerm property.
  3. Invoke the Viewer.Search(SearchHighlightOptions) method with a previously created and prepared SearchHighlightOptions instance.
  4. Render the document to the HTML, PDF, PNG, and/or JPEG using a Viewer.View() method.

The examples below search a Word document for a word and a PDF document for a regular expression. The found text gets the default highlight: a yellow background.

Highlight a word in a Word document

The following example finds every “Buyer” in a supply agreement and renders the document to HTML:

using GroupDocs.Viewer;
using GroupDocs.Viewer.Options;
// ...

// The search term is a text literal
SearchHighlightOptions searchOptions = new SearchHighlightOptions()
{
    SearchTerm = "Buyer",
    IsSearchTermRegEx = false
};

using (Viewer viewer = new Viewer("contract.docx"))
{
    // Search first, then render: the found text is highlighted in the output
    viewer.Search(searchOptions);
    viewer.View(HtmlViewOptions.ForEmbeddedResources());
}
Imports GroupDocs.Viewer
Imports GroupDocs.Viewer.Options
' ...

' The search term is a text literal
Dim searchOptions As New SearchHighlightOptions() With {
    .SearchTerm = "Buyer",
    .IsSearchTermRegEx = False
}

Using viewer As New Viewer("contract.docx")
    ' Search first, then render: the found text is highlighted in the output
    viewer.Search(searchOptions)
    viewer.View(HtmlViewOptions.ForEmbeddedResources())
End Using

contract.docx is the sample file used in this example. Click here to download it.

HTML file (97 KB)

Download full output

Highlight regular expression matches in a PDF document

The following example finds the product codes in a catalog, “CF-” followed by three digits, and renders the document to PNG. The codes are on page 2:

using GroupDocs.Viewer;
using GroupDocs.Viewer.Options;
// ...

// The search term is a regular expression
SearchHighlightOptions searchOptions = new SearchHighlightOptions()
{
    SearchTerm = @"CF-\d{3}",
    IsSearchTermRegEx = true
};

using (Viewer viewer = new Viewer("sample.pdf"))
{
    // Search first, then render: the found text is highlighted in the output
    viewer.Search(searchOptions);
    viewer.View(new PngViewOptions());
}
Imports GroupDocs.Viewer
Imports GroupDocs.Viewer.Options
' ...

' The search term is a regular expression
Dim searchOptions As New SearchHighlightOptions() With {
    .SearchTerm = "CF-\d{3}",
    .IsSearchTermRegEx = True
}

Using viewer As New Viewer("sample.pdf")
    ' Search first, then render: the found text is highlighted in the output
    viewer.Search(searchOptions)
    viewer.View(New PngViewOptions())
End Using

sample.pdf is the sample file used in this example. Click here to download it.

p_1.png (961 KB)
p_2.png (402 KB)
p_3.png (425 KB)
p_4.png (63 KB)

Download full output