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

Get Every Occurrence of a Label in a PDF

See more PDF Text Extraction Examples

Demonstrates the "labelOccurrence" option. When the same label appears more than once (for example "Total:" on each of several receipts), "first" (the default) uses the first occurrence, "all" returns every occurrence with its page, and a number selects one occurrence.

Chilkat Classic ASP Downloads

Classic ASP
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<%
success = 0

'  This example requires the Chilkat API to have been previously unlocked.
'  See Global Unlock Sample for sample code.

'  The PDF used in this example (three receipts, one per page) is available at three_receipts.pdf

set pdf = Server.CreateObject("Chilkat.Pdf")
success = pdf.LoadFile("qa_data/pdf/three_receipts.pdf")
If (success = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( pdf.LastErrorText) & "</pre>"
    Response.End
End If

set json = Server.CreateObject("Chilkat.JsonObject")
set jsonOut = Server.CreateObject("Chilkat.JsonObject")

'  Each of the three pages is a receipt with a "Total:" line.
success = json.UpdateString("label","Total")
success = json.UpdateString("labelValue","right")

'  By default only the first occurrence (in reading order) is used.
success = pdf.ExtractText("text",json,jsonOut)
If (success = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( pdf.LastErrorText) & "</pre>"
    Response.End
End If

Response.Write "<pre>" & Server.HTMLEncode( "First total: " & jsonOut.StringOf("text")) & "</pre>"

'  labelOccurrence "all" returns every occurrence.  The "text" member holds the values joined
'  by line breaks, and labelMatches has one entry per occurrence with its page.
success = json.UpdateString("labelOccurrence","all")
success = pdf.ExtractText("text",json,jsonOut)
If (success = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( pdf.LastErrorText) & "</pre>"
    Response.End
End If

i = 0
count_i = jsonOut.SizeOfArray("labelMatches")
Do While i < count_i
    jsonOut.I = i
    Response.Write "<pre>" & Server.HTMLEncode( "Page " & jsonOut.IntOf("labelMatches[i].page") & " total: " & jsonOut.StringOf("labelMatches[i].text")) & "</pre>"
    i = i + 1
Loop

'  A number selects one occurrence: 2 is the second.
success = json.UpdateInt("labelOccurrence",2)
success = pdf.ExtractText("text",json,jsonOut)
If (success = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( pdf.LastErrorText) & "</pre>"
    Response.End
End If

Response.Write "<pre>" & Server.HTMLEncode( "Second total: " & jsonOut.StringOf("text")) & "</pre>"

'  The occurrences are counted over the selected pages, so combining "pages" with the label
'  is another way to pick one receipt:
success = json.UpdateString("labelOccurrence","first")
success = json.UpdateString("pages","3")
success = json.UpdateString("label","Receipt No.")
success = pdf.ExtractText("text",json,jsonOut)
If (success = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( pdf.LastErrorText) & "</pre>"
    Response.End
End If

Response.Write "<pre>" & Server.HTMLEncode( "Receipt number on page 3: " & jsonOut.StringOf("text")) & "</pre>"

'  Sample output:

'  First total: $19.86
'  Page 1 total: $19.86
'  Page 2 total: $17.34
'  Page 3 total: $23.62
'  Second total: $17.34
'  Receipt number on page 3: 1003

%>
</body>
</html>