Starting from version 25.4 GroupDocs.Viewer can search the text of a loaded document and highlight what it finds in the rendered output.
What you can search
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).
Search may be performed by entered search term (text literal) or by a regular expression (RegEx).
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:
Load the input document to the Viewer class through its constructor.
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:
usingGroupDocs.Viewer;usingGroupDocs.Viewer.Options;// ...// The search term is a text literalSearchHighlightOptionssearchOptions=newSearchHighlightOptions(){SearchTerm="Buyer",IsSearchTermRegEx=false};using(Viewerviewer=newViewer("contract.docx")){// Search first, then render: the found text is highlighted in the outputviewer.Search(searchOptions);viewer.View(HtmlViewOptions.ForEmbeddedResources());}
ImportsGroupDocs.ViewerImportsGroupDocs.Viewer.Options' ...
' The search term is a text literal
DimsearchOptionsAsNewSearchHighlightOptions()With{.SearchTerm="Buyer",.IsSearchTermRegEx=False}UsingviewerAsNewViewer("contract.docx")' Search first, then render: the found text is highlighted in the output
viewer.Search(searchOptions)viewer.View(HtmlViewOptions.ForEmbeddedResources())EndUsing
contract.docx is the sample file used in this example. Click here to download it.
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:
usingGroupDocs.Viewer;usingGroupDocs.Viewer.Options;// ...// The search term is a regular expressionSearchHighlightOptionssearchOptions=newSearchHighlightOptions(){SearchTerm=@"CF-\d{3}",IsSearchTermRegEx=true};using(Viewerviewer=newViewer("sample.pdf")){// Search first, then render: the found text is highlighted in the outputviewer.Search(searchOptions);viewer.View(newPngViewOptions());}
ImportsGroupDocs.ViewerImportsGroupDocs.Viewer.Options' ...
' The search term is a regular expression
DimsearchOptionsAsNewSearchHighlightOptions()With{.SearchTerm="CF-\d{3}",.IsSearchTermRegEx=True}UsingviewerAsNewViewer("sample.pdf")' Search first, then render: the found text is highlighted in the output
viewer.Search(searchOptions)viewer.View(NewPngViewOptions())EndUsing
sample.pdf is the sample file used in this example. Click here to download it.