PureBasic Requires Chilkat v11.7.0+
PureBasic
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 PureBasic Downloads
IncludeFile "CkPdf.pb"
IncludeFile "CkJsonObject.pb"
Procedure ChilkatExample()
success.i = 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
pdf.i = CkPdf::ckCreate()
If pdf.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkPdf::ckLoadFile(pdf,"qa_data/pdf/filled_form.pdf")
If success = 0
Debug CkPdf::ckLastErrorText(pdf)
CkPdf::ckDispose(pdf)
ProcedureReturn
EndIf
json.i = CkJsonObject::ckCreate()
If json.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
jsonOut.i = CkJsonObject::ckCreate()
If jsonOut.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
; By default only the page's own text is returned: the captions, without the values typed into the fields.
CkJsonObject::ckUpdateString(json,"layout","lines")
success = CkPdf::ckExtractText(pdf,"text",json,jsonOut)
If success = 0
Debug CkPdf::ckLastErrorText(pdf)
CkPdf::ckDispose(pdf)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
ProcedureReturn
EndIf
Debug "---- default ----"
Debug CkJsonObject::ckStringOf(jsonOut,"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.
CkJsonObject::ckUpdateString(json,"formFields","include")
success = CkPdf::ckExtractText(pdf,"text",json,jsonOut)
If success = 0
Debug CkPdf::ckLastErrorText(pdf)
CkPdf::ckDispose(pdf)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
ProcedureReturn
EndIf
Debug "---- formFields: include ----"
Debug CkJsonObject::ckStringOf(jsonOut,"text")
; A field value can be captured by the caption beside it, as for any other text.
CkJsonObject::ckUpdateString(json,"label","Email")
success = CkPdf::ckExtractText(pdf,"text",json,jsonOut)
If success = 0
Debug CkPdf::ckLastErrorText(pdf)
CkPdf::ckDispose(pdf)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
ProcedureReturn
EndIf
Debug "Email: " + CkJsonObject::ckStringOf(jsonOut,"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
CkPdf::ckDispose(pdf)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
ProcedureReturn
EndProcedure