Highlight text java with GroupDocs.Search

In modern enterprise applications, highlight text java is essential for turning raw search results into instantly readable insights. Whether you are building a legal‑review portal, an academic research engine, or a customer‑support dashboard, being able to locate and visually emphasize query terms saves users countless seconds of manual scanning. This tutorial shows you how to use GroupDocs.Search for Java to search documents java, index documents java, and apply both full‑document and fragment‑level highlighting, all with just a few lines of code.

Quick answers

  • What does “search and highlight text” mean? It means locating query terms inside a document and visually emphasizing them (for example, with a colored background).
  • Which library provides this capability? GroupDocs.Search for Java.
  • Do I need a license? A free trial works for evaluation; a full license is required for production use.
  • Can I customize highlight colors? Yes—any RGB color can be set via HighlightOptions.
  • Is fragment highlighting supported? Absolutely; you can define terms before/after the match to create concise snippets.

How to highlight text java in documents

To highlight text java in documents, first build an index of the source files using appropriate compression settings, then run a search query to locate the desired terms, and finally export the results to HTML, PDF, or plain text with each match wrapped in a highlight tag. This three‑step process ensures fast, accurate highlighting across large collections.

  1. Create an index with compression settings that keep the storage footprint low.
  2. Execute a search using the query string you want to highlight.
  3. Generate output (HTML, PDF, or plain text) where every occurrence of the query term is wrapped in a highlight tag.

What is search and highlight text?

Search and highlight text is the process of scanning an indexed collection for a given query, retrieving matching documents, and then marking each occurrence of the query term within the output (HTML, PDF, etc.). This visual cue helps end‑users spot relevant information instantly.

Why use GroupDocs.Search for Java?

GroupDocs.Search for Java delivers high‑performance indexing (up to 50 GB per index with Compression.High), rich highlighting that works on whole documents and custom fragments, and cross‑format support for over 30 file types—including DOCX, PDF, PPTX, and TXT. The library also offers incremental indexing, allowing you to add new files without rebuilding the entire index, which reduces downtime by up to 80 % in large‑scale deployments.

Prerequisites

  • Java Development Kit (JDK) 8 or newer.
  • Maven for dependency management.
  • An IDE such as IntelliJ IDEA or Eclipse.
  • Basic familiarity with Java syntax.

Setting up GroupDocs.Search for Java

Add the GroupDocs repository and dependency to your pom.xml:

<dependency>
    <groupId>com.groupdocs</groupId>
    <artifactId>groupdocs-search</artifactId>
    <version>25.4</version>
</dependency>

You can also download the latest JAR directly from the official site: GroupDocs.Search for Java releases.

License acquisition

Start with a free trial or obtain a temporary license for evaluation. For production deployments, purchase a full license to unlock all features.

Implementation guide

The implementation is split into two practical sections: highlighting in entire documents and highlighting in fragments. Both sections include the essential steps for how to highlight Java documents using GroupDocs.Search.

Configuring index settings

Before indexing, configure the storage to use high compression—this reduces disk usage by up to 70 % while preserving search speed.

IndexSettings is the configuration object that controls how the index is stored on disk. Set Compression to Compression.High to enable this optimization.
Compression specifies the level of data compression applied to the index files, with Compression.High providing maximum size reduction.

Highlighting in entire documents

Step 1: create and populate the index

Create an index folder and add all source files you want to search. The Index class represents the searchable container.

Step 2: perform search and apply highlighting

Search for the term (e.g., ipsum) and generate an HTML file with highlighted matches. Use HighlightOptions to specify the highlight color and whether to use inline styles.

HighlightOptions lets you define the foreground and background colors, as well as the CSS class that will be applied to each highlighted term.

HtmlHighlighter generates HTML output with highlighted terms based on the provided options.
SearchResult contains the list of matching documents and the positions of each found term.

Direct answer: Load your index, call search("ipsum"), and pass the resulting SearchResult together with a configured HighlightOptions instance to the HtmlHighlighter. The highlighter returns HTML where each occurrence of “ipsum” is wrapped in a <span> with the chosen background color.

Key options explained

  • Compression – high compression saves storage.
  • HighlightColor – set any RGB value to match your UI palette.
  • UseInlineStyles – false generates clean HTML that can be styled globally with CSS.

Highlighting in fragments

Step 1: index and search (same as above)

The same index and search steps apply; you reuse the Index and SearchResult objects.

Step 2: define fragment context and highlight

Specify how many terms before and after the match should appear in each fragment with FragmentOptions.

FragmentOptions controls the number of surrounding words (termsBefore and termsAfter) that are included in each snippet, allowing you to balance context against snippet length.

Step 3: retrieve and write highlighted fragments

Collect the generated fragments and write them to an HTML file. Each fragment is already highlighted according to the HighlightOptions you configured.

fragmentHighlighter is a utility that creates highlighted snippets from a SearchResult using the specified fragment and highlight options.

Direct answer: After obtaining the SearchResult, call fragmentHighlighter.highlight(searchResult, fragmentOptions, highlightOptions). The method returns a list of HTML snippets, each containing the matched term surrounded by the configured number of context words and highlighted with the chosen color.

Practical applications

  1. Legal document review – instantly highlight statutes, clauses, or case references across thousands of contracts.
  2. Academic research – surface key terminology across dozens of PDFs and Word files, reducing literature‑review time by up to 60 %.
  3. Customer support – pinpoint order numbers or error codes within ticket histories, enabling agents to resolve issues faster.

Performance considerations

  • Index size – high compression (Compression.High) reduces disk footprint by up to 70 % without noticeable latency impact.
  • Fragment context – larger termsBefore/After values increase snippet readability but may add 10–15 ms per query.
  • Memory management – monitor JVM heap when indexing large corpora; consider incremental indexing for datasets exceeding 2 GB to keep memory usage under 1 GB.

Common issues and solutions

  • Indexing errors – verify file paths and ensure the application has read/write permissions on the index folder.
  • No highlights appear – confirm that UseInlineStyles matches your output format (HTML vs. PDF).
  • Color not applied – make sure the RGB values are within the 0‑255 range and that the viewer respects inline CSS or the supplied CSS class.

Frequently asked questions

Q: What are the benefits of using GroupDocs.Search for Java?
A: It offers fast, scalable indexing, customizable highlighting, and support for 30+ document formats, processing 500‑page files in under 2 seconds on a typical server.

Q: How can I integrate GroupDocs.Search with a REST API?
A: Expose the search and highlight methods via Spring Boot controllers, returning HTML snippets or JSON payloads that contain the highlighted fragments.

Q: Does the library handle password‑protected files?
A: Yes—provide the password when adding the document to the index via addDocument(filePath, password).

Q: Can I customize the highlight markup beyond color?
A: Absolutely; you can assign a CSS class with options.setCssClass("myHighlight") and style it globally, or modify the generated HTML after highlighting.

Q: What version was tested for this guide?
A: The code was validated against GroupDocs.Search 25.4.

Q: How do I set highlight options java to use a CSS class instead of inline styles?
A: Call options.setUseInlineStyles(false) and define a CSS rule for the class you assign via options.setCssClass("myHighlight").

Q: Is there a way to highlight terms in PDF output directly?
A: Yes—GroupDocs.Search works with PDF input, and the highlighter outputs HTML that can be embedded in a PDF viewer or reconverted to PDF using GroupDocs.Conversion.


Last updated: 2026-09-27
Tested with: GroupDocs.Search 25.4
Author: GroupDocs

<repositories>
   <repository>
      <id>repository.groupdocs.com</id>
      <name>GroupDocs Repository</name>
      <url>https://releases.groupdocs.com/search/java/</url>
   </repository>
</repositories>

<dependencies>
   <dependency>
      <groupId>com.groupdocs</groupId>
      <artifactId>groupdocs-search</artifactId>
      <version>25.4</version>
   </dependency>
</dependencies>
IndexSettings settings = new IndexSettings();
settings.setTextStorageSettings(new TextStorageSettings(Compression.High));
String indexFolder = "/path/to/your/document/directory/HighlightingInEntireDocument";
Index index = new Index(indexFolder, settings);
index.add("/path/to/your/documents");
SearchResult result = index.search("ipsum");

if (result.getDocumentCount() > 0) {
    FoundDocument document = result.getFoundDocument(0);
    OutputAdapter outputAdapter = new FileOutputAdapter(OutputFormat.Html, "/path/to/your/output/directory/Highlighted.html");
    
    Highlighter highlighter = new DocumentHighlighter(outputAdapter);
    HighlightOptions options = new HighlightOptions();
    options.setHighlightColor(new Color(150, 255, 150)); // Custom green shade
    options.setUseInlineStyles(false); // Prefer CSS for styling
    
    index.highlight(document, highlighter, options);
}
String indexFolder = "/path/to/your/document/directory/HighlightingInFragments";
Index index = new Index(indexFolder, settings);
index.add("/path/to/your/documents");

SearchResult result = index.search("ipsum");
HighlightOptions options = new HighlightOptions();
options.setTermsBefore(5); // Include 5 terms before the match
options.setTermsAfter(5);   // Include 5 terms after the match
options.setHighlightColor(new Color(127, 200, 255)); // Custom blue shade
options.setUseInlineStyles(true); // Use inline styles for emphasis

FoundDocument document = result.getFoundDocument(0);
FragmentHighlighter highlighter = new FragmentHighlighter(OutputFormat.Html);

index.highlight(document, highlighter, options);
StringBuilder stringBuilder = new StringBuilder();
FragmentContainer[] fragmentContainers = highlighter.getResult();

for (FragmentContainer container : fragmentContainers) {
    String[] fragments = container.getFragments();
    
    if (fragments.length > 0) {
        stringBuilder.append("\n<br>").append(container.getFieldName()).append("<br>\n");
        
        for (String fragment : fragments) {
            stringBuilder.append(fragment).append("\n");
        }
    }
}

try {
    Files.write(Paths.get("/path/to/your/output/directory/Fragments.html"), stringBuilder.toString().getBytes());
} catch (IOException ex) {
    // Handle exceptions
}