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

Get a PDF Field Value by its Label (Value to the Right)

See more PDF Text Extraction Examples

Demonstrates the "label" option, which captures the text that belongs to a caption such as "Invoice Number:" without needing coordinates. With labelValue "right", the value is the rest of the label's line. Label matching ignores case, extra spaces and a trailing colon.

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 invoice used in this example is available at invoice_labels.pdf

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

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

        // The invoice has lines such as "Invoice Number:   INV-2026-0412".
        // The label option restricts the "text" operation to the text belonging to the label.
        json.updateString("label", "Invoice Number");

        // "right" means the value is the rest of the label's line.
        // (The default, "auto", uses the rest of the line if there is text there, otherwise the block below.)
        json.updateString("labelValue", "right");

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

        // The value is returned as the text.
        System.out.println("Invoice Number: " + jsonOut.stringOf("text"));

        // The labelMatches array tells where the label was found and how it appeared in the PDF.
        System.out.println("Found on page " + jsonOut.intOf("labelMatches[0].page") + " as \"" + jsonOut.stringOf("labelMatches[0].label") + "\"");

        // Matching ignores case and the trailing colon, so these all work:
        json.updateString("label", "CUSTOMER:");
        try {
            pdf.extractText("text", json, jsonOut);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        System.out.println("Customer: " + jsonOut.stringOf("text"));

        json.updateString("label", "due date");
        try {
            pdf.extractText("text", json, jsonOut);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        System.out.println("Due Date: " + jsonOut.stringOf("text"));

        // The totals at the foot of the invoice are right-aligned labels with the amount beside them.
        json.updateString("label", "Total Due");
        try {
            pdf.extractText("text", json, jsonOut);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        System.out.println("Total Due: " + jsonOut.stringOf("text"));

        // A label that is not found is not an error.  The text is empty, labelMatches is empty,
        // and the quality report has an issue with the code "labelNotFound".
        json.updateString("label", "Tracking Number");
        try {
            pdf.extractText("text", json, jsonOut);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        if (jsonOut.sizeOfArray("labelMatches") == 0) {
            System.out.println("Tracking Number: (not found) " + jsonOut.stringOf("quality.pages[0].issues[0].code"));
        }

        // Sample output:

        // Invoice Number: INV-2026-0412
        // Found on page 1 as "Invoice Number:"
        // Customer: Northwind Traders
        // Due Date: April 13, 2026
        // Total Due: $1,234.53
        // Tracking Number: (not found) labelNotFound
    }
}