Search and highlight text in the loaded document
Leave feedback
Starting from version 25.4 the GroupDocs.Viewer is able to search the text spans in the loaded documents and highlight the found text. This feature can be described with the next three basic statements:
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. Just need to remember that search must be done before rendering the document, not after.
In order to perform a search a new public instance method was added to the Viewer class — void Search(Options.SearchHighlightOptions options). As it is seen from its signature, 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.
Summing up, in order to search for some text and highlight it using GroupDocs.Viewer, need to perform the next four 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.
Code sample below shows loading two distinct documents — DOCX and PDF, and performing search in them using text literal and RegEx. Finally, documents are saved to HTML and PNG.
usingGroupDocs.Viewer;usingGroupDocs.Viewer.Options;// ...// Creating two instances of SearchHighlightOptions and adjusting themSearchHighlightOptionsbyLiteral=newSearchHighlightOptions(){SearchTerm="some text",IsSearchTermRegEx=false};SearchHighlightOptionsbyRegEx=newSearchHighlightOptions(){SearchTerm=@"text\s*#(\d+)",IsSearchTermRegEx=true};// Loading two documents to the two instances of Viewer classusing(ViewerdocxViewer=newViewer("sample.docx"))using(ViewerpdfViewer=newViewer("sample.pdf")){// Performing search in both Viewer instancesdocxViewer.Search(byLiteral);pdfViewer.Search(byRegEx);// Rendering both documents - 1st to HTML and 2nd to PNGdocxViewer.View(HtmlViewOptions.ForEmbeddedResources());pdfViewer.View(newPngViewOptions());}
ImportsGroupDocs.ViewerImportsGroupDocs.Viewer.Options' ...
' Creating two instances of SearchHighlightOptions and adjusting them
DimbyLiteralAsNewSearchHighlightOptions()With{.SearchTerm="some text",.IsSearchTermRegEx=False}DimbyRegExAsNewSearchHighlightOptions()With{.SearchTerm="text\s*#(\d+)",.IsSearchTermRegEx=True}' Loading two documents to the two instances of Viewer class
UsingdocxViewerAsNewViewer("sample.docx"),pdfViewerAsNewViewer("sample.pdf")' Performing search in both Viewer instances
docxViewer.Search(byLiteral)pdfViewer.Search(byRegEx)'Rendering both documents - 1st to HTML and 2nd to PNG
docxViewer.View(HtmlViewOptions.ForEmbeddedResources())pdfViewer.View(NewPngViewOptions())EndUsing
Was this page helpful?
Any additional feedback you'd like to share with us?
Please tell us how we can improve this page.
Thank you for your feedback!
We value your opinion. Your feedback will help us improve our documentation.
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.