Sample code for 30+ languages & platforms
C 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 C Downloads

C
#include <C_CkPdf.h>
#include <C_CkJsonObject.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkPdf pdf;
    HCkJsonObject json;
    HCkJsonObject jsonOut;

    success = FALSE;

    //  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 = CkPdf_Create();
    success = CkPdf_LoadFile(pdf,"qa_data/pdf/invoice_labels.pdf");
    if (success == FALSE) {
        printf("%s\n",CkPdf_lastErrorText(pdf));
        CkPdf_Dispose(pdf);
        return;
    }

    json = CkJsonObject_Create();
    jsonOut = CkJsonObject_Create();

    //  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.
    CkJsonObject_UpdateString(json,"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.)
    CkJsonObject_UpdateString(json,"labelValue","right");

    success = CkPdf_ExtractText(pdf,"text",json,jsonOut);
    if (success == FALSE) {
        printf("%s\n",CkPdf_lastErrorText(pdf));
        CkPdf_Dispose(pdf);
        CkJsonObject_Dispose(json);
        CkJsonObject_Dispose(jsonOut);
        return;
    }

    //  The value is returned as the text.
    printf("Invoice Number: %s\n",CkJsonObject_stringOf(jsonOut,"text"));

    //  The labelMatches array tells where the label was found and how it appeared in the PDF.
    printf("Found on page %d as \"%s\"\n",CkJsonObject_IntOf(jsonOut,"labelMatches[0].page"),CkJsonObject_stringOf(jsonOut,"labelMatches[0].label"));

    //  Matching ignores case and the trailing colon, so these all work:
    CkJsonObject_UpdateString(json,"label","CUSTOMER:");
    success = CkPdf_ExtractText(pdf,"text",json,jsonOut);
    if (success == FALSE) {
        printf("%s\n",CkPdf_lastErrorText(pdf));
        CkPdf_Dispose(pdf);
        CkJsonObject_Dispose(json);
        CkJsonObject_Dispose(jsonOut);
        return;
    }

    printf("Customer: %s\n",CkJsonObject_stringOf(jsonOut,"text"));

    CkJsonObject_UpdateString(json,"label","due date");
    success = CkPdf_ExtractText(pdf,"text",json,jsonOut);
    if (success == FALSE) {
        printf("%s\n",CkPdf_lastErrorText(pdf));
        CkPdf_Dispose(pdf);
        CkJsonObject_Dispose(json);
        CkJsonObject_Dispose(jsonOut);
        return;
    }

    printf("Due Date: %s\n",CkJsonObject_stringOf(jsonOut,"text"));

    //  The totals at the foot of the invoice are right-aligned labels with the amount beside them.
    CkJsonObject_UpdateString(json,"label","Total Due");
    success = CkPdf_ExtractText(pdf,"text",json,jsonOut);
    if (success == FALSE) {
        printf("%s\n",CkPdf_lastErrorText(pdf));
        CkPdf_Dispose(pdf);
        CkJsonObject_Dispose(json);
        CkJsonObject_Dispose(jsonOut);
        return;
    }

    printf("Total Due: %s\n",CkJsonObject_stringOf(jsonOut,"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".
    CkJsonObject_UpdateString(json,"label","Tracking Number");
    success = CkPdf_ExtractText(pdf,"text",json,jsonOut);
    if (success == FALSE) {
        printf("%s\n",CkPdf_lastErrorText(pdf));
        CkPdf_Dispose(pdf);
        CkJsonObject_Dispose(json);
        CkJsonObject_Dispose(jsonOut);
        return;
    }

    if (CkJsonObject_SizeOfArray(jsonOut,"labelMatches") == 0) {
        printf("Tracking Number: (not found) %s\n",CkJsonObject_stringOf(jsonOut,"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


    CkPdf_Dispose(pdf);
    CkJsonObject_Dispose(json);
    CkJsonObject_Dispose(jsonOut);

    }