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

SQL Server
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    --  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

    DECLARE @pdf int
    EXEC @hr = sp_OACreate 'Chilkat.Pdf', @pdf OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    EXEC sp_OAMethod @pdf, 'LoadFile', @success OUT, 'qa_data/pdf/invoice_labels.pdf'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @pdf, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @pdf
        RETURN
      END

    DECLARE @json int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT

    DECLARE @jsonOut int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jsonOut OUT

    --  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.
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, '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.)
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'labelValue', 'right'

    EXEC sp_OAMethod @pdf, 'ExtractText', @success OUT, 'text', @json, @jsonOut
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @pdf, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @pdf
        EXEC @hr = sp_OADestroy @json
        EXEC @hr = sp_OADestroy @jsonOut
        RETURN
      END

    --  The value is returned as the text.

    EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp0 OUT, 'text'
    PRINT 'Invoice Number: ' + @sTmp0

    --  The labelMatches array tells where the label was found and how it appeared in the PDF.

    EXEC sp_OAMethod @jsonOut, 'IntOf', @iTmp0 OUT, 'labelMatches[0].page'

    EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp0 OUT, 'labelMatches[0].label'

    PRINT 'Found on page ' + @iTmp0 + ' as "' + @sTmp0 + '"'

    --  Matching ignores case and the trailing colon, so these all work:
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'label', 'CUSTOMER:'
    EXEC sp_OAMethod @pdf, 'ExtractText', @success OUT, 'text', @json, @jsonOut
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @pdf, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @pdf
        EXEC @hr = sp_OADestroy @json
        EXEC @hr = sp_OADestroy @jsonOut
        RETURN
      END

    EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp0 OUT, 'text'
    PRINT 'Customer: ' + @sTmp0

    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'label', 'due date'
    EXEC sp_OAMethod @pdf, 'ExtractText', @success OUT, 'text', @json, @jsonOut
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @pdf, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @pdf
        EXEC @hr = sp_OADestroy @json
        EXEC @hr = sp_OADestroy @jsonOut
        RETURN
      END

    EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp0 OUT, 'text'
    PRINT 'Due Date: ' + @sTmp0

    --  The totals at the foot of the invoice are right-aligned labels with the amount beside them.
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'label', 'Total Due'
    EXEC sp_OAMethod @pdf, 'ExtractText', @success OUT, 'text', @json, @jsonOut
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @pdf, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @pdf
        EXEC @hr = sp_OADestroy @json
        EXEC @hr = sp_OADestroy @jsonOut
        RETURN
      END

    EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp0 OUT, 'text'
    PRINT 'Total Due: ' + @sTmp0

    --  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".
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'label', 'Tracking Number'
    EXEC sp_OAMethod @pdf, 'ExtractText', @success OUT, 'text', @json, @jsonOut
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @pdf, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @pdf
        EXEC @hr = sp_OADestroy @json
        EXEC @hr = sp_OADestroy @jsonOut
        RETURN
      END
    EXEC sp_OAMethod @jsonOut, 'SizeOfArray', @iTmp0 OUT, 'labelMatches'
    IF @iTmp0 = 0
      BEGIN

        EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp0 OUT, 'quality.pages[0].issues[0].code'
        PRINT 'Tracking Number: (not found) ' + @sTmp0
      END

    --  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

    EXEC @hr = sp_OADestroy @pdf
    EXEC @hr = sp_OADestroy @json
    EXEC @hr = sp_OADestroy @jsonOut


END
GO