SQL Server Requires Chilkat v11.7.0+
SQL Server
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 SQL Server Downloads
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
DECLARE @hr 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 filled-in form used in this example is available at filled_form.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/filled_form.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
-- By default only the page's own text is returned: the captions, without the values typed into the fields.
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'layout', 'lines'
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
PRINT '---- default ----'
EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp0 OUT, 'text'
PRINT @sTmp0
-- 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.
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'formFields', 'include'
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
PRINT '---- formFields: include ----'
EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp0 OUT, 'text'
PRINT @sTmp0
-- A field value can be captured by the caption beside it, as for any other text.
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'label', 'Email'
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 'Email: ' + @sTmp0
-- 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
EXEC @hr = sp_OADestroy @pdf
EXEC @hr = sp_OADestroy @json
EXEC @hr = sp_OADestroy @jsonOut
END
GO