SQL Server Requires Chilkat v11.7.0+
SQL Server
Get the Text Block Below a PDF Caption
See more PDF Text Extraction Examples
Demonstrates the "label" option with labelValue "below", which captures the block of text directly under a caption such as "Ship To", ending at the next gap or heading. Combined with layout "lines", the lines of an address block are kept separate.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 packing slip used in this example is available at packing_slip.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/packing_slip.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
-- The packing slip has the captions "Bill To" and "Ship To" side by side,
-- each with an address block below it.
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'label', 'Ship To'
-- "below" means the value is the block of text directly under the label.
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'labelValue', 'below'
-- With the default "flow" layout the lines of the block would be joined into one paragraph.
-- "lines" keeps each line of the address on its own line.
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 'Ship To:'
EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp0 OUT, 'text'
PRINT @sTmp0
PRINT ''
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'label', 'Bill To'
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 'Bill To:'
EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp0 OUT, 'text'
PRINT @sTmp0
PRINT ''
-- A caption over a short paragraph works the same way. Here the flow layout is better,
-- because the two lines are one piece of text.
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'label', 'Special Instructions'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'layout', 'flow'
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 'Special Instructions:'
EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp0 OUT, 'text'
PRINT @sTmp0
-- Sample output:
-- Ship To:
-- Frank Delgado
-- c/o Harbor Pet Boarding
-- 9 Ferry Lane
-- Portside, WA 98001
--
-- Bill To:
-- Frank Delgado
-- Apartment 3B
-- 41 Mill Street
-- Portside, WA 98000
--
-- Special Instructions:
-- Leave the carton with the boarding office if no one answers. Do not leave it outside; the cat tree is not packed for rain.
EXEC @hr = sp_OADestroy @pdf
EXEC @hr = sp_OADestroy @json
EXEC @hr = sp_OADestroy @jsonOut
END
GO