Xbase++ Requires Chilkat v11.7.0+
Xbase++
Extract the Values of PDF Form Fields
See more PDF Text Extraction Examples
Demonstrates the "formFields" filter. The values of AcroForm fields (text fields, choices, check boxes) are not part of the page's text and are left out by default; "include" places each value at its field's position in the reading order, where a label capture can pick it up.Chilkat Xbase++ Downloads
LOCAL nSuccess
LOCAL oPdf
LOCAL oJson
LOCAL oJsonOut
nSuccess := 0
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// The filled-in form used in this example is available at filled_form.pdf
oPdf := CreateObject("Chilkat.Pdf")
nSuccess := oPdf:LoadFile("qa_data/pdf/filled_form.pdf")
IF (nSuccess == 0)
? oPdf:LastErrorText
oPdf:destroy()
RETURN
ENDIF
oJson := CreateObject("Chilkat.JsonObject")
oJsonOut := CreateObject("Chilkat.JsonObject")
// By default only the page's own text is returned: the captions, without the values typed into the fields.
oJson:UpdateString("layout", "lines")
nSuccess := oPdf:ExtractText("text", oJson, oJsonOut)
IF (nSuccess == 0)
? oPdf:LastErrorText
oPdf:destroy()
oJson:destroy()
oJsonOut:destroy()
RETURN
ENDIF
? "---- default ----"
? oJsonOut:StringOf("text")
// With formFields included, each field's value appears where the field is on the page.
// A checked check box appears as [x]; an unchecked one has no value.
oJson:UpdateString("formFields", "include")
nSuccess := oPdf:ExtractText("text", oJson, oJsonOut)
IF (nSuccess == 0)
? oPdf:LastErrorText
oPdf:destroy()
oJson:destroy()
oJsonOut:destroy()
RETURN
ENDIF
? "---- formFields: include ----"
? oJsonOut:StringOf("text")
// A field value can be captured by the caption beside it, as for any other text.
oJson:UpdateString("label", "Email")
nSuccess := oPdf:ExtractText("text", oJson, oJsonOut)
IF (nSuccess == 0)
? oPdf:LastErrorText
oPdf:destroy()
oJson:destroy()
oJsonOut:destroy()
RETURN
ENDIF
? "Email: " + oJsonOut:StringOf("text")
// Sample output:
// ---- default ----
// Membership Application
// Name:
// Email:
// Phone:
// Newsletter: Yes, send me the monthly newsletter
// Volunteer: I am willing to volunteer
// ---- formFields: include ----
// Membership Application
// Name: Dana Whitfield
// Email: dana.whitfield@example.com
// Phone: 555-0142
// Newsletter: [x] Yes, send me the monthly newsletter
// Volunteer: I am willing to volunteer
// Email: dana.whitfield@example.com
oPdf:destroy()
oJson:destroy()
oJsonOut:destroy()