Skip to main content

TextMatchGroup Class

A group of text matches found in a PDF document.

Declaration

public class TextMatchGroup extends JavaObject

Remarks

The TextMatchGroup class contains a text fragment and matching text ranges associated with this fragment. Use this class to access match information returned by text search operations.

Refer to the following help topic for additional information: Search and Process Text in PDF Documents.

The following code snippet changes the font color and underline color of all matching text fragments:

import com.devexpress.docs.pdf.*;

import java.nio.channels.*;
import java.nio.file.*;

public class Main {
    public static void main(String[] args) throws Exception {
        try (PdfDocument pdfDocument = new PdfDocument(FileChannel.open(
                Path.of("document.pdf")))) {

            TextSearchOptions options = new TextSearchOptions(
                true, // Match case.
                true // Match whole words.
            );

            // Search all pages in the document.
            int pageCount = pdfDocument.getPages().size();
            Iterable<TextSearchInfo> results = pdfDocument.findText(
                    "keyword",
                    options,
                    0,
                    pageCount - 1);

            // Process search results.
            for (TextSearchInfo result : results) {
                for (TextMatchGroup group : result.getGroups()) {

                    // Split the fragment into matching and non-matching parts.
                    TextFragment[][] matched = new TextFragment[1][];
                    TextFragment[][] notMatched = new TextFragment[1][];

                    // Split fragment into matched and non-matched parts.
                    TextFragment[] fragments =
                            group.getFragment().split(
                                    group,
                                    matched, () -> {},
                                    notMatched, () -> {});

                    SolidFill fill = new SolidFill(PdfColor.getRed());

                    // Apply formatting to matching fragments.
                    for (TextFragment[] fragment : matched) {
                        fragment[0].setUnderline(true);
                        fragment[0].setUnderlineFill(fill);
                        fragment[0].setForegroundFill(fill);
                    }

                    // Replace the original fragment.
                    pdfDocument
                            .getPages()
                            .get(result.getPageIndex())
                            .getFragments().
                            replace(group.getFragment(), fragments);
                }
            }

            // Save the document to a PDF file.
            try (WritableByteChannel writableByteChannel =
                FileChannel.open(Path.of("result.pdf"),
                    StandardOpenOption.CREATE,
                    StandardOpenOption.WRITE,
                    StandardOpenOption.TRUNCATE_EXISTING)) {

                pdfDocument.save(writableByteChannel);
            }
        }
    }
}

Inherited Members

com.devexpress.system.JavaObject.clone()
com.devexpress.system.JavaObject.equals(java.lang.Object)
com.devexpress.system.JavaObject.hashCode()
com.devexpress.system.JavaObject.initFields()
com.devexpress.system.JavaObject.memberwiseClone()
com.devexpress.system.JavaObject.toString()
java.lang.Object.finalize()
java.lang.Object.getClass()
java.lang.Object.notify()
java.lang.Object.notifyAll()
java.lang.Object.wait()
java.lang.Object.wait(long)
java.lang.Object.wait(long,int)

Inheritance

Object
com.devexpress.system.JavaObject
TextMatchGroup

Methods

getFragment() Method

Returns the text fragment that contains the matches.

Declaration

public TextFragment getFragment()

Returns

Type Description
TextFragment

A TextFragment object that contains the matched text.

getMatches() Method

Returns text match items.

Declaration

public List<TextMatchGroupItem> getMatches()

Returns

Type Description
java.util.List<TextMatchGroupItem>

The collection of text match items.

getRectangles() Method

Returns rectangles that define the bounds of the matched text.

Declaration

public Iterable<OrientedRectangle> getRectangles()

Returns

Type Description
java.lang.Iterable<OrientedRectangle>

The collection of rectangles that define the bounds of the matched text.