DataFlex Requires Chilkat v11.7.0+
DataFlex
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 DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoPdf
Variant vJson
Handle hoJson
Variant vJsonOut
Handle hoJsonOut
String sTemp1
Move False To iSuccess
// 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
Get Create (RefClass(cComChilkatPdf)) To hoPdf
If (Not(IsComObjectCreated(hoPdf))) Begin
Send CreateComObject of hoPdf
End
Get ComLoadFile Of hoPdf "qa_data/pdf/filled_form.pdf" To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoPdf To sTemp1
Showln sTemp1
Procedure_Return
End
Get Create (RefClass(cComChilkatJsonObject)) To hoJson
If (Not(IsComObjectCreated(hoJson))) Begin
Send CreateComObject of hoJson
End
Get Create (RefClass(cComChilkatJsonObject)) To hoJsonOut
If (Not(IsComObjectCreated(hoJsonOut))) Begin
Send CreateComObject of hoJsonOut
End
// By default only the page's own text is returned: the captions, without the values typed into the fields.
Get ComUpdateString Of hoJson "layout" "lines" To iSuccess
Get pvComObject of hoJson to vJson
Get pvComObject of hoJsonOut to vJsonOut
Get ComExtractText Of hoPdf "text" vJson vJsonOut To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoPdf To sTemp1
Showln sTemp1
Procedure_Return
End
Showln "---- default ----"
Get ComStringOf Of hoJsonOut "text" To sTemp1
Showln sTemp1
// 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.
Get ComUpdateString Of hoJson "formFields" "include" To iSuccess
Get pvComObject of hoJson to vJson
Get pvComObject of hoJsonOut to vJsonOut
Get ComExtractText Of hoPdf "text" vJson vJsonOut To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoPdf To sTemp1
Showln sTemp1
Procedure_Return
End
Showln "---- formFields: include ----"
Get ComStringOf Of hoJsonOut "text" To sTemp1
Showln sTemp1
// A field value can be captured by the caption beside it, as for any other text.
Get ComUpdateString Of hoJson "label" "Email" To iSuccess
Get pvComObject of hoJson to vJson
Get pvComObject of hoJsonOut to vJsonOut
Get ComExtractText Of hoPdf "text" vJson vJsonOut To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoPdf To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComStringOf Of hoJsonOut "text" To sTemp1
Showln "Email: " sTemp1
// 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
End_Procedure