Sample code for 30+ languages & platforms
Java Requires Chilkat v11.7.0+

Search a PDF with a Regular Expression

See more PDF Text Extraction Examples

Demonstrates searching a PDF with a regular expression (regex: true), limiting the number of results (maxResults), and matching whole words only (wholeWord).

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.Pdf;

public class ChilkatExample {

    public static void main(String[] args) {
        // This example requires the Chilkat API to have been previously unlocked.
        // See Global Unlock Sample for sample code.

        // The PDF used in this example is available at order_records.pdf

        Pdf pdf = new Pdf();
        try {
            pdf.loadFile("qa_data/pdf/order_records.pdf");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        JsonObject json = new JsonObject();
        JsonObject jsonOut = new JsonObject();

        // Find every order number.  With regex set to true, the query is a regular expression.
        json.updateString("query", "ORD-[0-9]{4}-[0-9]{4}");
        json.updateBool("regex", true);
        // We only want the matched text, so no context is needed.
        json.updateInt("context", 0);

        try {
            pdf.extractText("search", json, jsonOut);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        System.out.println("Order numbers found: " + jsonOut.intOf("count"));
        int i = 0;
        int count_i = jsonOut.sizeOfArray("matches");
        while (i < count_i) {
            jsonOut.setI(i);
            System.out.println(jsonOut.stringOf("matches[i].text"));
            i = i + 1;
        }

        // Find the e-mail addresses, but stop after the first three.
        json.updateString("query", "[a-z0-9._]+@[a-z]+[.][a-z]+");
        json.updateInt("maxResults", 3);
        try {
            pdf.extractText("search", json, jsonOut);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        System.out.println("First three e-mail addresses:");
        i = 0;
        count_i = jsonOut.sizeOfArray("matches");
        while (i < count_i) {
            jsonOut.setI(i);
            System.out.println(jsonOut.stringOf("matches[i].text"));
            i = i + 1;
        }

        // wholeWord: true matches only whole words.  The word "cat" appears once in the PDF,
        // but it is also inside "catalog" and "category".
        json.delete("maxResults");
        json.updateBool("regex", false);
        json.updateString("query", "cat");
        try {
            pdf.extractText("search", json, jsonOut);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        System.out.println("Occurrences of cat (any): " + jsonOut.intOf("count"));

        json.updateBool("wholeWord", true);
        try {
            pdf.extractText("search", json, jsonOut);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        System.out.println("Occurrences of cat (whole word): " + jsonOut.intOf("count"));

        // Sample output:

        // Order numbers found: 9
        // ORD-2026-0301
        // ORD-2026-0302
        // ORD-2026-0303
        // ORD-2026-0304
        // ORD-2026-0305
        // ORD-2026-0306
        // ORD-2026-0307
        // ORD-2026-0308
        // ORD-2026-0307
        // First three e-mail addresses:
        // alice.moore@example.com
        // bkumar@example.org
        // carla@example.net
        // Occurrences of cat (any): 3
        // Occurrences of cat (whole word): 1
    }
}