Skip to main content

TextMatchFragment Class

A fragment of text that matches a search query in a PDF document.

Declaration

public class TextMatchFragment extends JavaObject

Remarks

The TextMatchFragment class stores the matched text, its position within the source TextFragment, and the rectangle that defines the location of the match on the page.

Use this class to access information about individual text matches returned by text search operations.

The following code snippet searches for text in a PDF document and uses TextMatchFragment methods to retrieve the matched text and its location on the page:

package pdf;

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 (FileChannel fileChannel = FileChannel.open(Path.of("Document.pdf"));
             PdfDocument pdfDocument = new PdfDocument(fileChannel)) {

            pdfDocument.getPages().add(pdfDocument.getPages().getFirst().deepClone());

            // Configure text search options.
            TextSearchOptions searchOptions = new TextSearchOptions(true, true);

            // Search for text in the document.
            Iterable<TextSearchInfo> results = pdfDocument.findText("document");

            for (TextSearchInfo result : results) {

                // Process text matches on the current page.
                for (TextMatchInfo match : result.getMatches()) {

                    // A text match may consist of multiple text fragments.
                    for (TextMatchFragment fragment : match.getMatchFragments()) {

                        // Get the matched text.
                        String text = fragment.getText();

                        // Get the location of the matched text.
                        OrientedRectangle bounds = fragment.getRectangle();

                        System.out.println("Match: " + text);
                        System.out.println(
                            "Location: " + "Page " + result.getPageIndex() + ", " + bounds.getLeft() + ", " + bounds.getTop()
                        );
                    }
                }
            }
        }
    }
}

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

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
TextMatchFragment

Methods

getFragment() Method

Returns the source text fragment that contains the match.

Declaration

public TextFragment getFragment()

Returns

Type Description
TextFragment

The text fragment that contains the matched text.

getLength() Method

Returns the length of the matched text.

Declaration

public int getLength()

Returns

Type Description
int

The number of characters in the matched text.

getOffset() Method

Returns the starting position of the matched text within the source text fragment.

Declaration

public int getOffset()

Returns

Type Description
int

The zero-based offset of the matched text.

getRectangle() Method

Returns the rectangle that defines the location of the matched fragment.

Declaration

public OrientedRectangle getRectangle()

Returns

Type Description
OrientedRectangle

The oriented rectangle that defines the match bounds.

getText() Method

Returns the text content of the matched fragment.

Declaration

public String getText()

Returns

Type Description
String

The text content of the matched fragment.