SQL Server Requires Chilkat v11.7.0+
SQL Server
Extract a PDF Table with Merged Cells
See more PDF Text Extraction Examples
Demonstrates how merged cells are reported by the "tables" operation: a header cell that spans several columns has colSpan greater than 1, and a row label that spans several rows has rowSpan greater than 1. Also shows the minRows and minColumns options.Chilkat SQL Server Downloads
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
DECLARE @hr int
DECLARE @iTmp0 int
DECLARE @iTmp1 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 PDF used in this example is available at spans_table.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/spans_table.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
-- Grids smaller than minRows x minColumns are not reported as tables. The defaults are 2 and 2.
-- Setting them higher skips small boxes that are not really tables.
EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'minRows', 3
EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'minColumns', 3
EXEC sp_OAMethod @pdf, 'ExtractText', @success OUT, 'tables', @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, 'IntOf', @iTmp0 OUT, 'count'
PRINT 'Tables found: ' + @iTmp0
EXEC sp_OAMethod @jsonOut, 'IntOf', @iTmp0 OUT, 'tables[0].rows'
EXEC sp_OAMethod @jsonOut, 'IntOf', @iTmp1 OUT, 'tables[0].columns'
PRINT @iTmp0 + ' rows x ' + @iTmp1 + ' columns'
-- A merged cell is reported once, at its top-left position, with its spans.
-- The positions it covers have no cell of their own.
DECLARE @rowSpan int
DECLARE @colSpan int
DECLARE @j int
SELECT @j = 0
DECLARE @count_j int
EXEC sp_OAMethod @jsonOut, 'SizeOfArray', @count_j OUT, 'tables[0].cells'
WHILE @j < @count_j
BEGIN
EXEC sp_OASetProperty @jsonOut, 'J', @j
EXEC sp_OAMethod @jsonOut, 'IntOf', @rowSpan OUT, 'tables[0].cells[j].rowSpan'
EXEC sp_OAMethod @jsonOut, 'IntOf', @colSpan OUT, 'tables[0].cells[j].colSpan'
IF (@rowSpan > 1) or (@colSpan > 1)
BEGIN
EXEC sp_OAMethod @jsonOut, 'IntOf', @iTmp0 OUT, 'tables[0].cells[j].row'
EXEC sp_OAMethod @jsonOut, 'IntOf', @iTmp1 OUT, 'tables[0].cells[j].col'
EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp0 OUT, 'tables[0].cells[j].text'
PRINT 'row ' + @iTmp0 + ', col ' + @iTmp1 + ': "' + @sTmp0 + '" spans ' + @rowSpan + ' row(s) and ' + @colSpan + ' column(s)'
END
SELECT @j = @j + 1
END
-- Sample output:
-- Tables found: 1
-- 6 rows x 6 columns
-- row 0, col 0: "Area" spans 2 row(s) and 1 column(s)
-- row 0, col 1: "City" spans 2 row(s) and 1 column(s)
-- row 0, col 2: "First Half" spans 1 row(s) and 2 column(s)
-- row 0, col 4: "Second Half" spans 1 row(s) and 2 column(s)
-- row 2, col 0: "East" spans 2 row(s) and 1 column(s)
-- row 4, col 0: "West" spans 2 row(s) and 1 column(s)
EXEC @hr = sp_OADestroy @pdf
EXEC @hr = sp_OADestroy @json
EXEC @hr = sp_OADestroy @jsonOut
END
GO